"""Salon management aggregates — Phase 14.2."""
from __future__ import annotations

import uuid

from sqlalchemy import Index, Integer, String, Text, Time, 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 DayOfWeek, GUID, LifecycleStatus, ResourceStatus


class TreatmentRoom(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "treatment_rooms"

    organization_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    branch_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[ResourceStatus] = mapped_column(
        default=ResourceStatus.AVAILABLE, nullable=False
    )
    capacity: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_treatment_rooms_tenant_code"),
        Index("ix_treatment_rooms_branch", "tenant_id", "branch_id"),
    )


class Station(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "stations"

    organization_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    branch_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    room_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)
    status: Mapped[ResourceStatus] = mapped_column(
        default=ResourceStatus.AVAILABLE, nullable=False
    )
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_stations_tenant_code"),
        Index("ix_stations_branch", "tenant_id", "branch_id"),
    )


class BranchOperatingHours(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "branch_operating_hours"

    organization_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    branch_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    day_of_week: Mapped[DayOfWeek] = mapped_column(nullable=False)
    open_time: Mapped[Time] = mapped_column(Time, nullable=False)
    close_time: Mapped[Time] = mapped_column(Time, nullable=False)
    is_closed: Mapped[bool] = mapped_column(default=False, nullable=False)

    __table_args__ = (
        Index("ix_branch_operating_hours_branch", "tenant_id", "branch_id"),
    )


class BranchPolicy(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "branch_policies"

    organization_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    branch_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)
    policy_type: Mapped[str] = mapped_column(String(50), nullable=False)
    rules: Mapped[dict | None] = mapped_column(JSON, nullable=True)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.ACTIVE, nullable=False
    )

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_branch_policies_tenant_code"),
        Index("ix_branch_policies_branch", "tenant_id", "branch_id"),
    )
