from datetime import date, datetime
from typing import List, Optional
from pydantic import BaseModel, ConfigDict


class SerialNumberCreate(BaseModel):
    """Payload for creating serial numbers (single or bulk)."""
    product_id: str
    serial: str
    status: Optional[str] = "available"
    purchase_id: Optional[str] = None
    inventory_transaction_id: Optional[str] = None
    warranty_started_at: Optional[date] = None
    warranty_expires_at: Optional[date] = None
    purchase_date: Optional[date] = None
    supplier_id: Optional[str] = None
    customer_id: Optional[str] = None
    batch_number: Optional[str] = None
    branch_id: Optional[str] = None
    location: Optional[str] = None
    notes: Optional[str] = None


class SerialNumberBulkCreate(BaseModel):
    """Bulk creation helper: share product/context, different serials."""
    product_id: str
    serials: List[str]
    status: Optional[str] = "available"
    purchase_id: Optional[str] = None
    inventory_transaction_id: Optional[str] = None
    warranty_started_at: Optional[date] = None
    warranty_expires_at: Optional[date] = None
    purchase_date: Optional[date] = None
    supplier_id: Optional[str] = None
    customer_id: Optional[str] = None
    batch_number: Optional[str] = None
    branch_id: Optional[str] = None
    location: Optional[str] = None
    notes: Optional[str] = None


class SerialNumberUpdate(BaseModel):
    """Update status and associations for a serial number."""
    status: Optional[str] = None
    customer_id: Optional[str] = None
    sale_id: Optional[str] = None
    invoice_id: Optional[str] = None
    receipt_id: Optional[str] = None
    warranty_started_at: Optional[date] = None
    warranty_expires_at: Optional[date] = None
    batch_number: Optional[str] = None
    branch_id: Optional[str] = None
    location: Optional[str] = None
    notes: Optional[str] = None
    active: Optional[bool] = None
    sold_at: Optional[date] = None
    returned_at: Optional[date] = None


class SerialNumberResponse(BaseModel):
    """API response representation for serial numbers."""
    id: str
    serial: str
    product_id: str
    product_name: Optional[str] = None
    product_sku: Optional[str] = None
    status: Optional[str] = None
    purchase_id: Optional[str] = None
    sale_id: Optional[str] = None
    invoice_id: Optional[str] = None
    receipt_id: Optional[str] = None
    customer_id: Optional[str] = None
    customer_name: Optional[str] = None
    supplier_id: Optional[str] = None
    batch_number: Optional[str] = None
    branch_id: Optional[str] = None
    branch_name: Optional[str] = None
    warranty_started_at: Optional[date] = None
    warranty_expires_at: Optional[date] = None
    purchase_date: Optional[date] = None
    sold_at: Optional[date] = None
    returned_at: Optional[date] = None
    location: Optional[str] = None
    notes: Optional[str] = None
    active: bool
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)
