<?php

declare(strict_types=1);

namespace ApiModel\Action;

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

final class TaxRateAction
{
    public function __construct(private readonly TaxRateService $service) {}

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

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

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

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

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

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

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

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

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

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