"""
DTOs (Data Transfer Objects) para módulo de tenants
Reemplaza schemas.py siguiendo enfoque DDD y estilo NestJS
"""

from datetime import datetime
from enum import Enum
from typing import Optional

from pydantic import BaseModel, ConfigDict, EmailStr, Field


class TenantTypeDTO(str, Enum):
    """Tipo de tenant"""

    SHARED = "shared"
    DEDICATED = "dedicated"


class TenantCreateDTO(BaseModel):
    """DTO para crear un nuevo tenant"""

    name: str = Field(
        ..., min_length=3, max_length=255, description="Nombre del tenant"
    )
    slug: str = Field(
        ..., min_length=3, max_length=100, description="Identificador único del tenant"
    )
    keycloak_realm: str = Field(
        ..., min_length=3, max_length=255, description="Nombre del realm en Keycloak"
    )
    type: TenantTypeDTO = Field(
        default=TenantTypeDTO.SHARED, description="Tipo de tenant"
    )

    contact_name: Optional[str] = Field(
        None, max_length=255, description="Nombre de contacto"
    )
    contact_email: Optional[EmailStr] = Field(None, description="Email de contacto")
    contact_phone: Optional[str] = Field(
        None, max_length=50, description="Teléfono de contacto"
    )

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "name": "Empresa ABC S.A. de C.V.",
                "slug": "empresa-abc",
                "keycloak_realm": "empresa-abc-realm",
                "type": "shared",
                "contact_name": "Juan Pérez",
                "contact_email": "juan.perez@empresa-abc.com",
                "contact_phone": "+52 55 1234 5678",
            }
        }
    )


class TenantUpdateDTO(BaseModel):
    """DTO para actualizar un tenant"""

    name: Optional[str] = Field(None, min_length=3, max_length=255)
    contact_name: Optional[str] = Field(None, max_length=255)
    contact_email: Optional[EmailStr] = None
    contact_phone: Optional[str] = Field(None, max_length=50)
    is_active: Optional[bool] = None

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "name": "Empresa ABC S.A. de C.V. - Actualizado",
                "contact_email": "nuevo@empresa-abc.com",
            }
        }
    )


class TenantResponseDTO(BaseModel):
    """DTO para respuesta de tenant"""

    id: int
    name: str
    slug: str
    type: TenantTypeDTO
    keycloak_realm: str
    contact_name: Optional[str]
    contact_email: Optional[str]
    contact_phone: Optional[str]
    is_active: bool
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(
        from_attributes=True,
        json_schema_extra={
            "example": {
                "id": 1,
                "name": "Empresa ABC S.A. de C.V.",
                "slug": "empresa-abc",
                "type": "shared",
                "keycloak_realm": "empresa-abc-realm",
                "contact_name": "Juan Pérez",
                "contact_email": "juan.perez@empresa-abc.com",
                "contact_phone": "+52 55 1234 5678",
                "is_active": True,
                "created_at": "2025-01-15T10:30:00Z",
                "updated_at": "2025-01-15T10:30:00Z",
            }
        },
    )


class TenantListResponseDTO(BaseModel):
    """DTO para lista de tenants"""

    tenants: list[TenantResponseDTO]
    total: int
    page: int
    page_size: int
