"""Coach & Staff Management aggregates — Phase 9.3."""
from __future__ import annotations

import uuid
from datetime import date, datetime, time

from sqlalchemy import Boolean, Date, DateTime, Index, Integer, 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 AssignmentKind, GUID, LifecycleStatus


class StaffCertificate(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "staff_certificates"

    sports_center_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    coach_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    issuer: Mapped[str | None] = mapped_column(String(255), nullable=True)
    certificate_number: Mapped[str | None] = mapped_column(String(100), nullable=True)
    issued_on: Mapped[date | None] = mapped_column(Date, nullable=True)
    expires_on: Mapped[date | None] = mapped_column(Date, nullable=True)
    file_ref: Mapped[str | None] = mapped_column(String(255), nullable=True)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.ACTIVE, nullable=False
    )
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    __table_args__ = (
        Index("ix_staff_certificates_coach", "tenant_id", "coach_id"),
        Index("ix_staff_certificates_tenant_deleted", "tenant_id", "is_deleted"),
    )


class StaffSkill(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "staff_skills"

    sports_center_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    coach_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    skill_code: Mapped[str] = mapped_column(String(50), nullable=False)
    skill_name: Mapped[str] = mapped_column(String(255), nullable=False)
    level: Mapped[int | None] = mapped_column(Integer, nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    __table_args__ = (
        UniqueConstraint(
            "tenant_id",
            "coach_id",
            "skill_code",
            name="uq_staff_skills_tenant_coach_code",
        ),
        Index("ix_staff_skills_coach", "tenant_id", "coach_id"),
        Index("ix_staff_skills_tenant_deleted", "tenant_id", "is_deleted"),
    )


class StaffWorkingHours(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "staff_working_hours"

    sports_center_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    coach_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    branch_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    day_of_week: Mapped[int] = mapped_column(Integer, nullable=False)
    starts_at: Mapped[time] = mapped_column(nullable=False)
    ends_at: Mapped[time] = mapped_column(nullable=False)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)

    __table_args__ = (
        Index("ix_staff_working_hours_coach", "tenant_id", "coach_id"),
        Index("ix_staff_working_hours_tenant_deleted", "tenant_id", "is_deleted"),
    )


class StaffAvailability(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "staff_availability"

    sports_center_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    coach_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    starts_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
    ends_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
    is_available: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    reason: Mapped[str | None] = mapped_column(String(255), nullable=True)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        Index("ix_staff_availability_coach", "tenant_id", "coach_id"),
        Index("ix_staff_availability_window", "tenant_id", "starts_at", "ends_at"),
        Index("ix_staff_availability_tenant_deleted", "tenant_id", "is_deleted"),
    )


class StaffAssignment(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    __tablename__ = "staff_assignments"

    sports_center_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    coach_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    assignment_kind: Mapped[AssignmentKind] = mapped_column(nullable=False)
    member_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    sport_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    program_ref: Mapped[str | None] = mapped_column(String(100), nullable=True)
    starts_on: Mapped[date | None] = mapped_column(Date, nullable=True)
    ends_on: Mapped[date | None] = mapped_column(Date, nullable=True)
    status: Mapped[LifecycleStatus] = mapped_column(
        default=LifecycleStatus.ACTIVE, nullable=False
    )
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    __table_args__ = (
        Index("ix_staff_assignments_coach", "tenant_id", "coach_id"),
        Index("ix_staff_assignments_member", "tenant_id", "member_id"),
        Index("ix_staff_assignments_tenant_deleted", "tenant_id", "is_deleted"),
    )
