import uuid
from datetime import datetime
from decimal import Decimal
from enum import Enum

from sqlalchemy import (
    Boolean,
    DateTime,
    ForeignKey,
    Integer,
    Numeric,
    String,
    Uuid,
    func,
)
from sqlalchemy.orm import Mapped, mapped_column

from app.db.session import Base


class OptionSelectionType(str, Enum):
    SINGLE = "single"  # radio-button style: at most one option from the group
    MULTIPLE = "multiple"  # checkbox style: any number of options


class MenuItemOptionGroup(Base):
    """A branch-manager-configured group of choices on a menu item
    (e.g. "Spice Level", "Add-ons", "Size"). is_required means the
    customer must pick at least one option from this group before the
    item can be added to a cart; selection_type further constrains
    SINGLE groups to at most one pick. The individual choices live in
    MenuItemOption."""

    __tablename__ = "menu_item_option_groups"

    id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
    menu_item_id: Mapped[uuid.UUID] = mapped_column(
        Uuid,
        ForeignKey("menu_items.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    name: Mapped[str] = mapped_column(String(100), nullable=False)
    selection_type: Mapped[str] = mapped_column(
        String(20), nullable=False, default=OptionSelectionType.SINGLE.value
    )
    is_required: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
    display_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)

    created_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), nullable=False
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), onupdate=func.now(), nullable=False
    )
    deleted_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)


class MenuItemOption(Base):
    """One selectable choice within a MenuItemOptionGroup (e.g. "Extra
    Cheese", price_delta=15.00). price_delta is added to the item's
    base price for each selected option — 0 for non-priced choices
    like a spice level."""

    __tablename__ = "menu_item_options"

    id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
    group_id: Mapped[uuid.UUID] = mapped_column(
        Uuid,
        ForeignKey("menu_item_option_groups.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    name: Mapped[str] = mapped_column(String(100), nullable=False)
    price_delta: Mapped[Decimal] = mapped_column(
        Numeric(10, 2), nullable=False, default=Decimal("0.00")
    )
    display_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    is_available: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)

    created_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), nullable=False
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), onupdate=func.now(), nullable=False
    )
    deleted_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
