"""
Schema Pydantic per tenant (super-admin).
"""

import uuid
from datetime import datetime
from typing import Literal

from pydantic import BaseModel, Field, field_validator

TenantPlanType = Literal["starter", "pro", "enterprise"]


class TenantCreateRequest(BaseModel):
    slug: str = Field(min_length=3, max_length=63, pattern=r"^[a-z0-9-]+$")
    name: str = Field(min_length=2, max_length=255)
    plan: TenantPlanType = "starter"
    max_mailboxes: int = Field(default=5, ge=1, le=1000)
    max_users: int = Field(default=10, ge=1, le=1000)

    # Utente admin iniziale
    admin_email: str
    admin_password: str = Field(min_length=8)
    admin_full_name: str = Field(min_length=2, max_length=255)

    @field_validator("slug")
    @classmethod
    def validate_slug(cls, v: str) -> str:
        reserved = {"api", "admin", "www", "mail", "smtp", "imap", "pecflow", "app"}
        if v in reserved:
            raise ValueError(f"Slug '{v}' riservato")
        return v.lower()


class TenantUpdateRequest(BaseModel):
    name: str | None = Field(default=None, min_length=2, max_length=255)
    plan: TenantPlanType | None = None
    is_active: bool | None = None
    max_mailboxes: int | None = Field(default=None, ge=1, le=1000)
    max_users: int | None = Field(default=None, ge=1, le=1000)


class TenantResponse(BaseModel):
    id: uuid.UUID
    slug: str
    name: str
    plan: str
    is_active: bool
    max_mailboxes: int
    max_users: int
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}
