"""Vehicle type APIs — Phase 10.2."""
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.fleet import FleetCommands
from app.permissions.definitions import (
    VEHICLE_TYPES_CREATE,
    VEHICLE_TYPES_DELETE,
    VEHICLE_TYPES_UPDATE,
    VEHICLE_TYPES_VIEW,
)
from app.queries.fleet import FleetQueries
from app.schemas.fleet import (
    VehicleTypeCreate,
    VehicleTypeListResponse,
    VehicleTypeRead,
    VehicleTypeUpdate,
)
from app.specifications.fleet import VehicleTypeListSpec
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


def _list_spec(
    q: str | None = Query(default=None, max_length=100),
    sort_by: str = Query(default="created_at"),
    sort_dir: str = Query(default="desc", pattern="^(?i)(asc|desc)$"),
) -> VehicleTypeListSpec:
    return VehicleTypeListSpec(q=q, sort_by=sort_by, sort_dir=sort_dir.lower())


@router.post("", response_model=VehicleTypeRead, status_code=status.HTTP_201_CREATED)
async def create_vehicle_type(
    body: VehicleTypeCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(VEHICLE_TYPES_CREATE)),
):
    return await FleetCommands(db).create_vehicle_type(tenant_id, body, actor=user)


@router.get("", response_model=VehicleTypeListResponse)
async def list_vehicle_types(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    spec: VehicleTypeListSpec = Depends(_list_spec),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(VEHICLE_TYPES_VIEW)),
):
    items, total = await FleetQueries(db).list_vehicle_types(
        tenant_id, offset=pagination.offset, limit=pagination.page_size, spec=spec
    )
    return VehicleTypeListResponse(
        items=items, total=total, page=pagination.page, page_size=pagination.page_size
    )


@router.get("/{type_id}", response_model=VehicleTypeRead)
async def get_vehicle_type(
    type_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(VEHICLE_TYPES_VIEW)),
):
    return await FleetQueries(db).get_vehicle_type(tenant_id, type_id)


@router.patch("/{type_id}", response_model=VehicleTypeRead)
async def update_vehicle_type(
    type_id: UUID,
    body: VehicleTypeUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(VEHICLE_TYPES_UPDATE)),
):
    return await FleetCommands(db).update_vehicle_type(
        tenant_id, type_id, body, actor=user
    )


@router.post("/{type_id}/delete", response_model=VehicleTypeRead)
async def delete_vehicle_type(
    type_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(VEHICLE_TYPES_DELETE)),
):
    return await FleetCommands(db).delete_vehicle_type(tenant_id, type_id, actor=user)
