<?php
declare(strict_types=1);

namespace App\State\Provider\Utils;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResources\Utils\Siret;
use App\Service\Utils\Siret as SiretUtils;
use RuntimeException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

/**
 * Class SiretProvider : custom provider pour assurer l'alimentation des utils Siret
 * @package App\DataProvider\Utils
 */
final class SiretProvider implements ProviderInterface
{
    public function __construct(
        private SiretUtils $siretUtils
    )
    { }

    /**
     * @param Operation $operation
     * @param mixed[] $uriVariables
     * @param mixed[] $context
     * @return Siret|null
     * @throws ClientExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ServerExceptionInterface
     * @throws TransportExceptionInterface
     */
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?Siret
    {
        if($operation instanceof GetCollection) {
            throw new RuntimeException('not supported', 500);
        }

        $id = $uriVariables['id'];
        $siretResponse = new Siret();
        $siretResponse->setNumber($id);
        $siretResponse->setIsCorrect($this->siretUtils->isSiretCorrect($id));
        return $siretResponse;
    }
}
