from typing import Optional
from decimal import Decimal

from pydantic import BaseModel, Field, ConfigDict

# EquivalencyItem DTOs (pool global independiente)


class EquivalencyItemBase(BaseModel):
    original_field: str = Field(..., max_length=100, description="Campo original (from)")
    external_field: str = Field(..., max_length=100, description="Campo externo (to)")
    conversion_factor: Optional[Decimal] = Field(None, description="Factor de conversión")


class EquivalencyItemCreate(EquivalencyItemBase):
    conversion_factor: Optional[Decimal] = Field(None, description="Factor de conversión")


class EquivalencyItemUpdate(BaseModel):
    original_field: Optional[str] = Field(None, max_length=100)
    external_field: Optional[str] = Field(None, max_length=100)
    conversion_factor: Optional[Decimal] = None


class EquivalencyItemResponse(EquivalencyItemBase):
    id: int
    tenant_id: int
    company_id: int

    model_config = ConfigDict(from_attributes=True)


# Equivalency DTOs (catálogo — referencia un item del pool)


class EquivalencyBase(BaseModel):
    identifier: str = Field(..., max_length=10, description="Identificador único")
    description: Optional[str] = Field(None, max_length=200, description="Descripción (opcional)")
    item_id: Optional[int] = Field(None, description="ID del EquivalencyItem seleccionado")


class EquivalencyCreate(EquivalencyBase):
    pass


class EquivalencyUpdate(BaseModel):
    identifier: Optional[str] = Field(None, max_length=10)
    description: Optional[str] = Field(None, max_length=200)
    item_id: Optional[int] = Field(None, description="Re-asociar a otro EquivalencyItem")


class EquivalencyResponse(EquivalencyBase):
    id: int
    tenant_id: int
    company_id: int
    item: Optional[EquivalencyItemResponse] = None

    model_config = ConfigDict(from_attributes=True)
