"""Delivery integration APIs — Phase 13.7."""
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 (
    DELIVERY_INTEGRATION_MANAGE,
    DELIVERY_INTEGRATION_SUBMIT,
    DELIVERY_INTEGRATION_VIEW,
)
from app.schemas.delivery_integration import (
    DeliveryIntegrationCreate,
    DeliveryIntegrationRead,
    DeliveryJobIntentCreate,
    DeliveryJobIntentRead,
    DeliveryStatusWebhook,
)
from app.services.delivery_integration import DeliveryIntegrationService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()
integrations_router = APIRouter()
intents_router = APIRouter()
webhooks_router = APIRouter()


@integrations_router.post("", response_model=DeliveryIntegrationRead, status_code=status.HTTP_201_CREATED)
async def register_delivery_integration(
    body: DeliveryIntegrationCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(DELIVERY_INTEGRATION_MANAGE)),
):
    return await DeliveryIntegrationService(db).register(tenant_id, body, actor=user)


@integrations_router.get("", response_model=list[DeliveryIntegrationRead])
async def list_delivery_integrations(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(DELIVERY_INTEGRATION_VIEW)),
):
    return await DeliveryIntegrationService(db).list_registrations(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@intents_router.get("", response_model=list[DeliveryJobIntentRead])
async def list_delivery_job_intents(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(DELIVERY_INTEGRATION_VIEW)),
):
    return await DeliveryIntegrationService(db).list_intents(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@intents_router.post(
    "/prescription-orders/{prescription_order_id}",
    response_model=DeliveryJobIntentRead,
    status_code=status.HTTP_201_CREATED,
)
async def create_delivery_intent(
    prescription_order_id: UUID,
    body: DeliveryJobIntentCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(DELIVERY_INTEGRATION_SUBMIT)),
):
    return await DeliveryIntegrationService(db).create_intent_for_order(
        tenant_id, prescription_order_id, body, actor=user
    )


@webhooks_router.post("/delivery-status", response_model=DeliveryJobIntentRead)
async def delivery_status_webhook(
    body: DeliveryStatusWebhook,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
):
    return await DeliveryIntegrationService(db).apply_status_webhook(tenant_id, body)
