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

from uuid import UUID

from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.deps import get_db, get_pagination, require_tenant
from app.core.security import get_current_user
from app.schemas.staff import (
    StaffAssignmentCreate,
    StaffAssignmentRead,
    StaffAssignmentUpdate,
    StaffAvailabilityCreate,
    StaffAvailabilityRead,
    StaffAvailabilityUpdate,
    StaffCertificateCreate,
    StaffCertificateRead,
    StaffCertificateUpdate,
    StaffSkillCreate,
    StaffSkillRead,
    StaffSkillUpdate,
    StaffWorkingHoursCreate,
    StaffWorkingHoursRead,
    StaffWorkingHoursUpdate,
)
from app.services.staff import (
    StaffAssignmentService,
    StaffAvailabilityService,
    StaffCertificateService,
    StaffSkillService,
    StaffWorkingHoursService,
)
from shared.pagination import PaginationParams
from shared.security import CurrentUser

certificates_router = APIRouter()
skills_router = APIRouter()
working_hours_router = APIRouter()
availability_router = APIRouter()
assignments_router = APIRouter()


@certificates_router.post("", response_model=StaffCertificateRead, status_code=status.HTTP_201_CREATED)
async def create_certificate(body: StaffCertificateCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await StaffCertificateService(db).create(tenant_id, body, actor=user)


@certificates_router.get("", response_model=list[StaffCertificateRead])
async def list_certificates(coach_id: UUID = Query(...), tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(get_current_user)):
    return await StaffCertificateService(db).list(tenant_id, coach_id, offset=pagination.offset, limit=pagination.page_size)


@skills_router.post("", response_model=StaffSkillRead, status_code=status.HTTP_201_CREATED)
async def create_skill(body: StaffSkillCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await StaffSkillService(db).create(tenant_id, body, actor=user)


@skills_router.get("", response_model=list[StaffSkillRead])
async def list_skills(coach_id: UUID = Query(...), tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(get_current_user)):
    return await StaffSkillService(db).list(tenant_id, coach_id, offset=pagination.offset, limit=pagination.page_size)


@working_hours_router.post("", response_model=StaffWorkingHoursRead, status_code=status.HTTP_201_CREATED)
async def create_working_hours(body: StaffWorkingHoursCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await StaffWorkingHoursService(db).create(tenant_id, body, actor=user)


@working_hours_router.get("", response_model=list[StaffWorkingHoursRead])
async def list_working_hours(coach_id: UUID = Query(...), tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(get_current_user)):
    return await StaffWorkingHoursService(db).list(tenant_id, coach_id, offset=pagination.offset, limit=pagination.page_size)


@availability_router.post("", response_model=StaffAvailabilityRead, status_code=status.HTTP_201_CREATED)
async def create_availability(body: StaffAvailabilityCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await StaffAvailabilityService(db).create(tenant_id, body, actor=user)


@availability_router.get("", response_model=list[StaffAvailabilityRead])
async def list_availability(coach_id: UUID = Query(...), tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(get_current_user)):
    return await StaffAvailabilityService(db).list(tenant_id, coach_id, offset=pagination.offset, limit=pagination.page_size)


@assignments_router.post("", response_model=StaffAssignmentRead, status_code=status.HTTP_201_CREATED)
async def create_assignment(body: StaffAssignmentCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await StaffAssignmentService(db).create(tenant_id, body, actor=user)


@assignments_router.get("", response_model=list[StaffAssignmentRead])
async def list_assignments(coach_id: UUID = Query(...), tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(get_current_user)):
    return await StaffAssignmentService(db).list(tenant_id, coach_id, offset=pagination.offset, limit=pagination.page_size)
