from datetime import datetime
from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, ConfigDict, Field

from ..models.pedimentos import OperationType, PedimentoType
from .pedimento_config_additional import (
    PedimentoConfigAdditionalCreate,
    PedimentoConfigAdditionalResponse,
)
from .pedimento_config_calculations import (
    PedimentoConfigCalculationsCreate,
    PedimentoConfigCalculationsResponse,
)
from .pedimento_config_parameters import (
    PedimentoConfigParametersCreate,
    PedimentoConfigParametersResponse,
)
from .pedimento_config_surcharges import (
    PedimentoConfigSurchargesCreate,
    PedimentoConfigSurchargesResponse,
)
from .pedimento_config_update_rectification import (
    PedimentoConfigUpdateRectificationCreate,
    PedimentoConfigUpdateRectificationResponse,
)
from .pedimento_config_updates import (
    PedimentoConfigUpdatesCreate,
    PedimentoConfigUpdatesResponse,
)
from .pedimento_customs_offices import (
    PedimentoCustomsOfficesCreate,
    PedimentoCustomsOfficesResponse,
)
from .pedimento_dates import PedimentoDatesCreate, PedimentoDatesResponse
from .pedimento_decrementables import (
    PedimentoDecrementablesCreate,
    PedimentoDecrementablesResponse,
)
from .pedimento_incrementables import (
    PedimentoIncrementablesCreate,
    PedimentoIncrementablesResponse,
)
from .pedimento_indexes import PedimentoIndexesCreate, PedimentoIndexesResponse
from .pedimento_packages_transport import (
    PedimentoContainerCreate,
    PedimentoContainerResponse,
    PedimentoPackagesCreate,
    PedimentoPackagesResponse,
    PedimentoSealCreate,
    PedimentoSealResponse,
    PedimentoTransportCarrierCreate,
    PedimentoTransportCarrierResponse,
    PedimentoGuideCreate,
    PedimentoGuideResponse,
)
from .pedimento_contributions import (
    PedimentoContributionCreate,
    PedimentoContributionResponse,
)
from .pedimento_payments import PedimentoPaymentsCreate, PedimentoPaymentsResponse
from .pedimento_rectification_destination import (
    PedimentoRectificationDestinationCreate,
    PedimentoRectificationDestinationResponse,
)
from .pedimento_rectification_origin import (
    PedimentoRectificationOriginCreate,
    PedimentoRectificationOriginResponse,
)
from .pedimento_transport_means import (
    PedimentoTransportMeansCreate,
    PedimentoTransportMeansResponse,
)
from .pedimento_validation import PedimentoValidationCreate, PedimentoValidationResponse


class PedimentosBase(BaseModel):
    """Base schema for Pedimentos"""

    year: Optional[str] = Field(None, max_length=2, description="Year")
    customs_office: Optional[str] = Field(
        None, max_length=3, description="Customs office"
    )
    license: Optional[str] = Field(None, max_length=4, description="License")
    pedimento_number: Optional[str] = Field(
        None, max_length=7, description="Pedimento number"
    )
    client_id: Optional[int] = Field(None, description="Client ID")
    operation_type: Optional[OperationType] = Field(None, description="Operation type")
    pedimento_type: Optional[PedimentoType] = Field(None, description="Pedimento type")
    pedimento_code: str = Field(..., max_length=2, description="Pedimento key")
    regime: str = Field(..., max_length=3, description="Regime")
    status: Optional[str] = Field(None, max_length=30, description="Status")
    usd_value: Optional[Decimal] = Field(None, description="USD value")
    paid_price: Optional[Decimal] = Field(None, description="Paid price")
    gross_weight: Optional[Decimal] = Field(None, description="Gross weight")
    exchange_rate: Optional[Decimal] = Field(None, description="Exchange rate")
    observations: Optional[str] = Field(None, description="Observations")


class PedimentosCreate(PedimentosBase):
    """Schema for creating a new Pedimento"""

    # Override to make required fields non-optional
    year: str = Field(..., max_length=2, description="Year")
    customs_office: str = Field(..., max_length=3, description="Customs office")
    license: str = Field(..., max_length=4, description="License")
    pedimento_number: str = Field(..., max_length=7, description="Pedimento number")
    client_id: Optional[int] = Field(None, description="Client ID (opcional)")
    # operation_type, pedimento_type, status son opcionales - se pueden llenar después
    pedimento_code: str = Field(..., max_length=2, description="Pedimento key")
    regime: str = Field(..., max_length=3, description="Regime")

    pedimento_dates: Optional[PedimentoDatesCreate] = None
    pedimento_decrementables: Optional[PedimentoDecrementablesCreate] = None
    pedimento_incrementables: Optional[PedimentoIncrementablesCreate] = None
    pedimento_indexes: Optional[PedimentoIndexesCreate] = None
    pedimento_validation: Optional[PedimentoValidationCreate] = None
    pedimento_customs_offices: Optional[PedimentoCustomsOfficesCreate] = None
    pedimento_payments: Optional[PedimentoPaymentsCreate] = None
    pedimento_rectification_destination: Optional[
        PedimentoRectificationDestinationCreate
    ] = None
    pedimento_rectification_origin: Optional[PedimentoRectificationOriginCreate] = None
    pedimento_transport_means: Optional[PedimentoTransportMeansCreate] = None
    pedimento_config_additional: Optional[PedimentoConfigAdditionalCreate] = None
    pedimento_config_calculations: Optional[PedimentoConfigCalculationsCreate] = None
    pedimento_config_parameters: Optional[PedimentoConfigParametersCreate] = None
    pedimento_config_surcharges: Optional[PedimentoConfigSurchargesCreate] = None
    pedimento_config_update_rectification: Optional[
        PedimentoConfigUpdateRectificationCreate
    ] = None
    pedimento_config_updates: Optional[PedimentoConfigUpdatesCreate] = None
    pedimento_packages: Optional[PedimentoPackagesCreate] = None
    pedimento_transport_carriers: Optional[list[PedimentoTransportCarrierCreate]] = None
    pedimento_guides: Optional[list[PedimentoGuideCreate]] = None
    pedimento_contributions: Optional[list[PedimentoContributionCreate]] = None
    pedimento_seals: Optional[list[PedimentoSealCreate]] = None
    pedimento_containers: Optional[list[PedimentoContainerCreate]] = None


class PedimentosUpdate(BaseModel):
    """Schema for updating a Pedimento"""

    year: Optional[str] = Field(None, max_length=2)
    customs_office: Optional[str] = Field(None, max_length=3)
    license: Optional[str] = Field(None, max_length=4)
    pedimento_number: Optional[str] = Field(None, max_length=7)
    client_id: Optional[int] = None
    operation_type: Optional[str] = Field(None, max_length=3)
    pedimento_type: Optional[str] = Field(None, max_length=20)
    pedimento_code: Optional[str] = Field(None, max_length=2)
    regime: Optional[str] = Field(None, max_length=3)
    status: Optional[str] = Field(None, max_length=30)
    usd_value: Optional[Decimal] = None
    paid_price: Optional[Decimal] = None
    gross_weight: Optional[Decimal] = None
    exchange_rate: Optional[Decimal] = None
    observations: Optional[str] = None

    # Sub-resources
    pedimento_dates: Optional[PedimentoDatesCreate] = None
    pedimento_decrementables: Optional[PedimentoDecrementablesCreate] = None
    pedimento_incrementables: Optional[PedimentoIncrementablesCreate] = None
    pedimento_indexes: Optional[PedimentoIndexesCreate] = None
    pedimento_validation: Optional[PedimentoValidationCreate] = None
    pedimento_customs_offices: Optional[PedimentoCustomsOfficesCreate] = None
    pedimento_payments: Optional[PedimentoPaymentsCreate] = None
    pedimento_rectification_destination: Optional[
        PedimentoRectificationDestinationCreate
    ] = None
    pedimento_rectification_origin: Optional[PedimentoRectificationOriginCreate] = None
    pedimento_transport_means: Optional[PedimentoTransportMeansCreate] = None
    pedimento_config_additional: Optional[PedimentoConfigAdditionalCreate] = None
    pedimento_config_calculations: Optional[PedimentoConfigCalculationsCreate] = None
    pedimento_config_parameters: Optional[PedimentoConfigParametersCreate] = None
    pedimento_config_surcharges: Optional[PedimentoConfigSurchargesCreate] = None
    pedimento_config_update_rectification: Optional[
        PedimentoConfigUpdateRectificationCreate
    ] = None
    pedimento_config_updates: Optional[PedimentoConfigUpdatesCreate] = None
    pedimento_packages: Optional[PedimentoPackagesCreate] = None
    pedimento_transport_carriers: Optional[list[PedimentoTransportCarrierCreate]] = None
    pedimento_guides: Optional[list[PedimentoGuideCreate]] = None
    pedimento_contributions: Optional[list[PedimentoContributionCreate]] = None
    pedimento_seals: Optional[list[PedimentoSealCreate]] = None
    pedimento_containers: Optional[list[PedimentoContainerCreate]] = None


class PedimentosResponse(PedimentosBase):
    """Schema for Pedimento response"""

    id: int
    tenant_id: int
    created_at: datetime

    pedimento_dates: Optional[PedimentoDatesResponse] = None
    pedimento_decrementables: Optional[PedimentoDecrementablesResponse] = None
    pedimento_incrementables: Optional[PedimentoIncrementablesResponse] = None
    pedimento_indexes: Optional[PedimentoIndexesResponse] = None
    pedimento_validation: Optional[PedimentoValidationResponse] = None
    pedimento_customs_offices: Optional[PedimentoCustomsOfficesResponse] = None
    pedimento_payments: Optional[PedimentoPaymentsResponse] = None
    pedimento_rectification_destination: Optional[
        PedimentoRectificationDestinationResponse
    ] = None
    pedimento_rectification_origin: Optional[PedimentoRectificationOriginResponse] = (
        None
    )
    pedimento_transport_means: Optional[PedimentoTransportMeansResponse] = None
    pedimento_config_additional: Optional[PedimentoConfigAdditionalResponse] = None
    pedimento_config_calculations: Optional[PedimentoConfigCalculationsResponse] = None
    pedimento_config_parameters: Optional[PedimentoConfigParametersResponse] = None
    pedimento_config_surcharges: Optional[PedimentoConfigSurchargesResponse] = None
    pedimento_config_update_rectification: Optional[
        PedimentoConfigUpdateRectificationResponse
    ] = None
    pedimento_config_updates: Optional[PedimentoConfigUpdatesResponse] = None
    pedimento_packages: Optional[PedimentoPackagesResponse] = None
    pedimento_transport_carriers: Optional[list[PedimentoTransportCarrierResponse]] = None
    pedimento_guides: Optional[list[PedimentoGuideResponse]] = None
    pedimento_contributions: Optional[list[PedimentoContributionResponse]] = None
    pedimento_seals: Optional[list[PedimentoSealResponse]] = None
    pedimento_containers: Optional[list[PedimentoContainerResponse]] = None

    model_config = ConfigDict(from_attributes=True)
