"""
Schema Pydantic per le Virtual Box.
"""

import uuid
from datetime import datetime
from typing import Literal

from pydantic import BaseModel, Field


# ─── VirtualBoxRule ───────────────────────────────────────────────────────────

VBoxField = Literal["mailbox_id", "imap_folder", "subject", "from_address", "to_address"]
VBoxOperator = Literal["contains", "equals", "starts_with", "ends_with", "regex"]


class VirtualBoxRuleCreate(BaseModel):
    field: VBoxField
    operator: VBoxOperator = "contains"
    value: str = Field(..., min_length=1)
    date_from: str | None = None   # YYYY-MM-DD
    date_to: str | None = None     # YYYY-MM-DD


class VirtualBoxRuleResponse(BaseModel):
    id: uuid.UUID
    virtual_box_id: uuid.UUID
    field: str
    operator: str
    value: str
    date_from: str | None
    date_to: str | None
    created_at: datetime

    model_config = {"from_attributes": True}


# ─── Mailbox breve (usata nella risposta VirtualBox) ─────────────────────────

class MailboxBriefResponse(BaseModel):
    id: uuid.UUID
    email_address: str
    display_name: str | None
    status: str = "active"

    model_config = {"from_attributes": True}


# ─── VirtualBox ───────────────────────────────────────────────────────────────

class VirtualBoxCreate(BaseModel):
    name: str = Field(..., min_length=1, max_length=255)
    description: str | None = None
    label: str | None = None
    rules: list[VirtualBoxRuleCreate] = []
    mailbox_ids: list[uuid.UUID] = Field(
        default=[],
        description="ID delle caselle PEC reali a cui è associata questa Virtual Box",
    )


class VirtualBoxUpdate(BaseModel):
    name: str | None = None
    description: str | None = None
    label: str | None = None
    is_active: bool | None = None
    mailbox_ids: list[uuid.UUID] | None = Field(
        default=None,
        description="Se fornito, sostituisce completamente la lista di caselle associate",
    )


class VirtualBoxResponse(BaseModel):
    id: uuid.UUID
    tenant_id: uuid.UUID
    name: str
    description: str | None
    label: str | None
    is_active: bool
    created_by: uuid.UUID | None
    created_at: datetime
    updated_at: datetime
    rules: list[VirtualBoxRuleResponse] = []
    assignment_count: int = 0
    mailboxes: list[MailboxBriefResponse] = []

    model_config = {"from_attributes": True}


class VirtualBoxListResponse(BaseModel):
    items: list[VirtualBoxResponse]
    total: int
    page: int
    page_size: int


# ─── VirtualBoxAssignment ─────────────────────────────────────────────────────

class VirtualBoxAssignRequest(BaseModel):
    user_ids: list[uuid.UUID] = Field(..., min_length=1)


class VirtualBoxAssignmentResponse(BaseModel):
    id: uuid.UUID
    virtual_box_id: uuid.UUID
    user_id: uuid.UUID
    assigned_by: uuid.UUID | None
    assigned_at: datetime

    model_config = {"from_attributes": True}


class AssignedUserResponse(BaseModel):
    user_id: uuid.UUID
    user_email: str
    user_full_name: str
    assigned_at: datetime


# ─── Gestione caselle associate ───────────────────────────────────────────────

class VirtualBoxMailboxAssignRequest(BaseModel):
    mailbox_ids: list[uuid.UUID] = Field(
        ...,
        min_length=1,
        description="Lista degli ID delle caselle PEC da associare",
    )
