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
    avatar_url: Optional[str] = None


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


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] = []

    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}
