"""Kitchen APIs — Phase 12.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.core.security import get_current_user
from app.schemas.kitchen import (
    KitchenStationCreate,
    KitchenStationRead,
    KitchenTicketCreate,
    KitchenTicketItemCreate,
    KitchenTicketItemRead,
    KitchenTicketItemStatusUpdate,
    KitchenTicketRead,
    KitchenTicketStatusUpdate,
)
from app.services.kitchen import (
    KitchenStationService,
    KitchenTicketItemService,
    KitchenTicketService,
)
from shared.pagination import PaginationParams
from shared.security import CurrentUser

kitchen_stations_router = APIRouter()
kitchen_tickets_router = APIRouter()
kitchen_ticket_items_router = APIRouter()


@kitchen_stations_router.post(
    "", response_model=KitchenStationRead, status_code=status.HTTP_201_CREATED
)
async def create_kitchen_station(
    body: KitchenStationCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await KitchenStationService(db).create(tenant_id, body, actor=user)


@kitchen_stations_router.get("", response_model=list[KitchenStationRead])
async def list_kitchen_stations(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await KitchenStationService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@kitchen_stations_router.get("/{station_id}", response_model=KitchenStationRead)
async def get_kitchen_station(
    station_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await KitchenStationService(db).get(tenant_id, station_id)


@kitchen_tickets_router.post(
    "", response_model=KitchenTicketRead, status_code=status.HTTP_201_CREATED
)
async def create_kitchen_ticket(
    body: KitchenTicketCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await KitchenTicketService(db).create(tenant_id, body, actor=user)


@kitchen_tickets_router.get("", response_model=list[KitchenTicketRead])
async def list_kitchen_tickets(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await KitchenTicketService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@kitchen_tickets_router.get("/{ticket_id}", response_model=KitchenTicketRead)
async def get_kitchen_ticket(
    ticket_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await KitchenTicketService(db).get(tenant_id, ticket_id)


@kitchen_tickets_router.post("/{ticket_id}/status", response_model=KitchenTicketRead)
async def change_kitchen_ticket_status(
    ticket_id: UUID,
    body: KitchenTicketStatusUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await KitchenTicketService(db).change_status(
        tenant_id, ticket_id, body.status, actor=user
    )


@kitchen_ticket_items_router.post(
    "", response_model=KitchenTicketItemRead, status_code=status.HTTP_201_CREATED
)
async def create_kitchen_ticket_item(
    body: KitchenTicketItemCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await KitchenTicketItemService(db).create(tenant_id, body, actor=user)


@kitchen_ticket_items_router.get("", response_model=list[KitchenTicketItemRead])
async def list_kitchen_ticket_items(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await KitchenTicketItemService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@kitchen_ticket_items_router.get("/{item_id}", response_model=KitchenTicketItemRead)
async def get_kitchen_ticket_item(
    item_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await KitchenTicketItemService(db).get(tenant_id, item_id)


@kitchen_ticket_items_router.post("/{item_id}/status", response_model=KitchenTicketItemRead)
async def change_kitchen_ticket_item_status(
    item_id: UUID,
    body: KitchenTicketItemStatusUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await KitchenTicketItemService(db).change_status(
        tenant_id, item_id, body.status, actor=user
    )
