import uuid
from datetime import datetime

from sqlalchemy import DateTime, ForeignKey, String, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column

from app.db.session import Base


class GroceryItemDetails(Base):
    """Grocery-only catalog attributes for a MenuItem — a 1:1
    extension, not a fork: MenuItem itself stays completely untouched
    and every restaurant dish simply has no row here. Created only for
    a MenuItem that's actually sold by weight/volume/bundle (see
    app.api.v1.endpoints.branches.create_menu_item), so a normal
    per-piece grocery product (e.g. a canned drink) also has no row
    here, same as a restaurant dish.

    Mirrors app.models.delivery.CourierProfile's own pattern of
    extending an entity without adding role/vertical-specific columns
    to the shared table it's about.

    unit_of_measure (e.g. "kg", "liter", "pack") is what allows a
    fractional cart quantity for this item — see
    app.api.v1.endpoints.cart._require_whole_number_if_no_unit and
    app.models.cart.CartItem.quantity. Room to add more grocery-only
    fields later (barcode, brand, expiry) without ever touching
    MenuItem again.
    """

    __tablename__ = "grocery_item_details"

    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"),
        unique=True,
        nullable=False,
    )
    unit_of_measure: Mapped[str] = mapped_column(String(20), nullable=False)

    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
    )
