"""Service catalog aggregates — Phase 14.3."""
from __future__ import annotations

import uuid

from sqlalchemy import Boolean, Index, Integer, Numeric, 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, ServiceStatus


class ServiceCategory(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "service_categories"

    organization_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.ACTIVE, nullable=False
    )
    sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_service_categories_tenant_code"),
        Index("ix_service_categories_org", "tenant_id", "organization_id"),
    )


class BeautyService(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "beauty_services"

    organization_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    category_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    status: Mapped[ServiceStatus] = mapped_column(
        default=ServiceStatus.DRAFT, nullable=False
    )
    base_duration_minutes: Mapped[int] = mapped_column(Integer, default=30, nullable=False)
    base_price: Mapped[float | None] = mapped_column(Numeric(18, 2), nullable=True)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_beauty_services_tenant_code"),
        Index("ix_beauty_services_org", "tenant_id", "organization_id"),
    )


class ServiceVariant(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "service_variants"

    service_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    duration_minutes: Mapped[int] = mapped_column(Integer, default=30, nullable=False)
    price: Mapped[float | None] = mapped_column(Numeric(18, 2), nullable=True)
    is_default: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)

    __table_args__ = (
        UniqueConstraint(
            "tenant_id", "service_id", "code", name="uq_service_variants_tenant_code"
        ),
        Index("ix_service_variants_service", "tenant_id", "service_id"),
    )


class BranchServiceAssignment(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "branch_service_assignments"

    branch_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    service_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    price_override: Mapped[float | None] = mapped_column(Numeric(18, 2), nullable=True)

    __table_args__ = (
        Index("ix_branch_service_assignments_branch", "tenant_id", "branch_id"),
        Index("ix_branch_service_assignments_service", "tenant_id", "service_id"),
    )


class ServiceMediaRef(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "service_media_refs"

    service_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    storage_file_ref: Mapped[str] = mapped_column(String(255), nullable=False)
    title: Mapped[str | None] = mapped_column(String(255), nullable=True)
    sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)

    __table_args__ = (
        Index("ix_service_media_refs_service", "tenant_id", "service_id"),
    )
