"""Delivery settings APIs — Phase 13.0."""
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, get_pagination, require_tenant
from app.api.permissions import require_permissions
from app.permissions.definitions import SETTINGS_MANAGE, SETTINGS_VIEW
from app.schemas.foundation import HealthcareSettingRead, HealthcareSettingUpsert
from app.services.foundation import HealthcareSettingService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.put("", response_model=HealthcareSettingRead, status_code=status.HTTP_200_OK)
async def upsert_setting(
    body: HealthcareSettingUpsert,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(SETTINGS_MANAGE)),
):
    return await HealthcareSettingService(db).upsert(tenant_id, body, actor=user)


@router.get("", response_model=list[HealthcareSettingRead])
async def list_settings(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(SETTINGS_VIEW)),
):
    return await HealthcareSettingService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )
