"""
DTOs (Data Transfer Objects) para relación entre partes y países - Anexo 24
"""

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

from pydantic import BaseModel, Field


class PartCountryCreateDTO(BaseModel):
    """DTO para crear una relación entre parte y país"""

    part_id: int = Field(..., description="Part ID")
    country_code: str = Field(..., max_length=3, description="Country code (ISO 3166-1 alpha-3)")
    fraction: Optional[str] = Field(None, max_length=20, description="Tariff fraction")
    preference: str = Field(default="GENERAL", description="Trade preference: GENERAL, PROSEC, ALADI, TLCS")
    has_certificate: bool = Field(default=False, description="Has certificate of origin")
    certificate_number: Optional[str] = Field(None, max_length=50, description="Certificate of origin number")
    end_date: Optional[datetime] = Field(None, description="Certificate expiration date")
    previous_fractions_7m: bool = Field(default=False, description="Has fractions older than 7 months")
    omission_import: bool = Field(default=False, description="Import omission flag")
    omission_export: bool = Field(default=False, description="Export omission flag")
    import_percentage: Optional[Decimal] = Field(None, description="Import tariff percentage")
    export_percentage: Optional[Decimal] = Field(None, description="Export tariff percentage")
    sector: Optional[str] = Field(None, max_length=10, description="Sector reference")

    class Config:
        from_attributes = True


class PartCountryUpdateDTO(BaseModel):
    """DTO para actualizar una relación entre parte y país"""

    country_code: Optional[str] = Field(None, max_length=3, description="Country code")
    fraction: Optional[str] = Field(None, max_length=20, description="Tariff fraction")
    preference: Optional[str] = Field(None, description="Trade preference")
    has_certificate: Optional[bool] = Field(None, description="Has certificate of origin")
    certificate_number: Optional[str] = Field(None, max_length=50, description="Certificate number")
    end_date: Optional[datetime] = Field(None, description="Certificate expiration date")
    previous_fractions_7m: Optional[bool] = Field(None, description="Previous fractions flag")
    omission_import: Optional[bool] = Field(None, description="Import omission flag")
    omission_export: Optional[bool] = Field(None, description="Export omission flag")
    import_percentage: Optional[Decimal] = Field(None, description="Import percentage")
    export_percentage: Optional[Decimal] = Field(None, description="Export percentage")
    sector: Optional[str] = Field(None, max_length=10, description="Sector reference")

    class Config:
        from_attributes = True


class PartCountryResponseDTO(BaseModel):
    """DTO para responder con datos de relación entre parte y país"""

    id: int
    part_id: int
    country_code: str
    fraction: Optional[str] = None
    preference: str
    has_certificate: bool
    certificate_number: Optional[str] = None
    end_date: Optional[datetime] = None
    previous_fractions_7m: bool
    omission_import: bool
    omission_export: bool
    import_percentage: Optional[Decimal] = None
    export_percentage: Optional[Decimal] = None
    sector: Optional[str] = None

    class Config:
        from_attributes = True


class PartCountriesBulkResponseDTO(BaseModel):
    """DTO para responder con lista de relaciones entre partes y países"""

    data: list[PartCountryResponseDTO]
    total: int
    skip: int
    limit: int
