"""Patient portal aggregates — Phase 13.4."""
from __future__ import annotations

import uuid

from sqlalchemy import Boolean, Index, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import JSON

from app.core.database import Base
from app.models.base import (
    ActorAuditMixin,
    OptimisticLockMixin,
    SoftDeleteMixin,
    TenantMixin,
    TimestampMixin,
    UUIDPrimaryKeyMixin,
)
from app.models.types import GUID


class PatientPortalProfile(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
    OptimisticLockMixin,
):
    __tablename__ = "patient_portal_profiles"

    patient_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    preferred_clinic_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    preferred_language: Mapped[str] = mapped_column(String(16), default="fa", nullable=False)
    emergency_contact_ref: Mapped[str | None] = mapped_column(String(255), nullable=True)
    notification_prefs: Mapped[dict | None] = mapped_column(JSON, nullable=True)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "patient_id", name="uq_patient_portal_profiles_patient"),
        Index("ix_patient_portal_profiles_tenant", "tenant_id", "patient_id"),
    )


class PatientDocumentRef(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "patient_document_refs"

    patient_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    title: Mapped[str] = mapped_column(String(255), nullable=False)
    storage_file_ref: Mapped[str] = mapped_column(String(255), nullable=False)
    document_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_patient_document_refs_tenant_code"),
        Index("ix_patient_document_refs_patient", "tenant_id", "patient_id"),
    )


class PatientNotificationPreference(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "patient_notification_preferences"

    patient_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    channel: Mapped[str] = mapped_column(String(32), nullable=False)
    template_key: Mapped[str] = mapped_column(String(100), nullable=False)
    enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint(
            "tenant_id",
            "patient_id",
            "channel",
            "template_key",
            name="uq_patient_notification_prefs",
        ),
        Index("ix_patient_notification_prefs_patient", "tenant_id", "patient_id"),
    )
