from typing import Optional

from pydantic import BaseModel, Field


class CustomsBrokerBaseDTO(BaseModel):
    """Base fields for CustomsBroker"""

    type: Optional[str] = None
    name: Optional[str] = None
    address: Optional[str] = None
    postal_code: Optional[str] = None
    city: Optional[str] = None
    state: Optional[str] = None
    phone: Optional[str] = Field(None, pattern=r"^$|^[\d\s\-\+\(\)]+$")
    fax: Optional[str] = None
    email: Optional[str] = Field(None, pattern=r"^$|^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
    country: Optional[str] = None
    tax_id: Optional[str] = Field(None, pattern=r"^$|^[A-Z&Ñ]{3,4}\d{6}[A-Z0-9]{3}$")
    personal_id: Optional[str] = Field(None, pattern=r"^$|^[A-Z][AEIOUX][A-Z]{2}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])[HM](AS|BC|BS|CC|CS|CH|CL|CM|DF|DG|GT|GR|HG|JC|MC|MN|MS|NT|NL|OC|PL|QT|QR|SP|SL|SR|TC|TS|TL|VZ|YN|ZS|NE)[B-DF-HJ-NP-TV-Z]{3}[0-9A-Z]\d$")
    position: Optional[str] = None
    license: Optional[str] = Field(None, max_length=4, pattern=r"^$|^[0-9]*[1-9][0-9]*$")
    company: Optional[str] = None
    contact: Optional[str] = Field(None, pattern=r"^$|^[a-zA-Z0-9\sñÑáéíóúÁÉÍÓÚ\-\.,]+$")


class CustomsBrokerCreateDTO(CustomsBrokerBaseDTO):
    """Schema for creating a new CustomsBroker"""

    broker_key: str = Field(..., max_length=5, pattern=r"^[a-zA-Z0-9]+$", description="Clave única del agente aduanal (máx 5 caracteres)")


class CustomsBrokerUpdateDTO(CustomsBrokerBaseDTO):
    """Schema for updating an existing CustomsBroker"""

    pass


class CustomsBrokerResponseDTO(CustomsBrokerBaseDTO):
    """Schema for CustomsBroker response"""

    id: int
    broker_key: str
    tenant_id: int
    company_id: int
    vu: Optional["CustomsBrokerVUResponseDTO"] = None

    class Config:
        from_attributes = True


# Legacy DTO for backwards compatibility (if needed elsewhere)
class CustomsBrokerDTO(BaseModel):
    type: Optional[str] = None
    broker_key: str = Field(..., max_length=5, pattern=r"^[a-zA-Z0-9]+$")
    name: Optional[str] = None
    address: Optional[str] = None
    postal_code: Optional[str] = None
    city: Optional[str] = None
    state: Optional[str] = None
    phone: Optional[str] = Field(None, pattern=r"^$|^[\d\s\-\+\(\)]+$")
    fax: Optional[str] = None
    email: Optional[str] = Field(None, pattern=r"^$|^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
    country: Optional[str] = None
    tax_id: Optional[str] = Field(None, pattern=r"^$|^[A-Z&Ñ]{3,4}\d{6}[A-Z0-9]{3}$")
    personal_id: Optional[str] = Field(None, pattern=r"^$|^[A-Z][AEIOUX][A-Z]{2}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])[HM](AS|BC|BS|CC|CS|CH|CL|CM|DF|DG|GT|GR|HG|JC|MC|MN|MS|NT|NL|OC|PL|QT|QR|SP|SL|SR|TC|TS|TL|VZ|YN|ZS|NE)[B-DF-HJ-NP-TV-Z]{3}[0-9A-Z]\d$")
    position: Optional[str] = None
    license: Optional[str] = Field(None, max_length=4, pattern=r"^$|^[0-9]*[1-9][0-9]*$")
    company: Optional[str] = None
    contact: Optional[str] = Field(None, pattern=r"^$|^[a-zA-Z0-9\sñÑáéíóúÁÉÍÓÚ\-\.,]+$")
    tenant_id: str
    company_id: str

    class Config:
        from_attributes = True


class CustomsBrokerVUCreateDTO(BaseModel):
    certificate_path: Optional[str] = None
    key_path: Optional[str] = None
    access_key: Optional[str] = None
    fiel_format: Optional[str] = None
    signature_read_path: Optional[str] = None
    archive_path: Optional[str] = None
    fiel_access_key: Optional[str] = None
    web_service_user: Optional[str] = None
    web_service_access_key: Optional[str] = None
    vu_email: Optional[str] = None
    vu_figure_type: Optional[str] = None
    xml_files_path: Optional[str] = None
    query_tax_id: Optional[str] = None
    doda_certificate_path: Optional[str] = None
    doda_key_path: Optional[str] = None
    doda_web_service_user: Optional[str] = None
    doda_web_service_access_key: Optional[str] = None
    doda_fiel_access_key: Optional[str] = None
    doda_xml_files_path: Optional[str] = None
    tenant_id: Optional[int] = None
    company_id: Optional[int] = None

class CustomsBrokerVUResponseDTO(CustomsBrokerVUCreateDTO):
    customs_broker_id: int

    class Config:
        from_attributes = True


class CustomsBrokerPersonnelDTO(BaseModel):
    broker_key: str = Field(..., max_length=5, pattern=r"^[a-zA-Z0-9]+$")
    line: int
    name: Optional[str] = None
    tax_id: Optional[str] = Field(None, pattern=r"^$|^[A-Z&Ñ]{3,4}\d{6}[A-Z0-9]{3}$")
    personal_id: Optional[str] = Field(None, pattern=r"^$|^[A-Z][AEIOUX][A-Z]{2}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])[HM](AS|BC|BS|CC|CS|CH|CL|CM|DF|DG|GT|GR|HG|JC|MC|MN|MS|NT|NL|OC|PL|QT|QR|SP|SL|SR|TC|TS|TL|VZ|YN|ZS|NE)[B-DF-HJ-NP-TV-Z]{3}[0-9A-Z]\d$")
    position: Optional[str] = None
    license: Optional[str] = Field(None, max_length=4, pattern=r"^$|^[0-9]*[1-9][0-9]*$")
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    middle_name: Optional[str] = None
    email: Optional[str] = Field(None, pattern=r"^$|^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
    tenant_id: Optional[int] = None
    company_id: Optional[int] = None

    class Config:
        from_attributes = True
