"""
SMTP-Konfiguration pro Firma.
Passwort wird Fernet-verschlüsselt gespeichert (gleiche Methode wie ldap_service).
"""
import uuid

from sqlalchemy import Boolean, Integer, String, Text, UniqueConstraint
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column

from app.core.database import Base


class SmtpConfig(Base):
    __tablename__ = "smtp_configs"
    __table_args__ = (UniqueConstraint("company_id"),)

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    company_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False, index=True)

    host: Mapped[str] = mapped_column(String(255), nullable=False)
    port: Mapped[int] = mapped_column(Integer, default=587, nullable=False)
    use_tls: Mapped[bool] = mapped_column(Boolean, default=False)       # SMTPS port 465
    use_starttls: Mapped[bool] = mapped_column(Boolean, default=True)   # STARTTLS port 587

    username: Mapped[str | None] = mapped_column(String(255))
    password_encrypted: Mapped[str | None] = mapped_column(Text)

    from_email: Mapped[str] = mapped_column(String(255), nullable=False)
    from_name: Mapped[str] = mapped_column(String(255), default="TimeMaster", nullable=False)

    is_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
