"""Table Service & Reservations DTOs — Phase 12.3."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import (
    ReservationStatus,
    ServiceRequestKind,
    ServiceRequestStatus,
    TableAssignmentStatus,
    WaitlistStatus,
)
from app.schemas.common import ORMBase


class ReservationCreate(BaseModel):
    venue_id: UUID
    branch_id: UUID | None = None
    dining_area_id: UUID | None = None
    table_id: UUID | None = None
    code: str = Field(max_length=50)
    guest_name: str = Field(max_length=255)
    guest_phone: str | None = Field(default=None, max_length=32)
    party_size: int = 1
    reserved_from: datetime
    reserved_to: datetime
    notes: str | None = None
    metadata_json: dict | None = None


class ReservationStatusUpdate(BaseModel):
    status: ReservationStatus


class ReservationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    branch_id: UUID | None
    dining_area_id: UUID | None
    table_id: UUID | None
    code: str
    guest_name: str
    guest_phone: str | None
    party_size: int
    status: ReservationStatus
    reserved_from: datetime
    reserved_to: datetime
    notes: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class TableAssignmentCreate(BaseModel):
    venue_id: UUID
    table_id: UUID
    reservation_id: UUID | None = None
    ordering_session_id: UUID | None = None
    guest_label: str | None = Field(default=None, max_length=255)
    metadata_json: dict | None = None


class TableAssignmentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    table_id: UUID
    reservation_id: UUID | None
    ordering_session_id: UUID | None
    status: TableAssignmentStatus
    assigned_at: datetime | None
    released_at: datetime | None
    guest_label: str | None
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class ServiceRequestCreate(BaseModel):
    venue_id: UUID
    table_id: UUID
    table_assignment_id: UUID | None = None
    code: str = Field(max_length=50)
    kind: ServiceRequestKind = ServiceRequestKind.OTHER
    notes: str | None = None


class ServiceRequestStatusUpdate(BaseModel):
    status: ServiceRequestStatus


class ServiceRequestRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    table_id: UUID
    table_assignment_id: UUID | None
    code: str
    kind: ServiceRequestKind
    status: ServiceRequestStatus
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class WaitlistEntryCreate(BaseModel):
    venue_id: UUID
    code: str = Field(max_length=50)
    guest_name: str = Field(max_length=255)
    party_size: int = 1
    phone: str | None = Field(default=None, max_length=32)
    notes: str | None = None


class WaitlistEntryStatusUpdate(BaseModel):
    status: WaitlistStatus


class WaitlistEntryRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    code: str
    guest_name: str
    party_size: int
    status: WaitlistStatus
    phone: str | None
    notes: str | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime
