<?php

declare(strict_types=1);

namespace ApiModel\Action;

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

final class ExchangeRateAction
{
    public function __construct(private readonly ExchangeRateService $service) {}

    /** @return list<array<string, mixed>> */
    public function listExchangeRates(ServerRequestInterface $request): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);

        return $this->service->listExchangeRates($ctx['company_id']);
    }

    /** @return array<string, mixed> */
    public function getExchangeRate(ServerRequestInterface $request, string $id): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);

        return $this->service->getExchangeRate($ctx['company_id'], $id);
    }

    /** @return array<string, mixed> */
    public function saveExchangeRate(ServerRequestInterface $request): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);

        return $this->service->saveExchangeRate($ctx['company_id'], HttpUtil::parseJson($request));
    }

    /** @return array{ok: true} */
    public function deleteExchangeRate(ServerRequestInterface $request, string $id): array
    {
        AuthContext::jwtUser($request);
        $ctx = CompanyContext::require($request);
        $this->service->deleteExchangeRate($ctx['company_id'], $id);

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