"""Scheduling & Booking APIs — Phase 9.4."""
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.booking import (
    BookingCancelRequest,
    BookingCreate,
    BookingRead,
    EquipmentCreate,
    EquipmentRead,
    EquipmentUpdate,
    ResourceReservationCreate,
    ResourceReservationRead,
    SportsSessionCreate,
    SportsSessionRead,
    SportsSessionUpdate,
    WaitingListCreate,
    WaitingListRead,
)
from app.services.booking import (
    BookingService,
    EquipmentService,
    ResourceReservationService,
    SportsSessionService,
    WaitingListService,
)
from shared.pagination import PaginationParams
from shared.security import CurrentUser

equipment_router = APIRouter()
sessions_router = APIRouter()
bookings_router = APIRouter()
waiting_list_router = APIRouter()
reservations_router = APIRouter()


@equipment_router.post("", response_model=EquipmentRead, status_code=status.HTTP_201_CREATED)
async def create_equipment(body: EquipmentCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await EquipmentService(db).create(tenant_id, body, actor=user)


@equipment_router.get("", response_model=list[EquipmentRead])
async def list_equipment(tenant_id: UUID = Depends(require_tenant), pagination: PaginationParams = Depends(get_pagination), db: AsyncSession = Depends(get_db), _user: CurrentUser = Depends(get_current_user)):
    return await EquipmentService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)


@sessions_router.post("", response_model=SportsSessionRead, status_code=status.HTTP_201_CREATED)
async def create_session(body: SportsSessionCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await SportsSessionService(db).create(tenant_id, body, actor=user)


@sessions_router.get("", response_model=list[SportsSessionRead])
async def list_sessions(sports_center_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 SportsSessionService(db).list(tenant_id, sports_center_id, offset=pagination.offset, limit=pagination.page_size)


@bookings_router.post("", response_model=BookingRead, status_code=status.HTTP_201_CREATED)
async def create_booking(body: BookingCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await BookingService(db).create(tenant_id, body, actor=user)


@bookings_router.post("/{booking_id}/confirm", response_model=BookingRead)
async def confirm_booking(booking_id: UUID, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await BookingService(db).confirm(tenant_id, booking_id, actor=user)


@bookings_router.post("/{booking_id}/cancel", response_model=BookingRead)
async def cancel_booking(booking_id: UUID, body: BookingCancelRequest, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await BookingService(db).cancel(tenant_id, booking_id, body, actor=user)


@waiting_list_router.post("", response_model=WaitingListRead, status_code=status.HTTP_201_CREATED)
async def join_waiting_list(body: WaitingListCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await WaitingListService(db).join(tenant_id, body, actor=user)


@reservations_router.post("", response_model=ResourceReservationRead, status_code=status.HTTP_201_CREATED)
async def create_reservation(body: ResourceReservationCreate, tenant_id: UUID = Depends(require_tenant), db: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user)):
    return await ResourceReservationService(db).create(tenant_id, body, actor=user)
