"""
DTOs (Data Transfer Objects) para módulo de DODA
"""

from datetime import datetime
from decimal import Decimal
from typing import Optional, List

from pydantic import AliasChoices, BaseModel, Field


# ============ DODA CONTAINER SEAL DTOS ============
class DodaContainerSealCreateDTO(BaseModel):
    """DTO para crear un candado de contenedor"""

    seal_value: Optional[str] = Field(
        None, max_length=21, description="Seal value")

    class Config:
        from_attributes = True


class DodaContainerSealResponseDTO(BaseModel):
    """DTO para responder con datos de un candado"""

    id: int
    doda_sys_id: int = Field(
        validation_alias=AliasChoices("doda_sys_id", "doda_id")
    )
    seal_line: int
    seal_value: Optional[str] = None

    class Config:
        from_attributes = True


# ============ DODA CONTAINER DTOS ============
class DodaContainerCreateDTO(BaseModel):
    """DTO para crear un contenedor"""

    container_value: Optional[str] = Field(
        None, max_length=20, description="Container value")
    seals: Optional[str] = Field(None, max_length=254, description="Seals")
    seals_detail: Optional[List[DodaContainerSealCreateDTO]] = Field(
        None, description="Container seals"
    )

    class Config:
        from_attributes = True


class DodaContainerUpdateDTO(BaseModel):
    """DTO para actualizar un contenedor"""

    container_value: Optional[str] = Field(
        None, max_length=20, description="Container value")
    seals: Optional[str] = Field(None, max_length=254, description="Seals")

    class Config:
        from_attributes = True


class DodaContainerResponseDTO(BaseModel):
    """DTO para responder con datos de un contenedor"""

    id: int
    doda_sys_id: int = Field(
        validation_alias=AliasChoices("doda_sys_id", "doda_id")
    )
    container_line: int
    container_value: Optional[str] = None
    seals: Optional[str] = None
    seals_detail: Optional[List[DodaContainerSealResponseDTO]] = None

    class Config:
        from_attributes = True


# ============ DODA AMERICAN PEDIMENTO DTOS ============
class DodaAmericanPedimentoCreateDTO(BaseModel):
    """DTO para crear un pedimento americano"""

    american_pedimento_type: Optional[str] = Field(
        None, max_length=2, description="American pedimento type"
    )
    american_pedimento_value: Optional[str] = Field(
        None, max_length=20, description="American pedimento value"
    )

    class Config:
        from_attributes = True


class DodaAmericanPedimentoUpdateDTO(BaseModel):
    """DTO para actualizar un pedimento americano"""

    american_pedimento_type: Optional[str] = Field(
        None, max_length=2, description="American pedimento type"
    )
    american_pedimento_value: Optional[str] = Field(
        None, max_length=20, description="American pedimento value"
    )

    class Config:
        from_attributes = True


class DodaAmericanPedimentoResponseDTO(BaseModel):
    """DTO para responder con datos de un pedimento americano"""

    id: int
    doda_sys_id: int = Field(
        validation_alias=AliasChoices("doda_sys_id", "doda_id")
    )
    american_pedimento_line: int
    american_pedimento_type: Optional[str] = None
    american_pedimento_value: Optional[str] = None

    class Config:
        from_attributes = True


# ============ DODA PEDIMENTO DTOS ============
class DodaPedimentoCreateDTO(BaseModel):
    """DTO para crear un pedimento DODA"""

    authorization_patent: Optional[str] = Field(
        None, max_length=10, description="Authorization patent"
    )
    document: Optional[str] = Field(
        None, max_length=50, description="Document")
    shipment: Optional[str] = Field(
        None, max_length=11, description="Shipment")
    cove: Optional[str] = Field(None, max_length=50, description="COVE")
    umc: Optional[str] = Field(None, max_length=20, description="UMC")
    effective_amount_usd: Optional[Decimal] = Field(
        None, description="Effective amount USD")
    difference_amount_usd: Optional[Decimal] = Field(
        None, description="Difference amount USD")
    dta_niu: Optional[str] = Field(None, max_length=20, description="DTA NIU")
    article_7: Optional[bool] = Field(None, description="Article 7")
    pedimento_sys_id: Optional[int] = Field(
        None, description="Pedimento system ID")
    invoice_line: Optional[int] = Field(None, description="Invoice line")
    part_ii_line: Optional[int] = Field(None, description="Part II line")
    pedimento_type: Optional[str] = Field(
        None, max_length=20, description="Pedimento type")
    zero_packaging_validation: Optional[bool] = Field(
        None, description="Zero packaging validation"
    )

    class Config:
        from_attributes = True


class DodaPedimentoUpdateDTO(BaseModel):
    """DTO para actualizar un pedimento DODA"""

    authorization_patent: Optional[str] = Field(
        None, max_length=10, description="Authorization patent"
    )
    document: Optional[str] = Field(
        None, max_length=50, description="Document")
    shipment: Optional[str] = Field(
        None, max_length=11, description="Shipment")
    cove: Optional[str] = Field(None, max_length=50, description="COVE")
    umc: Optional[str] = Field(None, max_length=20, description="UMC")
    effective_amount_usd: Optional[Decimal] = Field(
        None, description="Effective amount USD")
    difference_amount_usd: Optional[Decimal] = Field(
        None, description="Difference amount USD")
    dta_niu: Optional[str] = Field(None, max_length=20, description="DTA NIU")
    article_7: Optional[bool] = Field(None, description="Article 7")
    pedimento_sys_id: Optional[int] = Field(
        None, description="Pedimento system ID")
    invoice_line: Optional[int] = Field(None, description="Invoice line")
    part_ii_line: Optional[int] = Field(None, description="Part II line")
    pedimento_type: Optional[str] = Field(
        None, max_length=20, description="Pedimento type")
    zero_packaging_validation: Optional[bool] = Field(
        None, description="Zero packaging validation"
    )

    class Config:
        from_attributes = True


class DodaPedimentoResponseDTO(BaseModel):
    """DTO para responder con datos de un pedimento DODA"""

    id: int
    doda_sys_id: int = Field(
        validation_alias=AliasChoices("doda_sys_id", "doda_id")
    )
    pedimento_line: int
    authorization_patent: Optional[str] = None
    document: Optional[str] = None
    shipment: Optional[str] = None
    cove: Optional[str] = None
    umc: Optional[str] = None
    effective_amount_usd: Optional[Decimal] = None
    difference_amount_usd: Optional[Decimal] = None
    dta_niu: Optional[str] = None
    article_7: Optional[bool] = None
    pedimento_sys_id: Optional[int] = None
    invoice_line: Optional[int] = None
    part_ii_line: Optional[int] = None
    pedimento_type: Optional[str] = None
    zero_packaging_validation: Optional[bool] = None

    class Config:
        from_attributes = True


# ============ MAIN DODA DTOS ============
class DodaCreateDTO(BaseModel):
    """DTO para crear un DODA"""

    integration_number: Optional[str] = Field(
        None, max_length=30, description="Integration number")
    doda_date: Optional[int] = Field(None, description="DODA date")
    doda_time: Optional[int] = Field(None, description="DODA time")
    dispatch_customs: Optional[str] = Field(
        None, max_length=3, description="Dispatch customs")
    customs_sections: Optional[str] = Field(
        None, max_length=3, description="Customs sections")
    patent: Optional[str] = Field(None, max_length=4, description="Patent")
    pedimentos: Optional[str] = Field(
        None, max_length=80, description="Pedimentos")
    caat: Optional[str] = Field(None, max_length=10, description="CAAT")
    transport_identification: Optional[str] = Field(
        None, max_length=20, description="Transport identification"
    )
    fast_id: Optional[str] = Field(None, max_length=20, description="FAST ID")
    operation_type: Optional[str] = Field(
        None, max_length=1, description="Operation type")
    selected: Optional[bool] = Field(None, description="Selected")
    user_selected: Optional[str] = Field(
        None, max_length=30, description="User selected")
    last_user: Optional[str] = Field(
        None, max_length=30, description="Last user")
    responsible: Optional[str] = Field(
        None, max_length=14, description="Responsible")
    carrier: Optional[str] = Field(None, max_length=8, description="Carrier")
    shipments: Optional[str] = Field(
        None, max_length=80, description="Shipments")
    pedimento_type: Optional[str] = Field(
        None, max_length=30, description="Pedimento type")
    original_chain: Optional[str] = Field(
        None, max_length=5000, description="Original chain")
    serial_number: Optional[str] = Field(
        None, max_length=21, description="Serial number")
    electronic_signature: Optional[str] = Field(
        None, max_length=2000, description="Electronic signature"
    )
    transaction_number: Optional[str] = Field(
        None, max_length=30, description="Transaction number")
    status: Optional[str] = Field(None, max_length=30, description="Status")
    linq_sat_qr: Optional[str] = Field(
        None, max_length=1000, description="LINQ SAT QR")
    sat_certificate: Optional[str] = Field(
        None, max_length=2001, description="SAT certificate")
    sat_digital_seal: Optional[str] = Field(
        None, description="SAT digital seal")
    xml_doda_sent_path: Optional[str] = Field(
        None, max_length=1000, description="XML DODA sent path")
    xml_doda_response_path: Optional[str] = Field(
        None, max_length=1000, description="XML DODA response path"
    )
    sat_original_chain: Optional[str] = Field(
        None, description="SAT original chain")
    customs_clearance: Optional[int] = Field(
        None, description="Customs clearance")
    unique_badge_number: Optional[str] = Field(
        None, max_length=250, description="Unique badge number"
    )

    class Config:
        from_attributes = True


class DodaUpdateDTO(BaseModel):
    """DTO para actualizar un DODA"""

    integration_number: Optional[str] = Field(
        None, max_length=30, description="Integration number")
    doda_date: Optional[int] = Field(None, description="DODA date")
    doda_time: Optional[int] = Field(None, description="DODA time")
    dispatch_customs: Optional[str] = Field(
        None, max_length=3, description="Dispatch customs")
    customs_sections: Optional[str] = Field(
        None, max_length=3, description="Customs sections")
    patent: Optional[str] = Field(None, max_length=4, description="Patent")
    pedimentos: Optional[str] = Field(
        None, max_length=80, description="Pedimentos")
    caat: Optional[str] = Field(None, max_length=10, description="CAAT")
    transport_identification: Optional[str] = Field(
        None, max_length=20, description="Transport identification"
    )
    fast_id: Optional[str] = Field(None, max_length=20, description="FAST ID")
    operation_type: Optional[str] = Field(
        None, max_length=1, description="Operation type")
    selected: Optional[bool] = Field(None, description="Selected")
    user_selected: Optional[str] = Field(
        None, max_length=30, description="User selected")
    last_user: Optional[str] = Field(
        None, max_length=30, description="Last user")
    responsible: Optional[str] = Field(
        None, max_length=14, description="Responsible")
    carrier: Optional[str] = Field(None, max_length=8, description="Carrier")
    shipments: Optional[str] = Field(
        None, max_length=80, description="Shipments")
    pedimento_type: Optional[str] = Field(
        None, max_length=30, description="Pedimento type")
    original_chain: Optional[str] = Field(
        None, max_length=5000, description="Original chain")
    serial_number: Optional[str] = Field(
        None, max_length=21, description="Serial number")
    electronic_signature: Optional[str] = Field(
        None, max_length=2000, description="Electronic signature"
    )
    transaction_number: Optional[str] = Field(
        None, max_length=30, description="Transaction number")
    status: Optional[str] = Field(None, max_length=30, description="Status")
    linq_sat_qr: Optional[str] = Field(
        None, max_length=1000, description="LINQ SAT QR")
    sat_certificate: Optional[str] = Field(
        None, max_length=2001, description="SAT certificate")
    sat_digital_seal: Optional[str] = Field(
        None, description="SAT digital seal")
    xml_doda_sent_path: Optional[str] = Field(
        None, max_length=1000, description="XML DODA sent path")
    xml_doda_response_path: Optional[str] = Field(
        None, max_length=1000, description="XML DODA response path"
    )
    sat_original_chain: Optional[str] = Field(
        None, description="SAT original chain")
    customs_clearance: Optional[int] = Field(
        None, description="Customs clearance")
    unique_badge_number: Optional[str] = Field(
        None, max_length=250, description="Unique badge number"
    )

    class Config:
        from_attributes = True


class DodaResponseDTO(BaseModel):
    """DTO para responder con datos de un DODA"""

    id: int
    integration_number: Optional[str] = None
    doda_date: Optional[int] = None
    doda_time: Optional[int] = None
    dispatch_customs: Optional[str] = None
    customs_sections: Optional[str] = None
    patent: Optional[str] = None
    pedimentos: Optional[str] = None
    caat: Optional[str] = None
    transport_identification: Optional[str] = None
    fast_id: Optional[str] = None
    operation_type: Optional[str] = None
    selected: Optional[bool] = None
    user_selected: Optional[str] = None
    last_user: Optional[str] = None
    responsible: Optional[str] = None
    carrier: Optional[str] = None
    shipments: Optional[str] = None
    pedimento_type: Optional[str] = None
    original_chain: Optional[str] = None
    serial_number: Optional[str] = None
    electronic_signature: Optional[str] = None
    transaction_number: Optional[str] = None
    status: Optional[str] = None
    linq_sat_qr: Optional[str] = None
    sat_certificate: Optional[str] = None
    sat_digital_seal: Optional[str] = None
    xml_doda_sent_path: Optional[str] = None
    xml_doda_response_path: Optional[str] = None
    doda_report_pdf_path: Optional[str] = None
    doda_report_pdf_generated_at: Optional[datetime] = None
    doda_report_source_fingerprint: Optional[str] = None
    sat_original_chain: Optional[str] = None
    customs_clearance: Optional[int] = None
    unique_badge_number: Optional[str] = None
    containers: Optional[List[DodaContainerResponseDTO]] = None
    american_pedimentos: Optional[List[DodaAmericanPedimentoResponseDTO]] = None
    pedimentos_detail: Optional[List[DodaPedimentoResponseDTO]] = None

    class Config:
        from_attributes = True


class DodaDetailResponseDTO(BaseModel):
    """DTO detallado para responder con todos los datos de un DODA"""

    id: int
    integration_number: Optional[str] = None
    doda_date: Optional[int] = None
    doda_time: Optional[int] = None
    dispatch_customs: Optional[str] = None
    customs_sections: Optional[str] = None
    patent: Optional[str] = None
    pedimentos: Optional[str] = None
    caat: Optional[str] = None
    transport_identification: Optional[str] = None
    fast_id: Optional[str] = None
    operation_type: Optional[str] = None
    selected: Optional[bool] = None
    user_selected: Optional[str] = None
    last_user: Optional[str] = None
    responsible: Optional[str] = None
    carrier: Optional[str] = None
    shipments: Optional[str] = None
    pedimento_type: Optional[str] = None
    original_chain: Optional[str] = None
    serial_number: Optional[str] = None
    electronic_signature: Optional[str] = None
    transaction_number: Optional[str] = None
    status: Optional[str] = None
    linq_sat_qr: Optional[str] = None
    sat_certificate: Optional[str] = None
    sat_digital_seal: Optional[str] = None
    xml_doda_sent_path: Optional[str] = None
    xml_doda_response_path: Optional[str] = None
    doda_report_pdf_path: Optional[str] = None
    doda_report_pdf_generated_at: Optional[datetime] = None
    doda_report_source_fingerprint: Optional[str] = None
    sat_original_chain: Optional[str] = None
    customs_clearance: Optional[int] = None
    unique_badge_number: Optional[str] = None
    containers: List[DodaContainerResponseDTO] = []
    american_pedimentos: List[DodaAmericanPedimentoResponseDTO] = []
    pedimentos_detail: List[DodaPedimentoResponseDTO] = []

    class Config:
        from_attributes = True
