from pydantic import BaseModel, ConfigDict, field_validator
from typing import Optional, List, Dict, Any
from datetime import date


class AphisCatalogDTO(BaseModel):
    id: Optional[int] = None
    
    # --- Pestaña 1: General ---
    program_code: Optional[str] = None
    processing_code: Optional[str] = None
    aphis_type: Optional[str] = None
    disclaimer: Optional[str] = None
    electronic_image: Optional[str] = None
    confidential: Optional[str] = None
    global_product_id: Optional[str] = None
    intended_use_code: Optional[str] = None
    intended_use_description: Optional[str] = None
    item_type: Optional[str] = None
    product_code: Optional[str] = None
    product_code_2: Optional[str] = None
    product_code_3: Optional[str] = None
    scientific_genus_name: Optional[str] = None
    scientific_species_name: Optional[str] = None
    scientific_sub_species_name: Optional[str] = None
    common_name_specific: Optional[str] = None
    common_name_general: Optional[str] = None
    signed_doc: Optional[str] = None
    signed_doc_date: Optional[date] = None
    signed_doc_id: Optional[str] = None
    invoice_number: Optional[str] = None
    quantity_1: Optional[str] = None
    quantity_2: Optional[str] = None
    quantity_3: Optional[str] = None
    inspection: Optional[str] = None
    inspection_date: Optional[date] = None
    inspection_loc_date: Optional[date] = None
    inspection_location: Optional[str] = None
    country_production: Optional[str] = None
    country_source: Optional[str] = None

    # --- Pestañas 2-7: Detalles (Listas de objetos) ---
    characteristics: Optional[List[Dict[str, Any]]] = []
    pitems: Optional[List[Dict[str, Any]]] = []
    lpcos: Optional[List[Dict[str, Any]]] = []
    entities: Optional[List[Dict[str, Any]]] = []
    containers: Optional[List[Dict[str, Any]]] = []
    routing: Optional[List[Dict[str, Any]]] = []

    model_config = ConfigDict(from_attributes=True)

    @field_validator("signed_doc_date", "inspection_date", "inspection_loc_date", mode="before")
    @classmethod
    def empty_to_none(cls, v):
        if v == "":
            return None
        return v
