"""Pharmacy network APIs — Phase 13.6."""
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 (
    PHARMACIES_MANAGE,
    PHARMACY_VIEW,
    PRESCRIPTION_ORDERS_FULFILL,
    PRESCRIPTIONS_SUBMIT,
)
from app.schemas.pharmacy import (
    PharmacyCreate,
    PharmacyRead,
    PrescriptionLifecycleRequest,
    PrescriptionOrderCreate,
    PrescriptionOrderRead,
)
from app.services.pharmacy import PharmacyService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

pharmacies_router = APIRouter()
prescription_orders_router = APIRouter()


@pharmacies_router.post("", response_model=PharmacyRead, status_code=status.HTTP_201_CREATED)
async def create_pharmacy(
    body: PharmacyCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PHARMACIES_MANAGE)),
):
    return await PharmacyService(db).create_pharmacy(tenant_id, body, actor=user)


@pharmacies_router.get("", response_model=list[PharmacyRead])
async def list_pharmacies(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(PHARMACY_VIEW)),
):
    return await PharmacyService(db).list_pharmacies(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@prescription_orders_router.post("", response_model=PrescriptionOrderRead, status_code=status.HTTP_201_CREATED)
async def submit_prescription_order(
    body: PrescriptionOrderCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PRESCRIPTIONS_SUBMIT)),
):
    return await PharmacyService(db).submit_order(tenant_id, body, actor=user)


@prescription_orders_router.get("", response_model=list[PrescriptionOrderRead])
async def list_prescription_orders(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(PHARMACY_VIEW)),
):
    return await PharmacyService(db).list_orders(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@prescription_orders_router.get("/{order_id}", response_model=PrescriptionOrderRead)
async def get_prescription_order(
    order_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(PHARMACY_VIEW)),
):
    return await PharmacyService(db).get_order(tenant_id, order_id)


@prescription_orders_router.post("/{order_id}/accept", response_model=PrescriptionOrderRead)
async def accept_prescription(
    order_id: UUID,
    body: PrescriptionLifecycleRequest,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PRESCRIPTION_ORDERS_FULFILL)),
):
    return await PharmacyService(db).accept(tenant_id, order_id, body, actor=user)


@prescription_orders_router.post("/{order_id}/prepare", response_model=PrescriptionOrderRead)
async def prepare_prescription(
    order_id: UUID,
    body: PrescriptionLifecycleRequest,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PRESCRIPTION_ORDERS_FULFILL)),
):
    return await PharmacyService(db).prepare(tenant_id, order_id, body, actor=user)


@prescription_orders_router.post("/{order_id}/mark-ready", response_model=PrescriptionOrderRead)
async def mark_prescription_ready(
    order_id: UUID,
    body: PrescriptionLifecycleRequest,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PRESCRIPTION_ORDERS_FULFILL)),
):
    return await PharmacyService(db).mark_ready(tenant_id, order_id, body, actor=user)


@prescription_orders_router.post("/{order_id}/complete-pickup", response_model=PrescriptionOrderRead)
async def complete_prescription_pickup(
    order_id: UUID,
    body: PrescriptionLifecycleRequest,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PRESCRIPTION_ORDERS_FULFILL)),
):
    return await PharmacyService(db).complete_pickup(tenant_id, order_id, body, actor=user)


@prescription_orders_router.post("/{order_id}/cancel", response_model=PrescriptionOrderRead)
async def cancel_prescription(
    order_id: UUID,
    body: PrescriptionLifecycleRequest,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PRESCRIPTION_ORDERS_FULFILL)),
):
    return await PharmacyService(db).cancel(tenant_id, order_id, body, actor=user)
