<?php

declare(strict_types=1);

namespace App\State\Provider\Dolibarr;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResources\Dolibarr\DolibarrAccount;
use App\Service\ApiResourceBuilder\Dolibarr\DolibarrAccountBuilder;
use Symfony\Component\HttpFoundation\Response;

/**
 * Custom provider pour les DolibarrAccounts récupérés via l'api dolibarr.
 */
final class DolibarrAccountProvider implements ProviderInterface
{
    public function __construct(
        private DolibarrAccountBuilder $dolibarrAccountCreator,
    ) {
    }

    /**
     * @param mixed[] $uriVariables
     * @param mixed[] $context
     */
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): DolibarrAccount
    {
        if ($operation instanceof GetCollection) {
            throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
        }

        return $this->dolibarrAccountCreator->getDolibarrAccount($uriVariables['organizationId']);
    }
}
