from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime

# ------------------- INVENTORY -------------------
class InventoryBase(BaseModel):
    product_id: str
    warehouse_id: str
    q_on_hand: int
    q_reserved: int
    last_update: Optional[datetime] = None
    condition: str
    sku: Optional[str] = None
    q_in_transit: Optional[int] = 0
    reorder_point: Optional[int] = None
    location: Optional[str] = None
    safety_stock: Optional[int] = None


class InventoryCreate(InventoryBase):
    pass

class Inventory(InventoryBase):
    inventory_id: str
    class Config:
        from_attributes = True
             
        
# ------------------- ORDER ITEM -------------------
class OrderItemBase(BaseModel):
    order_id: str
    product_id: Optional[str] = None
    sku: str
    qty: int
    price: float
    order_date: Optional[datetime] = None

class OrderItem(OrderItemBase):
    order_item_id: int
    created_at: Optional[datetime]

    class Config:
        from_attributes = True  


# ------------------- PRODUCT -------------------
class ProductBase(BaseModel):
    product_name: str
    sku: str
    category: Optional[str] = None
    base_price: float
    model_number: Optional[str] = None
    description: Optional[str] = None
    weight: Optional[float] = None
    dimensions: Optional[str] = None
    brand: Optional[str] = None
    status: Optional[str] = None
    

class ProductCreate(ProductBase):
    pass

class Product(ProductBase):
    product_id: str
    class Config:
        from_attributes = True


# ------------------- WAREHOUSE -------------------
class WarehouseBase(BaseModel):
    warehouse_id: str
    warehouse_name: str
    city: Optional[str] = None
    state: Optional[str] = None
    country: Optional[str] = None
    latitude: Optional[float] = None
    longitude: Optional[float] = None
    capacity: Optional[int] = None
    operating_hours: Optional[str] = None
    manager: Optional[str] = None

class WarehouseCreate(WarehouseBase):
    pass

class Warehouse(WarehouseBase):
    warehouse_id: str
    class Config:
        from_attributes = True


# ------------------- ORDER -------------------
class OrderBase(BaseModel):
    order_id: str
    order_date: Optional[datetime] = None
    customer_id: str
    customer_name: str
    customer_location: Optional[str] = None
    order_total: float
    items: List[OrderItem] = Field(default_factory=list)
    assigned_warehouse_id: Optional[str] = None
    status: str
    payment_method: Optional[str] = None
    shipping_address: Optional[str] = None
    estimated_delivery: Optional[datetime] = None


class OrderCreate(OrderBase):
    pass

class Order(OrderBase):
    class Config:
        from_attributes = True


# ------------------- SPARE PART -------------------
class SparePartBase(BaseModel):
    part_id: str
    main_product_id: str
    main_product_model: str
    part_name: str
    part_sku: str
    part_price: float
    availability: str
    estimate_shipment_days: int
    supplier: str
    part_number: str
    compatibility_notes: Optional[str] = None

class SparePartCreate(SparePartBase):
    pass

class SparePart(SparePartBase):
    part_id: str
    class Config:
        from_attributes = True


    
# ------------------- STOCK LEDGER -------------------   
# class StockLedgerBase(BaseModel):
#     product_id: int
#     warehouse_id: int
#     event_type: str
#     quantity: int
#     timestamp: Optional[str] = None

# class StockLedgerCreate(StockLedgerBase):
#     pass

# class StockLedger(StockLedgerBase):
#     ledger_id: int
#     class Config:
#         orm_mode = True
        
# # ------------------- STOCK RESERVATION -------------------        
# class StockReservationBase(BaseModel):
#     product_id: int
#     warehouse_id: int
#     reserved_quantity: int
#     status: str

# class StockReservationCreate(StockReservationBase):
#     pass

# class StockReservation(StockReservationBase):
#     reservation_id: int
#     created_at: Optional[str] = None
#     class Config:
#         orm_mode = True


