<?php

declare(strict_types=1);

namespace ApiModel\Action;

use ApiModel\Service\PagoFerreteriaService;
use ApiModel\Shared\Support\AuthContext;
use ApiModel\Shared\Http\HttpUtil;
use Psr\Http\Message\ServerRequestInterface;

final class PagoFerreteriaAction
{
    public function __construct(private readonly PagoFerreteriaService $service) {}

    /** @return list<array<string, mixed>> */
    public function listar(ServerRequestInterface $request): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMINISTRADOR']);

        $params = $request->getQueryParams();
        $ferreteriaId = isset($params['ferreteria_id']) && trim((string) $params['ferreteria_id']) !== '' ? (string) $params['ferreteria_id'] : null;

        return $this->service->listPagos($ferreteriaId);
    }

    /** @return array<string, mixed> */
    public function crear(ServerRequestInterface $request): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMINISTRADOR']);

        $b = HttpUtil::parseJson($request);

        return $this->service->createPago($b, $u['userId']);
    }

    /** @return array{ok: true} */
    public function eliminar(ServerRequestInterface $request, string $id): array
    {
        $u = AuthContext::jwtUser($request);
        AuthContext::requireRoles($u, ['ADMINISTRADOR']);

        $this->service->deletePago($id);

        return ['ok' => true];
    }
}
