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

# --- Users ---
class UserBase(BaseModel):
    username: str
    role: str = "user"
    origin: str = "local"

class UserCreate(UserBase):
    password: Optional[str] = None

class User(UserBase):
    id: int

    class Config:
        from_attributes = True

class UserUpdate(BaseModel):
    username: Optional[str] = None
    password: Optional[str] = None
    role: Optional[str] = None

class UserLogin(BaseModel):
    username: str
    password: str

class UserPasswordUpdate(BaseModel):
    old_password: Optional[str] = None
    new_password: str

class TokenResponse(BaseModel):
    access_token: str
    token_type: str = "bearer"
    user_id: int
    username: str
    role: str

# --- Categories ---
class CategoryBase(BaseModel):
    name: str
    description: Optional[str] = None

class CategoryCreate(CategoryBase):
    pass

class Category(CategoryBase):
    id: int

    class Config:
        from_attributes = True

# --- Items ---
class ItemBase(BaseModel):
    name: str
    category: str
    category_id: Optional[int] = None
    type: Optional[str] = None
    barcode: str
    part_number: Optional[str] = None
    color: Optional[str] = None
    specs: Optional[str] = None
    quantity: float = 0.0
    min_quantity: float = 1.0
    image_url: Optional[str] = None
    box_label: Optional[str] = None
    labels_data: Optional[str] = None

class ItemCreate(ItemBase):
    pass

class Item(ItemBase):
    id: int

    class Config:
        from_attributes = True

# --- Operations (Check-in/Check-out Validation) ---
class OperationCreate(BaseModel):
    barcode: str
    quantity: float
    user_id: int

class BulkOperationCreate(BaseModel):
    user_id: int
    items: List[OperationCreate]

class TrashOperationCreate(BaseModel):
    barcode: str
    quantity: float
    user_id: int
    reason: Optional[str] = "unspecified"

# --- Sync ---
class SyncOperation(BaseModel):
    type: str  # 'CHECK_IN', 'CHECK_OUT'
    barcode: str
    quantity: float
    uuid: Optional[str] = None
    timestamp: datetime

class SyncPayload(BaseModel):
    user_id: int
    operations: List[SyncOperation]

# --- Audit Logs ---
class AuditLogResponse(BaseModel):
    id: int
    timestamp: datetime
    user_id: int
    username: Optional[str] = None
    action: str
    target_item_id: Optional[int]
    target_item_name: Optional[str] = None
    target_item_pn: Optional[str] = None
    target_item_barcode: Optional[str] = None
    target_snapshot: Optional[str] = None
    quantity_change: Optional[float]
    details: Optional[str] = None

    class Config:
        from_attributes = True

# --- System Settings ---
class SystemSettingBase(BaseModel):
    key: str
    value: str

class SystemSetting(SystemSettingBase):
    class Config:
        from_attributes = True

# --- Database Management ---
class BackupInfo(BaseModel):
    filename: str
    size_bytes: int
    created_at: datetime

class DatabaseStats(BaseModel):
    backup_count: int
    total_size_bytes: int

class DbSettingsUpdate(BaseModel):
    retention_count: int
    schedule_hour: int
    schedule_freq_days: int
