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] = None
    fax: Optional[str] = None
    email: Optional[str] = None
    country: Optional[str] = None
    tax_id: Optional[str] = None
    personal_id: Optional[str] = None
    position: Optional[str] = None
    license: Optional[str] = Field(None, max_length=4, pattern=r"^\d*$")
    company: Optional[str] = None
    contact: Optional[str] = None


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["CustomsBrokerVUCreateDTO"] = 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] = None
    fax: Optional[str] = None
    email: Optional[str] = None
    country: Optional[str] = None
    tax_id: Optional[str] = None
    personal_id: Optional[str] = None
    position: Optional[str] = None
    license: Optional[str] = Field(None, max_length=4, pattern=r"^\d*$")
    company: Optional[str] = None
    contact: Optional[str] = None
    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

    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] = None
    personal_id: Optional[str] = None
    position: Optional[str] = None
    license: Optional[str] = Field(None, max_length=4, pattern=r"^\d*$")
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    middle_name: Optional[str] = None
    email: Optional[str] = None

    class Config:
        from_attributes = True
