import uuid

from sqlalchemy import Boolean, ForeignKey, String
from sqlalchemy.dialects.postgresql import JSONB, UUID as PGUUID
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base, TenantScopedMixin, TimestampMixin, UUIDPKMixin


class SavedView(UUIDPKMixin, TenantScopedMixin, TimestampMixin, Base):
    """A person's named view of a page's Data List — its sort, column
    filters, hidden columns, display and the Filter Bar values narrowing it
    (`state`, kept as the browser sends it). One per person, page and block
    may be their default, applied when the page opens. Only ever their own —
    see app/routers/saved_views.py."""

    __tablename__ = "saved_views"

    user_id: Mapped[uuid.UUID] = mapped_column(PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
    page_id: Mapped[uuid.UUID] = mapped_column(PGUUID(as_uuid=True), nullable=False)
    block_id: Mapped[str] = mapped_column(String(100), nullable=False)
    name: Mapped[str] = mapped_column(String(120), nullable=False)
    state: Mapped[dict] = mapped_column(JSONB, nullable=False, server_default="{}")
    is_default: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default="false")
