"""POS Pro APIs — Phase 12.5."""
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.pos_pro import (
    PosDiscountCreate,
    PosDiscountRead,
    PosFloorPlanCreate,
    PosFloorPlanRead,
    PosPaymentCreate,
    PosPaymentRead,
    PosStationCreate,
    PosStationRead,
    PosTaxLineCreate,
    PosTaxLineRead,
)
from app.services.pos_pro import (
    PosDiscountService,
    PosFloorPlanService,
    PosPaymentService,
    PosStationService,
    PosTaxLineService,
)
from shared.pagination import PaginationParams
from shared.security import CurrentUser

pos_payments_router = APIRouter()
pos_discounts_router = APIRouter()
pos_tax_lines_router = APIRouter()
pos_floor_plans_router = APIRouter()
pos_stations_router = APIRouter()


@pos_payments_router.post("", response_model=PosPaymentRead, status_code=status.HTTP_201_CREATED)
async def create_pos_payment(
    body: PosPaymentCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosPaymentService(db).create(tenant_id, body, actor=user)


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


@pos_payments_router.get("/{payment_id}", response_model=PosPaymentRead)
async def get_pos_payment(
    payment_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosPaymentService(db).get(tenant_id, payment_id)


@pos_discounts_router.post("", response_model=PosDiscountRead, status_code=status.HTTP_201_CREATED)
async def create_pos_discount(
    body: PosDiscountCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosDiscountService(db).create(tenant_id, body, actor=user)


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


@pos_discounts_router.get("/{discount_id}", response_model=PosDiscountRead)
async def get_pos_discount(
    discount_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosDiscountService(db).get(tenant_id, discount_id)


@pos_tax_lines_router.post("", response_model=PosTaxLineRead, status_code=status.HTTP_201_CREATED)
async def create_pos_tax_line(
    body: PosTaxLineCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosTaxLineService(db).create(tenant_id, body, actor=user)


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


@pos_tax_lines_router.get("/{tax_line_id}", response_model=PosTaxLineRead)
async def get_pos_tax_line(
    tax_line_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosTaxLineService(db).get(tenant_id, tax_line_id)


@pos_floor_plans_router.post(
    "", response_model=PosFloorPlanRead, status_code=status.HTTP_201_CREATED
)
async def create_pos_floor_plan(
    body: PosFloorPlanCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosFloorPlanService(db).create(tenant_id, body, actor=user)


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


@pos_floor_plans_router.get("/{floor_plan_id}", response_model=PosFloorPlanRead)
async def get_pos_floor_plan(
    floor_plan_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosFloorPlanService(db).get(tenant_id, floor_plan_id)


@pos_stations_router.post("", response_model=PosStationRead, status_code=status.HTTP_201_CREATED)
async def create_pos_station(
    body: PosStationCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosStationService(db).create(tenant_id, body, actor=user)


@pos_stations_router.get("", response_model=list[PosStationRead])
async def list_pos_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 PosStationService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@pos_stations_router.get("/{station_id}", response_model=PosStationRead)
async def get_pos_station(
    station_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosStationService(db).get(tenant_id, station_id)
