"""QR Menu & QR Ordering DTOs — Phase 12.2."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import (
    CartStatus,
    LifecycleStatus,
    OrderingSessionStatus,
    QrSessionStatus,
    QrTargetType,
)
from app.schemas.common import ORMBase


class QrCodeCreate(BaseModel):
    venue_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    target_type: QrTargetType = QrTargetType.MENU
    menu_id: UUID | None = None
    dining_area_id: UUID | None = None
    table_id: UUID | None = None
    ordering_enabled: bool = False
    metadata_json: dict | None = None


class QrCodeRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    code: str
    token: str
    name: str
    description: str | None
    status: LifecycleStatus
    target_type: QrTargetType
    menu_id: UUID | None
    dining_area_id: UUID | None
    table_id: UUID | None
    ordering_enabled: bool
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class QrMenuSessionCreate(BaseModel):
    venue_id: UUID
    qr_code_id: UUID
    menu_id: UUID | None = None
    table_id: UUID | None = None
    locale: str | None = Field(default=None, max_length=16)
    guest_label: str | None = Field(default=None, max_length=255)
    metadata_json: dict | None = None


class QrMenuSessionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    qr_code_id: UUID
    menu_id: UUID | None
    table_id: UUID | None
    session_token: str
    status: QrSessionStatus
    locale: str | 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 QrOrderingSessionCreate(BaseModel):
    venue_id: UUID
    qr_code_id: UUID
    qr_menu_session_id: UUID | None = None
    menu_id: UUID | None = None
    table_id: UUID | None = None
    guest_label: str | None = Field(default=None, max_length=255)
    notes: str | None = None
    metadata_json: dict | None = None


class QrOrderingSessionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    qr_code_id: UUID
    qr_menu_session_id: UUID | None
    menu_id: UUID | None
    table_id: UUID | None
    session_token: str
    status: OrderingSessionStatus
    guest_label: str | None
    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 CartCreate(BaseModel):
    venue_id: UUID
    ordering_session_id: UUID
    currency_code: str = Field(default="IRR", max_length=3)
    notes: str | None = None
    metadata_json: dict | None = None


class CartRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    ordering_session_id: UUID
    status: CartStatus
    currency_code: str
    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 CartLineCreate(BaseModel):
    venue_id: UUID
    cart_id: UUID
    menu_item_id: UUID
    quantity: int = 1
    modifier_option_ids: list[UUID] | None = None
    line_notes: str | None = None
    sort_order: int = 0


class CartLineRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    cart_id: UUID
    menu_item_id: UUID
    quantity: int
    unit_price: int
    currency_code: str
    modifier_option_ids: list | None
    line_notes: str | None
    sort_order: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime
