"""POS Lite APIs — Phase 12.4."""
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_lite import (
    PosRegisterCreate,
    PosRegisterRead,
    PosShiftCreate,
    PosShiftRead,
    PosTicketCreate,
    PosTicketLineCreate,
    PosTicketLineRead,
    PosTicketRead,
)
from app.services.pos_lite import (
    PosRegisterService,
    PosShiftService,
    PosTicketLineService,
    PosTicketService,
)
from shared.pagination import PaginationParams
from shared.security import CurrentUser

pos_registers_router = APIRouter()
pos_shifts_router = APIRouter()
pos_tickets_router = APIRouter()
pos_ticket_lines_router = APIRouter()


@pos_registers_router.post("", response_model=PosRegisterRead, status_code=status.HTTP_201_CREATED)
async def create_pos_register(
    body: PosRegisterCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosRegisterService(db).create(tenant_id, body, actor=user)


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


@pos_registers_router.get("/{register_id}", response_model=PosRegisterRead)
async def get_pos_register(
    register_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosRegisterService(db).get(tenant_id, register_id)


@pos_shifts_router.post("", response_model=PosShiftRead, status_code=status.HTTP_201_CREATED)
async def open_pos_shift(
    body: PosShiftCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosShiftService(db).create(tenant_id, body, actor=user)


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


@pos_shifts_router.get("/{shift_id}", response_model=PosShiftRead)
async def get_pos_shift(
    shift_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosShiftService(db).get(tenant_id, shift_id)


@pos_shifts_router.post("/{shift_id}/close", response_model=PosShiftRead)
async def close_pos_shift(
    shift_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosShiftService(db).close(tenant_id, shift_id, actor=user)


@pos_tickets_router.post("", response_model=PosTicketRead, status_code=status.HTTP_201_CREATED)
async def create_pos_ticket(
    body: PosTicketCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosTicketService(db).create(tenant_id, body, actor=user)


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


@pos_tickets_router.get("/{ticket_id}", response_model=PosTicketRead)
async def get_pos_ticket(
    ticket_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosTicketService(db).get(tenant_id, ticket_id)


@pos_tickets_router.post("/{ticket_id}/void", response_model=PosTicketRead)
async def void_pos_ticket(
    ticket_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosTicketService(db).void(tenant_id, ticket_id, actor=user)


@pos_ticket_lines_router.post(
    "", response_model=PosTicketLineRead, status_code=status.HTTP_201_CREATED
)
async def add_pos_ticket_line(
    body: PosTicketLineCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await PosTicketLineService(db).add(tenant_id, body, actor=user)


@pos_ticket_lines_router.get("", response_model=list[PosTicketLineRead])
async def list_pos_ticket_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 PosTicketLineService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@pos_ticket_lines_router.get("/{line_id}", response_model=PosTicketLineRead)
async def get_pos_ticket_line(
    line_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PosTicketLineService(db).get(tenant_id, line_id)
