from pydantic import BaseModel
from datetime import datetime
from typing import Optional, List
from schemas.base import UTCDatetime


class UserBase(BaseModel):
    username: str
    role: str
    is_active: bool = True
    full_name: Optional[str] = None
    nickname: Optional[str] = None
    mobile_phone: Optional[str] = None
    note: Optional[str] = None
    avatar_url: Optional[str] = None
    email: Optional[str] = None
    # Phase 2B — payroll
    hourly_rate: Optional[float] = None
    # Access permissions
    perm_access_dashboard: bool = False
    perm_access_waiter_app: bool = True
    perm_access_kds: bool = False
    # Order action permissions
    perm_cancel_orders: bool = False
    perm_apply_discounts: bool = False
    perm_modify_prices: bool = False
    perm_open_orders: bool = True
    perm_close_orders: bool = True
    # Management permissions
    perm_view_reports: bool = False
    perm_manage_staff: bool = False
    perm_manage_tables: bool = False
    perm_manage_menu: bool = False
    perm_manage_settings: bool = False


class UserCreate(UserBase):
    pin: str


class UserUpdate(BaseModel):
    username: Optional[str] = None
    role: Optional[str] = None
    is_active: Optional[bool] = None
    full_name: Optional[str] = None
    nickname: Optional[str] = None
    mobile_phone: Optional[str] = None
    email: Optional[str] = None
    note: Optional[str] = None
    # Phase 2B — payroll
    hourly_rate: Optional[float] = None
    # Access permissions
    perm_access_dashboard: Optional[bool] = None
    perm_access_waiter_app: Optional[bool] = None
    perm_access_kds: Optional[bool] = None
    # Order action permissions
    perm_cancel_orders: Optional[bool] = None
    perm_apply_discounts: Optional[bool] = None
    perm_modify_prices: Optional[bool] = None
    perm_open_orders: Optional[bool] = None
    perm_close_orders: Optional[bool] = None
    # Management permissions
    perm_view_reports: Optional[bool] = None
    perm_manage_staff: Optional[bool] = None
    perm_manage_tables: Optional[bool] = None
    perm_manage_menu: Optional[bool] = None
    perm_manage_settings: Optional[bool] = None


class WaiterZoneOut(BaseModel):
    id: int
    waiter_id: int
    group_id: Optional[int] = None   # None = all zones

    model_config = {"from_attributes": True}


class UserOut(UserBase):
    id: int
    created_at: UTCDatetime
    zone_assignments: List[WaiterZoneOut] = []
    waiter_settings: Optional[str] = None  # JSON blob — per-waiter settings + favorites

    model_config = {"from_attributes": True}


class SetZonesRequest(BaseModel):
    """Replace all zone assignments for a waiter in one call.
    group_ids=[] means remove all (sees nothing).
    group_ids=[null] or all_zones=True means the wildcard 'all zones' sentinel."""
    group_ids: Optional[List[Optional[int]]] = None  # list of group ids; None in list = all-zones sentinel
    all_zones: bool = False  # convenience flag: if True, set a single NULL-group_id row


class AssistantAssignmentOut(BaseModel):
    id: int
    primary_waiter_id: int
    assistant_waiter_id: int
    assigned_at: UTCDatetime

    model_config = {"from_attributes": True}


class PermissionToggle(BaseModel):
    """Patch a single permission flag for a staff member."""
    permission: str
    value: bool
