<?php

declare(strict_types=1);

namespace ApiModel\Action;

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

final class CurrencyAction
{
    public function __construct(private readonly CurrencyService $service) {}

    /** @return list<array<string, mixed>> */
    public function listCurrencies(ServerRequestInterface $request): array
    {
        AuthContext::jwtUser($request);
        $params = $request->getQueryParams();
        $activeOnly = isset($params['active']) && in_array((string) $params['active'], ['1', 'true'], true);

        return $this->service->listCurrencies($activeOnly ? true : null);
    }

    /** @return array<string, mixed> */
    public function getCurrency(ServerRequestInterface $request, string $id): array
    {
        AuthContext::jwtUser($request);

        return $this->service->getCurrency($id);
    }

    /** @return array<string, mixed> */
    public function createCurrency(ServerRequestInterface $request): array
    {
        AuthContext::jwtUser($request);

        return $this->service->createCurrency(HttpUtil::parseJson($request));
    }

    /** @return array<string, mixed> */
    public function updateCurrency(ServerRequestInterface $request, string $id): array
    {
        AuthContext::jwtUser($request);

        return $this->service->updateCurrency($id, HttpUtil::parseJson($request));
    }

    /** @return array{ok: true} */
    public function deleteCurrency(ServerRequestInterface $request, string $id): array
    {
        AuthContext::jwtUser($request);
        $this->service->deleteCurrency($id);

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