from datetime import datetime
from decimal import Decimal

from pydantic import BaseModel, ConfigDict, EmailStr, Field


class SupplierBase(BaseModel):
    # Datos generales
    name: str = Field(..., min_length=1, max_length=255)
    trade_name: str | None = Field(None, max_length=255)
    rfc: str | None = Field(None, max_length=13)
    curp: str | None = Field(None, max_length=18)
    person_type: str | None = Field(None, max_length=10)
    status: str = Field("active", max_length=20)
    classifications: list[str] = Field(default_factory=list)
    classification_other: str | None = Field(None, max_length=120)
    # Comercial
    services_offered: str | None = None
    coverage: str | None = Field(None, max_length=20)
    countries: list[str] = Field(default_factory=list)
    ports: list[str] = Field(default_factory=list)
    airports: list[str] = Field(default_factory=list)
    customs: list[str] = Field(default_factory=list)
    business_hours: str | None = Field(None, max_length=255)
    quote_currency: str | None = Field(None, max_length=3)
    avg_response_time: str | None = Field(None, max_length=120)
    commercial_notes: str | None = None
    email: EmailStr | None = None
    phone: str | None = Field(None, max_length=40)
    website: str | None = Field(None, max_length=255)
    # Fiscal
    tax_regime: str | None = Field(None, max_length=120)
    payment_method: str | None = Field(None, max_length=60)
    payment_form: str | None = Field(None, max_length=60)
    credit_limit: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
    credit_days: int | None = Field(None, ge=0)
    commercial_terms: str | None = None
    # Observaciones
    notes: str | None = None
    internal_notes: str | None = None
    owner_user_id: str | None = Field(None, max_length=64)


class SupplierCreate(SupplierBase):
    pass


class SupplierUpdate(BaseModel):
    name: str | None = Field(None, min_length=1, max_length=255)
    trade_name: str | None = Field(None, max_length=255)
    rfc: str | None = Field(None, max_length=13)
    curp: str | None = Field(None, max_length=18)
    person_type: str | None = Field(None, max_length=10)
    status: str | None = Field(None, max_length=20)
    classifications: list[str] | None = None
    classification_other: str | None = Field(None, max_length=120)
    services_offered: str | None = None
    coverage: str | None = Field(None, max_length=20)
    countries: list[str] | None = None
    ports: list[str] | None = None
    airports: list[str] | None = None
    customs: list[str] | None = None
    business_hours: str | None = Field(None, max_length=255)
    quote_currency: str | None = Field(None, max_length=3)
    avg_response_time: str | None = Field(None, max_length=120)
    commercial_notes: str | None = None
    email: EmailStr | None = None
    phone: str | None = Field(None, max_length=40)
    website: str | None = Field(None, max_length=255)
    tax_regime: str | None = Field(None, max_length=120)
    payment_method: str | None = Field(None, max_length=60)
    payment_form: str | None = Field(None, max_length=60)
    credit_limit: Decimal | None = Field(None, ge=0, max_digits=14, decimal_places=2)
    credit_days: int | None = Field(None, ge=0)
    commercial_terms: str | None = None
    notes: str | None = None
    internal_notes: str | None = None
    owner_user_id: str | None = Field(None, max_length=64)


class SupplierResponse(SupplierBase):
    model_config = ConfigDict(from_attributes=True)

    id: int
    tenant_id: int
    company_id: int
    created_by: str | None = None
    updated_by: str | None = None
    created_at: datetime
    updated_at: datetime
