from decimal import Decimal
from typing import Optional
from pydantic import BaseModel, Field, ConfigDict

# ============================================================================
# LINE CUSTOMS SCHEMAS
# ============================================================================

class LineCustomBase(BaseModel):
    """Base schema for line customs"""
    # Tariff/Customs Classifications
    fraction: Optional[str] = Field(None, max_length=10, description="Fraction (FRACCION/FRACCIONIMPO/FRACCIONEXPO)")
    fraction_type: Optional[str] = Field(None, max_length=7, description="Fraction type (TIPOFRACCION/TIPOFRACCIONIMPO/TIPOFRACCIONEXPO)")
    american_fraction: Optional[str] = Field(None, max_length=16, description="American fraction (FRACCIONAMERICANA)")
    alternate_fraction: Optional[str] = Field(None, max_length=10, description="Alternate fraction (FRACCIONALTERNA)")
    reference_fraction: Optional[str] = Field(None, max_length=10, description="Reference fraction (FRACCIONREFERENCIA)")
    octave_fraction: Optional[str] = Field(None, max_length=10, description="Octave fraction (FRACCIONROCTAVA)")
    tlcan_fraction: Optional[str] = Field(None, max_length=13, description="TLCAN fraction (FRACCIONTLCAN)")
    extra_american_fraction: Optional[str] = Field(None, max_length=16, description="Extra American fraction (FRACAMESELEXTRA)")
    garment_fraction: Optional[str] = Field(None, max_length=19, description="Garment fraction (FRACCIONDELAPRENDA/FRACCIONDELAPARTIDA)")
    
    # Ad Valorem
    advalorem: Optional[str] = Field(None, max_length=10, description="Ad valorem (ADVIMPO/ADVEXPO)")
    advalorem_numeric: Optional[Decimal] = Field(None, description="Ad valorem numeric (ADVIMPONUM/ADVEXPONUM)")
    advalorem_american: Optional[Decimal] = Field(None, description="Ad valorem American (ADVAME)")
    advalorem_tlcan: Optional[Decimal] = Field(None, description="Ad valorem TLCAN (ADVTLCAN)")
    
    # Rates
    rate: Optional[str] = Field(None, max_length=10, description="Rate (TASAIM/TASAEX)")
    depreciation_rate: Optional[Decimal] = Field(None, description="Depreciation rate (TASADEPRECIA)")
    
    # Origin/Destination
    origin_country: Optional[str] = Field(None, max_length=3, description="Origin country (PAISORIGEN)")
    destination_country: Optional[str] = Field(None, max_length=3, description="Destination country (PAISDESTINO)")
    optional_country: Optional[str] = Field(None, max_length=3, description="Optional country (PAISOPCIONAL)")
    origin_procedure: Optional[str] = Field(None, max_length=3, description="Origin procedure (PROCEDENCIA)")
    scrap_procedure: Optional[str] = Field(None, max_length=3, description="Scrap procedure (PROCSCRAP)")
    
    # Sector
    sector: Optional[str] = Field(None, max_length=8, description="Sector (SECTOR)")


class LineCustomCreate(LineCustomBase):
    """Schema for creating line customs"""
    pass


class LineCustomUpdate(LineCustomBase):
    """Schema for updating line customs"""
    pass


class LineCustomResponse(LineCustomBase):
    """Schema for line customs response"""
    id: int
    item_line_id: int

    model_config = ConfigDict(from_attributes=True)
