from datetime import UTC, datetime

from sqlmodel import Field, SQLModel


def utcnow() -> datetime:
    return datetime.now(UTC).replace(tzinfo=None)


class TenantModel(SQLModel, table=False):
    """Base class for all tenant-scoped models.

    Provides restaurant_id (FK to restaurant.id), created_at, and updated_at.
    All tables inheriting this base are filtered by restaurant_id at the app layer
    (not via Postgres RLS, which breaks with Clerk's service-level connections).
    """

    restaurant_id: str = Field(
        foreign_key="restaurant.id",
        index=True,
        nullable=False,
    )
    created_at: datetime = Field(default_factory=utcnow, nullable=False)
    updated_at: datetime | None = Field(
        default=None,
        sa_column_kwargs={"onupdate": utcnow},
        nullable=True,
    )
