<?php

declare(strict_types=1);

namespace ApiModel\Route;

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

final class SalesRoutes
{
    public static function register(RouteCollectorProxy $g, SalesAction $action, string $uuid): void
    {
        // Sales Invoices Endpoints
        $g->get('/sales/invoices', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->listInvoices($request)));
        $g->get("/sales/invoices/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->getInvoice($request, $args['id'])));
        $g->post('/sales/invoices', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $action->saveInvoice($request)));
        $g->patch("/sales/invoices/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->saveInvoice($request)));
        $g->delete("/sales/invoices/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->deleteInvoice($request, $args['id'])));
        $g->get("/sales/invoices/unpaid/{customerId:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $action->listUnpaidInvoices($request, $args['customerId'])));

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