<?php

declare(strict_types=1);

namespace App\Domain\Shared\Infrastructure\ApiPlatform\State\Provider;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Domain\Shared\Application\Query\GetJobById;
use App\Domain\Shared\Application\QueryHandler\GetJobByIdHandler;
use App\Domain\Shared\Domain\Exception\JobNotFoundException;
use App\Domain\Shared\Infrastructure\ApiPlatform\Resource\GetJobListResource;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

readonly class GetJobStateProvider implements ProviderInterface
{
    public function __construct(
        private GetJobByIdHandler $handler
    ) {
    }

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): GetJobListResource
    {
        try {
            $response = $this->handler->handle(new GetJobById($uriVariables['id']));
        } catch (JobNotFoundException $e) {
            throw new NotFoundHttpException($e->getMessage(), $e);
        }

        return GetJobListResource::fromJobResponse($response);
    }
}
