"""Driver availability APIs — Phase 10.3."""
from __future__ import annotations

from uuid import UUID

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

from app.api.deps import get_db, require_tenant
from app.api.permissions import require_permissions
from app.commands.availability import AvailabilityCommands
from app.permissions.definitions import AVAILABILITY_MANAGE, AVAILABILITY_VIEW
from app.queries.availability import AvailabilityQueries
from app.schemas.availability import DriverAvailabilityRead, DriverAvailabilityUpsert
from shared.security import CurrentUser

router = APIRouter()


@router.get("/{driver_id}/availability", response_model=DriverAvailabilityRead)
async def get_availability(
    driver_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(AVAILABILITY_VIEW)),
):
    return await AvailabilityQueries(db).get_availability(tenant_id, driver_id)


@router.put("/{driver_id}/availability", response_model=DriverAvailabilityRead)
async def upsert_availability(
    driver_id: UUID,
    body: DriverAvailabilityUpsert,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(AVAILABILITY_MANAGE)),
):
    return await AvailabilityCommands(db).upsert_availability(
        tenant_id, driver_id, body, actor=user
    )
