"""Digital Menu Catalog DTOs — Phase 12.1."""
from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import (
    AvailabilityScope,
    LifecycleStatus,
    LocalizationTarget,
    MediaKind,
    ModifierSelectionMode,
)
from app.schemas.common import ORMBase


class AllergenCreate(BaseModel):
    venue_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    icon_ref: str | None = Field(default=None, max_length=255)
    sort_order: int = 0


class AllergenRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    code: str
    name: str
    description: str | None
    status: LifecycleStatus
    icon_ref: str | None
    sort_order: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class ModifierGroupCreate(BaseModel):
    venue_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    selection_mode: ModifierSelectionMode = ModifierSelectionMode.SINGLE
    min_select: int = 0
    max_select: int | None = None
    is_required: bool = False
    sort_order: int = 0


class ModifierGroupRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    code: str
    name: str
    description: str | None
    status: LifecycleStatus
    selection_mode: ModifierSelectionMode
    min_select: int
    max_select: int | None
    is_required: bool
    sort_order: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class ModifierOptionCreate(BaseModel):
    venue_id: UUID
    modifier_group_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    price_delta: int = 0
    currency_code: str = Field(default="IRR", max_length=3)
    is_default: bool = False
    sort_order: int = 0


class ModifierOptionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    modifier_group_id: UUID
    code: str
    name: str
    description: str | None
    status: LifecycleStatus
    price_delta: int
    currency_code: str
    is_default: bool
    sort_order: int
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class MenuItemModifierLinkCreate(BaseModel):
    venue_id: UUID
    menu_item_id: UUID
    modifier_group_id: UUID
    sort_order: int = 0
    is_required_override: bool | None = None


class MenuItemModifierLinkRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    menu_item_id: UUID
    modifier_group_id: UUID
    sort_order: int
    is_required_override: bool | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class MenuItemAllergenLinkCreate(BaseModel):
    venue_id: UUID
    menu_item_id: UUID
    allergen_id: UUID
    is_contains: bool = True
    is_may_contain: bool = False


class MenuItemAllergenLinkRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    menu_item_id: UUID
    allergen_id: UUID
    is_contains: bool
    is_may_contain: bool
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class AvailabilityWindowCreate(BaseModel):
    venue_id: UUID
    scope: AvailabilityScope
    target_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    weekday: int | None = Field(default=None, ge=0, le=6)
    start_time: str = Field(max_length=8)
    end_time: str = Field(max_length=8)
    timezone: str = Field(default="Asia/Tehran", max_length=64)
    metadata_json: dict | None = None


class AvailabilityWindowRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    scope: AvailabilityScope
    target_id: UUID
    code: str
    name: str
    status: LifecycleStatus
    weekday: int | None
    start_time: str
    end_time: str
    timezone: str
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class MenuMediaRefCreate(BaseModel):
    venue_id: UUID
    menu_id: UUID | None = None
    menu_item_id: UUID | None = None
    kind: MediaKind = MediaKind.IMAGE
    file_ref: str = Field(max_length=255)
    alt_text: str | None = Field(default=None, max_length=255)
    sort_order: int = 0
    status: LifecycleStatus = LifecycleStatus.ACTIVE
    metadata_json: dict | None = None


class MenuMediaRefRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    menu_id: UUID | None
    menu_item_id: UUID | None
    kind: MediaKind
    file_ref: str
    alt_text: str | None
    sort_order: int
    status: LifecycleStatus
    metadata_json: dict | None
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime


class MenuLocalizationUpsert(BaseModel):
    venue_id: UUID
    target_type: LocalizationTarget
    target_id: UUID
    locale: str = Field(max_length=16)
    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    status: LifecycleStatus = LifecycleStatus.ACTIVE


class MenuLocalizationRead(ORMBase):
    id: UUID
    tenant_id: UUID
    venue_id: UUID
    target_type: LocalizationTarget
    target_id: UUID
    locale: str
    name: str | None
    description: str | None
    status: LifecycleStatus
    is_deleted: bool
    created_by: str | None
    updated_by: str | None
    created_at: datetime
    updated_at: datetime
