# @File: backend/schemas/smtp_settings.py
# @Desc: Pydantic schemas for the admin-facing SMTP settings endpoint.
from typing import Literal, Optional

from pydantic import BaseModel, EmailStr, Field

Encryption = Literal["NONE", "TLS", "SSL"]


class SmtpSettingsBase(BaseModel):
    smtp_host: Optional[str] = Field(None, description="SMTP server hostname")
    smtp_port: Optional[int] = Field(None, ge=1, le=65535, description="SMTP server port")
    smtp_username: Optional[str] = Field(None, description="SMTP auth username")
    smtp_encryption: Encryption = Field("TLS", description="Transport encryption mode")

    sender_name: Optional[str] = Field(None, description="Default From display name")
    sender_email: Optional[EmailStr] = Field(None, description="Default From email address")
    reply_to: Optional[EmailStr] = Field(None, description="Default Reply-To address")

    evt_new_ticket: bool = True
    evt_invoice: bool = True
    evt_service_up: bool = True
    evt_service_down: bool = True
    evt_user_register: bool = True
    evt_payment_fail: bool = True


class SmtpSettingsUpdate(SmtpSettingsBase):
    """Payload accepted by the admin-only PUT endpoint.

    ``smtp_password`` is optional so the admin can edit other fields without
    re-typing the password. An empty string means "leave the stored password
    as-is"; a non-empty string replaces it.
    """

    smtp_password: Optional[str] = Field(None, description="SMTP auth password (leave blank to keep existing)")


class SmtpSettingsResponse(SmtpSettingsBase):
    """Response shape — the password is never echoed back to the client."""

    has_password: bool = Field(
        False,
        description="True when a password is stored in the database. The actual value is never returned.",
    )


class SmtpTestRequest(BaseModel):
    to_email: EmailStr = Field(..., description="Recipient of the test email")


class SmtpTestResponse(BaseModel):
    success: bool
    message: str