"""Kitchen aggregates — Phase 12.6.

Kitchen display / ticket routing shell consuming POS tickets and QR
ordering sessions by reference only (source_type + source_id UUID).
"""
from __future__ import annotations

import uuid

from sqlalchemy import Index, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from app.core.database import Base
from app.models.base import (
    ActorAuditMixin,
    SoftDeleteMixin,
    TenantMixin,
    TimestampMixin,
    UUIDPrimaryKeyMixin,
)
from app.models.types import (
    GUID,
    KitchenItemStatus,
    KitchenTicketSource,
    KitchenTicketStatus,
    LifecycleStatus,
)


class KitchenStation(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    """A kitchen preparation station within a venue."""

    __tablename__ = "kitchen_stations"

    venue_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.ACTIVE, nullable=False
    )

    __table_args__ = (
        UniqueConstraint(
            "tenant_id", "venue_id", "code", name="uq_kitchen_stations_tenant_venue_code"
        ),
        Index("ix_kitchen_stations_venue", "tenant_id", "venue_id"),
        Index("ix_kitchen_stations_tenant_deleted", "tenant_id", "is_deleted"),
    )


class KitchenTicket(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    """A kitchen ticket routed from a POS ticket or QR ordering session (by reference only)."""

    __tablename__ = "kitchen_tickets"

    venue_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    station_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    source_type: Mapped[KitchenTicketSource] = mapped_column(
        default=KitchenTicketSource.POS_TICKET, nullable=False
    )
    source_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    status: Mapped[KitchenTicketStatus] = mapped_column(
        default=KitchenTicketStatus.QUEUED, nullable=False
    )
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    __table_args__ = (
        UniqueConstraint(
            "tenant_id", "venue_id", "code", name="uq_kitchen_tickets_tenant_venue_code"
        ),
        Index("ix_kitchen_tickets_venue", "tenant_id", "venue_id"),
        Index("ix_kitchen_tickets_station", "tenant_id", "station_id"),
        Index("ix_kitchen_tickets_source", "tenant_id", "source_type", "source_id"),
        Index("ix_kitchen_tickets_status", "tenant_id", "status"),
        Index("ix_kitchen_tickets_tenant_deleted", "tenant_id", "is_deleted"),
    )


class KitchenTicketItem(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    """A single menu-item line on a kitchen ticket."""

    __tablename__ = "kitchen_ticket_items"

    venue_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    kitchen_ticket_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    menu_item_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    quantity: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
    status: Mapped[KitchenItemStatus] = mapped_column(
        default=KitchenItemStatus.QUEUED, nullable=False
    )
    sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)

    __table_args__ = (
        Index("ix_kitchen_ticket_items_venue", "tenant_id", "venue_id"),
        Index("ix_kitchen_ticket_items_ticket", "tenant_id", "kitchen_ticket_id"),
        Index("ix_kitchen_ticket_items_status", "tenant_id", "status"),
        Index("ix_kitchen_ticket_items_tenant_deleted", "tenant_id", "is_deleted"),
    )
