from typing import List, Optional

from pydantic import BaseModel, EmailStr, model_validator


class FileAttachment(BaseModel):
    file_name: str
    url: str
    file_id: Optional[str] = None


class SendMailRequest(BaseModel):
    to: List[EmailStr]
    subject: str
    body: str
    from_email: Optional[EmailStr] = None
    cc: List[EmailStr] = []
    bcc: List[EmailStr] = []

    @model_validator(mode="after")
    def _deduplicate_recipients(self) -> "SendMailRequest":
        seen: set[str] = set()

        def dedupe(emails: List[EmailStr]) -> List[EmailStr]:
            unique: List[EmailStr] = []
            for email in emails:
                key = str(email).strip().lower()
                if not key or key in seen:
                    continue
                seen.add(key)
                unique.append(email)
            return unique

        self.to = dedupe(self.to)
        self.cc = dedupe(self.cc)
        self.bcc = dedupe(self.bcc)
        return self
