"""Customer management aggregates — Phase 14.4."""
from __future__ import annotations

import uuid
from datetime import datetime

from sqlalchemy import DateTime, 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,
    SoftDeleteMixin,
    TenantMixin,
    TimestampMixin,
    UUIDPrimaryKeyMixin,
)
from app.models.types import GUID, LifecycleStatus


class CustomerProfile(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "customer_profiles"

    organization_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    display_name: Mapped[str] = mapped_column(String(255), nullable=False)
    external_crm_contact_ref: Mapped[str | None] = mapped_column(String(100), nullable=True)
    external_user_ref: Mapped[str | None] = mapped_column(String(100), nullable=True)
    mobile: Mapped[str | None] = mapped_column(String(32), nullable=True)
    email: Mapped[str | None] = mapped_column(String(255), nullable=True)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.ACTIVE, nullable=False
    )
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_customer_profiles_tenant_code"),
        Index("ix_customer_profiles_org", "tenant_id", "organization_id"),
    )


class CustomerPreference(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "customer_preferences"

    customer_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    key: Mapped[str] = mapped_column(String(100), nullable=False)
    value: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint(
            "tenant_id", "customer_id", "key", name="uq_customer_preferences_key"
        ),
        Index("ix_customer_preferences_customer", "tenant_id", "customer_id"),
    )


class VisitHistoryEntry(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "visit_history_entries"

    customer_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    appointment_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    service_ref: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    staff_ref: Mapped[str | None] = mapped_column(String(100), nullable=True)
    visited_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    __table_args__ = (
        Index("ix_visit_history_entries_customer", "tenant_id", "customer_id"),
    )


class CustomerDocumentRef(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "customer_document_refs"

    customer_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    title: Mapped[str] = mapped_column(String(255), nullable=False)
    storage_file_ref: Mapped[str] = mapped_column(String(255), nullable=False)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    __table_args__ = (
        Index("ix_customer_document_refs_customer", "tenant_id", "customer_id"),
    )
