# @File: backend/models/smtp_settings.py
# @Desc: SQLAlchemy model for storing SMTP credentials and sender identity that
#        the admin configures through the CMS Notifications page. The table is
#        effectively singleton (id=1 is always the active record); we keep a
#        surrogate primary key so SQLAlchemy has an easy time with upserts.
from datetime import datetime

from core.database import Base
from sqlalchemy import Boolean, Column, DateTime, Integer, String


class SmtpSettings(Base):
    __tablename__ = "smtp_settings"
    __table_args__ = {"extend_existing": True}

    id = Column(Integer, primary_key=True, index=True, autoincrement=True, nullable=False)

    # Core SMTP credentials
    smtp_host = Column(String(255), nullable=True)
    smtp_port = Column(Integer, nullable=True)
    smtp_username = Column(String(255), nullable=True)
    smtp_password = Column(String(512), nullable=True)
    smtp_encryption = Column(String(16), nullable=True, default="TLS")  # NONE | TLS | SSL

    # Sender identity
    sender_name = Column(String(255), nullable=True)
    sender_email = Column(String(255), nullable=True)
    reply_to = Column(String(255), nullable=True)

    # Event toggles (persisted so they can be read at runtime if we need them)
    evt_new_ticket = Column(Boolean, nullable=False, default=True)
    evt_invoice = Column(Boolean, nullable=False, default=True)
    evt_service_up = Column(Boolean, nullable=False, default=True)
    evt_service_down = Column(Boolean, nullable=False, default=True)
    evt_user_register = Column(Boolean, nullable=False, default=True)
    evt_payment_fail = Column(Boolean, nullable=False, default=True)

    created_at = Column(DateTime(timezone=True), default=datetime.now)
    updated_at = Column(DateTime(timezone=True), default=datetime.now, onupdate=datetime.now)