from datetime import datetime from typing import List, Optional from pydantic import BaseModel, Field, validator class ItemBase(BaseModel): """Base model with common attributes for an inventory item.""" name: str = Field(..., description="Name of the inventory item", min_length=1, max_length=255) description: Optional[str] = Field(None, description="Detailed description of the item") quantity: int = Field(..., description="Current quantity in stock", ge=0) unit_price: Optional[float] = Field(None, description="Price per unit", ge=0) sku: Optional[str] = Field(None, description="Stock Keeping Unit - unique identifier", max_length=50) category: Optional[str] = Field(None, description="Category the item belongs to", max_length=100) location: Optional[str] = Field(None, description="Storage location of the item", max_length=100) class ItemCreate(ItemBase): """Schema for creating a new inventory item.""" pass class ItemUpdate(BaseModel): """Schema for updating an existing inventory item - all fields are optional.""" name: Optional[str] = Field(None, description="Name of the inventory item", min_length=1, max_length=255) description: Optional[str] = Field(None, description="Detailed description of the item") quantity: Optional[int] = Field(None, description="Current quantity in stock", ge=0) unit_price: Optional[float] = Field(None, description="Price per unit", ge=0) sku: Optional[str] = Field(None, description="Stock Keeping Unit - unique identifier", max_length=50) category: Optional[str] = Field(None, description="Category the item belongs to", max_length=100) location: Optional[str] = Field(None, description="Storage location of the item", max_length=100) @validator("*", pre=True) def check_none_fields(cls, v): """Make sure at least one field is not None to perform an update""" return v class Config: extra = "forbid" class ItemInDB(ItemBase): """Schema for an inventory item as stored in the database.""" id: int created_at: datetime updated_at: datetime class Config: from_attributes = True class Item(ItemInDB): """Schema for returning an inventory item.""" pass class ItemList(BaseModel): """Schema for returning a list of inventory items.""" items: List[Item] total: int