"""
Schemi Pydantic per la gestione delle firme automatiche.
"""

import uuid
from datetime import datetime
from typing import Literal

from pydantic import BaseModel, Field


# ─── Signature ────────────────────────────────────────────────────────────────

class SignatureCreate(BaseModel):
    name: str = Field(..., min_length=1, max_length=255)
    description: str | None = Field(None)
    body_html: str | None = Field(None)
    body_text: str | None = Field(None)


class SignatureUpdate(BaseModel):
    name: str | None = Field(None, min_length=1, max_length=255)
    description: str | None = Field(None)
    body_html: str | None = Field(None)
    body_text: str | None = Field(None)


class SignatureResponse(BaseModel):
    id: uuid.UUID
    tenant_id: uuid.UUID
    name: str
    description: str | None
    body_html: str | None
    body_text: str | None
    created_by: uuid.UUID | None
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}


class SignatureListResponse(BaseModel):
    items: list[SignatureResponse]
    total: int


# ─── SignatureAssignment ───────────────────────────────────────────────────────

SignatureContext = Literal["reply", "compose", "both"]


class SignatureAssignmentCreate(BaseModel):
    signature_id: uuid.UUID
    mailbox_id: uuid.UUID | None = None
    virtual_box_id: uuid.UUID | None = None
    context: SignatureContext = "both"


class SignatureAssignmentResponse(BaseModel):
    id: uuid.UUID
    tenant_id: uuid.UUID
    signature_id: uuid.UUID
    mailbox_id: uuid.UUID | None
    virtual_box_id: uuid.UUID | None
    context: str
    created_at: datetime
    # Nome della firma (join eagerly nel service)
    signature_name: str | None = None

    model_config = {"from_attributes": True}


class SignatureAssignmentListResponse(BaseModel):
    items: list[SignatureAssignmentResponse]
    total: int
