from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field


class ProductCreate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    client_name: str = Field(min_length=1, max_length=255)
    item_id: str | None = Field(default=None, max_length=128)
    name: str = Field(min_length=1, max_length=255)
    mix_id: int
    sale_type: str = "standard"
    own_bag: bool = False
    visible: bool = True
    unit_of_measure: str = Field(default="20kg bag", min_length=1, max_length=64)
    items_per_pallet: int = Field(default=50, gt=0)
    bagging_process: str | None = Field(default=None, max_length=128)
    distributor_margin: float | None = Field(default=None, gt=0, lt=1)
    wholesale_margin: float | None = Field(default=None, gt=0, lt=1)
    notes: str | None = Field(default=None, max_length=2000)


class ProductUpdate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    client_name: str | None = Field(default=None, min_length=1, max_length=255)
    item_id: str | None = Field(default=None, max_length=128)
    name: str | None = Field(default=None, min_length=1, max_length=255)
    mix_id: int | None = None
    sale_type: str | None = None
    own_bag: bool | None = None
    visible: bool | None = None
    unit_of_measure: str | None = Field(default=None, min_length=1, max_length=64)
    items_per_pallet: int | None = Field(default=None, gt=0)
    bagging_process: str | None = Field(default=None, max_length=128)
    distributor_margin: float | None = Field(default=None, gt=0, lt=1)
    wholesale_margin: float | None = Field(default=None, gt=0, lt=1)
    notes: str | None = Field(default=None, max_length=2000)


class ProductRead(BaseModel):
    id: int
    tenant_id: str
    client_name: str
    item_id: str | None
    name: str
    mix_id: int
    mix_name: str
    sale_type: str
    own_bag: bool
    visible: bool
    unit_of_measure: str
    items_per_pallet: int
    bagging_process: str | None
    distributor_margin: float | None
    wholesale_margin: float | None
    notes: str | None
    created_at: datetime
    model_config = ConfigDict(from_attributes=True)


class ProductCostBreakdown(BaseModel):
    product_id: int
    product_name: str
    client_name: str
    mix_name: str
    cleaned_product_cost: float
    grading_cost: float
    bagging_cost: float
    cracking_cost: float
    bag_cost: float
    freight_cost: float
    finished_product_delivered: float
    distributor_price: float | None
    wholesale_price: float | None
    warnings: list[str]
    inputs: dict[str, object]
