from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field


class MixIngredientCreate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    raw_material_id: int
    quantity_kg: float = Field(gt=0)
    notes: str | None = Field(default=None, max_length=1000)


class MixIngredientUpdate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    quantity_kg: float | None = Field(default=None, gt=0)
    notes: str | None = Field(default=None, max_length=1000)


class MixIngredientRead(BaseModel):
    id: int
    raw_material_id: int
    raw_material_name: str
    quantity_kg: float
    cost_per_kg: float | None
    line_cost: float | None
    notes: str | None
    model_config = ConfigDict(from_attributes=True)


class MixCreate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    client_name: str = Field(min_length=1, max_length=255)
    name: str = Field(min_length=1, max_length=255)
    status: str = "draft"
    version: int = 1
    notes: str | None = Field(default=None, max_length=2000)
    ingredients: list[MixIngredientCreate]


class MixUpdate(BaseModel):
    model_config = ConfigDict(extra="forbid")
    client_name: str | None = Field(default=None, min_length=1, max_length=255)
    name: str | None = Field(default=None, min_length=1, max_length=255)
    status: str | None = None
    version: int | None = None
    notes: str | None = Field(default=None, max_length=2000)


class MixRead(BaseModel):
    id: int
    tenant_id: str
    client_name: str
    name: str
    status: str
    version: int
    notes: str | None
    created_at: datetime
    ingredients: list[MixIngredientRead]
    total_mix_kg: float
    total_mix_cost: float
    mix_cost_per_kg: float | None
    warnings: list[str]
    model_config = ConfigDict(from_attributes=True)
