"""
Unit of measure catalog.

A single shared (global, NOT lab-scoped) list of measurement units used by
test parameters (e.g. 'mg/dL', '10^3/uL'). Units are universal scientific
tokens, so one org-wide list avoids per-branch duplication.

Test parameters reference units by plain string (TestParameter.unit), NOT by
FK — deleting/renaming a unit never breaks historical result data.

Soft delete: NO — delete is a hard delete (replaceable config row, no FK
references). System-seeded units (is_system=True) cannot be deleted, only
deactivated, so the standard list is never lost.
"""

from sqlalchemy import Boolean, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin


class UnitOfMeasure(UUIDPrimaryKeyMixin, TimestampMixin, Base):
    """A reusable measurement unit for test parameters."""

    __tablename__ = "unit_of_measure"

    name: Mapped[str] = mapped_column(
        String(50),
        nullable=False,
        unique=True,
        index=True,
        comment="Unit symbol as shown on reports (e.g. 'mg/dL')",
    )
    description: Mapped[str | None] = mapped_column(
        Text,
        nullable=True,
        comment="Optional human description (e.g. 'milligrams per deciliter')",
    )
    is_system: Mapped[bool] = mapped_column(
        Boolean,
        nullable=False,
        default=False,
        server_default="false",
        comment="True = seeded standard unit; cannot be deleted (only deactivated)",
    )
    is_active: Mapped[bool] = mapped_column(
        Boolean,
        nullable=False,
        default=True,
        server_default="true",
        comment="False = hidden from the unit picker for new analytes",
    )

    def __repr__(self) -> str:
        return f"<UnitOfMeasure id={self.id!s:.8} name={self.name!r}>"
