"""Clinic management APIs — Phase 13.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.api.permissions import require_permissions
from app.commands.clinic_management import ClinicManagementCommands, ClinicManagementQueries
from app.permissions.definitions import CLINIC_ADMIN_MANAGE, CLINIC_ADMIN_VIEW, CLINIC_SERVICES_MANAGE
from app.schemas.clinic_management import (
    ClinicPolicyCreate,
    ClinicPolicyRead,
    ClinicServiceCreate,
    ClinicServiceRead,
    OperatingHoursPolicyCreate,
    OperatingHoursPolicyRead,
    StaffAssignmentCreate,
    StaffAssignmentRead,
)
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()
services_router = APIRouter()
staff_router = APIRouter()
policies_router = APIRouter()
hours_router = APIRouter()


@services_router.post("", response_model=ClinicServiceRead, status_code=status.HTTP_201_CREATED)
async def create_clinic_service(
    body: ClinicServiceCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(CLINIC_SERVICES_MANAGE)),
):
    return await ClinicManagementCommands(db).create_service(tenant_id, body, actor=user)


@services_router.get("", response_model=list[ClinicServiceRead])
async def list_clinic_services(
    clinic_id: UUID | None = Query(default=None),
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(CLINIC_ADMIN_VIEW)),
):
    return await ClinicManagementQueries(db).list_services(
        tenant_id, clinic_id=clinic_id, offset=pagination.offset, limit=pagination.page_size
    )


@staff_router.post("", response_model=StaffAssignmentRead, status_code=status.HTTP_201_CREATED)
async def create_staff_assignment(
    body: StaffAssignmentCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(CLINIC_ADMIN_MANAGE)),
):
    return await ClinicManagementCommands(db).create_staff(tenant_id, body, actor=user)


@staff_router.get("", response_model=list[StaffAssignmentRead])
async def list_staff_assignments(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(CLINIC_ADMIN_VIEW)),
):
    return await ClinicManagementQueries(db).list_staff(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@policies_router.post("", response_model=ClinicPolicyRead, status_code=status.HTTP_201_CREATED)
async def create_clinic_policy(
    body: ClinicPolicyCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(CLINIC_ADMIN_MANAGE)),
):
    return await ClinicManagementCommands(db).create_policy(tenant_id, body, actor=user)


@policies_router.get("", response_model=list[ClinicPolicyRead])
async def list_clinic_policies(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(CLINIC_ADMIN_VIEW)),
):
    return await ClinicManagementQueries(db).list_policies(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@hours_router.post("", response_model=OperatingHoursPolicyRead, status_code=status.HTTP_201_CREATED)
async def create_operating_hours(
    body: OperatingHoursPolicyCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(CLINIC_ADMIN_MANAGE)),
):
    return await ClinicManagementCommands(db).create_hours(tenant_id, body, actor=user)


@hours_router.get("", response_model=list[OperatingHoursPolicyRead])
async def list_operating_hours(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(CLINIC_ADMIN_VIEW)),
):
    return await ClinicManagementQueries(db).list_hours(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )
