<?php

declare(strict_types=1);

namespace ApiModel\Route;

use ApiModel\Action\PurchasesAction;
use ApiModel\Shared\Support\HttpRunner;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Routing\RouteCollectorProxy;

final class PurchasesRoutes
{
    public static function register(RouteCollectorProxy $g, PurchasesAction $action, string $uuid): void
    {
        // Purchase & Service Orders Endpoints
        $g->get('/purchases/orders', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->listOrders($request)));
        $g->get("/purchases/orders/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->getOrder($request, $args['id'])));
        $g->post('/purchases/orders', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->saveOrder($request)));
        $g->patch("/purchases/orders/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->saveOrder($request)));
        $g->delete("/purchases/orders/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->deleteOrder($request, $args['id'])));
        $g->post("/purchases/orders/{id:$uuid}/precommit", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->precommitOrder($request, $args['id'])));

        // Bills (Facturas de Compra) Endpoints
        $g->get('/purchases/bills', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->listBills($request)));
        $g->get("/purchases/bills/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->getBill($request, $args['id'])));
        $g->post('/purchases/bills', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->saveBill($request)));
        $g->patch("/purchases/bills/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->saveBill($request)));
        $g->delete("/purchases/bills/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->deleteBill($request, $args['id'])));
        $g->get("/purchases/bills/unpaid/{vendorId:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->listUnpaidBills($request, $args['vendorId'])));

        // Purchase Payments (Pagos a Proveedores) Endpoints
        $g->get('/purchases/payments', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->listPayments($request)));
        $g->get("/purchases/payments/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->getPayment($request, $args['id'])));
        $g->post('/purchases/payments', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->savePayment($request)));
        $g->delete("/purchases/payments/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->deletePayment($request, $args['id'])));

        // Expenses (Gastos Directos de Caja) Endpoints
        $g->get('/purchases/expenses', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->listExpenses($request)));
        $g->get("/purchases/expenses/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->getExpense($request, $args['id'])));
        $g->post('/purchases/expenses', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->saveExpense($request)));
        $g->patch("/purchases/expenses/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->saveExpense($request)));
        $g->delete("/purchases/expenses/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->deleteExpense($request, $args['id'])));
    }
}
