import uuid
from datetime import datetime

from sqlalchemy import DateTime, ForeignKey, UniqueConstraint, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column

from app.db.session import Base


class Favorite(Base):
    """A customer saving a branch for quick access later — scoped to
    Branch, not MenuItem, matching the live Flutter customer app's
    existing favorite-restaurant screen (there's no menu-item-level
    favoriting UI to build against). Unique on (customer_id,
    branch_id) so favoriting an already-favorited branch again is a
    harmless no-op (see app.services.favorites.add_favorite) rather
    than a duplicate row."""

    __tablename__ = "favorites"
    __table_args__ = (UniqueConstraint("customer_id", "branch_id"),)

    id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
    customer_id: Mapped[uuid.UUID] = mapped_column(
        Uuid, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
    )
    branch_id: Mapped[uuid.UUID] = mapped_column(
        Uuid, ForeignKey("branches.id", ondelete="CASCADE"), nullable=False, index=True
    )

    created_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), nullable=False
    )
