"""
Lab model — represents the organization's lab profile or branch/unit.

In production this system is single-tenant: one organization.
The LAB table holds branch or unit records (e.g., Main Lab, Branch Lab,
Collection Center). All other branch-scoped entities carry a lab_id FK
pointing here.

Relationships (defined on the owning side in each child model to avoid
circular imports — only back-references listed here):
  Lab ← User             (one lab has many staff users)
  Lab ← Patient          (patients registered at this branch)
  Lab ← TestCategory     (test categories configured per branch)
  Lab ← TestPackage      (packages configured per branch)
  Lab ← AppointmentSlot  (slots defined per branch)
  Lab ← VisitOrder       (orders placed at this branch)
  Lab ← DiscountRule     (discount rules configured per branch)
  Lab ← Referral         (referral sources per branch)
  Lab ← Payment          (payments recorded per branch)

Soft delete: YES — consistency with the SoftDeleteMixin pattern.
             Practically: a branch closure would set deleted_at rather
             than destroying records.
"""

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

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


class Lab(UUIDPrimaryKeyMixin, TimestampMixin, SoftDeleteMixin, Base):
    """
    Lab branch/unit profile.

    Single record per branch. The `name` column is what appears on
    reports and patient-facing documents.
    """

    __tablename__ = "lab"

    name: Mapped[str] = mapped_column(
        String(200),
        nullable=False,
        comment="Lab or branch display name (appears on reports)",
    )
    address: Mapped[str | None] = mapped_column(
        Text,
        nullable=True,
        comment="Physical address of this branch",
    )
    logo_url: Mapped[str | None] = mapped_column(
        String(500),
        nullable=True,
        comment="Storage path or URL for the lab logo (used in PDF reports)",
    )

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