<?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 Symfony\Component\HttpFoundation\Response;
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.
 */
final class SiretProvider implements ProviderInterface
{
    public function __construct(
        private readonly SiretUtils $siretUtils,
    ) {
    }

    /**
     * @param mixed[] $uriVariables
     * @param mixed[] $context
     *
     * @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', Response::HTTP_METHOD_NOT_ALLOWED);
        }

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

        return $siretResponse;
    }
}
