# FILE: src/apps/plots/models/plot_status.py
"""Tenant-configurable plot status catalog (INDL-41).

The map / list / heat-map / legend all source their status colours from this one
catalog so they never drift (AC-17). It does NOT replace the canonical
``plots.status`` string enum (vacant/reserved/occupied/hold) that the plot state
machine relies on — instead each catalog row optionally carries a ``status_key``
that maps a display entry to the underlying enum value. Custom statuses (with a
NULL ``status_key``) may be added by admins without touching the enum.
"""
from __future__ import annotations

from typing import Optional
from uuid import UUID

from sqlalchemy import Boolean, ForeignKey, SmallInteger, String, UniqueConstraint
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
from sqlalchemy.orm import Mapped, mapped_column

from src.database.base import TenantModel


class PlotStatusCatalog(TenantModel):
    __tablename__ = "plot_statuses"

    __table_args__ = (
        UniqueConstraint("tenant_id", "name", name="uq_plot_statuses_tenant_name"),
    )

    # Override tenant_id with explicit FK (matches plot_types / plots pattern).
    tenant_id: Mapped[UUID] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("accounts.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )

    name: Mapped[str] = mapped_column(String(50), nullable=False)
    color_hex: Mapped[str] = mapped_column(String(7), nullable=False)
    # Maps this catalog entry to the canonical plots.status enum value
    # (vacant/reserved/occupied/hold). NULL for admin-defined custom statuses.
    status_key: Mapped[Optional[str]] = mapped_column(String(20), nullable=True)
    is_default: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    sort_order: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False)
