"""SavedFilter model — tenant-scoped, user-scoped saved filter criteria."""

from __future__ import annotations

import uuid
from typing import Any

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

from app.core.db import Base, TenantMixin


class SavedFilter(Base, TenantMixin):
    """Saved filter — reusable filter criteria for list views (contacts, mail, calendar, DMS)."""

    __tablename__ = "saved_filters"
    __table_args__ = (
        UniqueConstraint("tenant_id", "user_id", "entity_type", "name", name="uq_saved_filters_tenant_user_entity_name"),
        Index("ix_saved_filters_tenant_user", "tenant_id", "user_id"),
        Index("ix_saved_filters_tenant_entity", "tenant_id", "entity_type"),
    )

    id: Mapped[uuid.UUID] = mapped_column(
        PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    name: Mapped[str] = mapped_column(String(100), nullable=False)
    entity_type: Mapped[str] = mapped_column(
        String(50), nullable=False
    )  # contacts, mail, calendar, dms
    filter_criteria: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False, default=dict)
    user_id: Mapped[uuid.UUID] = mapped_column(
        PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False
    )
