<?php

declare(strict_types=1);

namespace App\Shared\Infrastructure\ApiPlatform\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Shared\Domain\Module\ModuleRegistry;
use App\Shared\Infrastructure\ApiPlatform\Resource\ModulesResource;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

final readonly class ModulesProvider implements ProviderInterface
{
    public function __construct(
        #[Autowire('%kernel.project_dir%')]
        private string $projectDir,
    ) {}

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ModulesResource
    {
        /** @var list<class-string> $classes */
        $classes = require $this->projectDir.'/config/modules.php';

        $dto          = new ModulesResource();
        $dto->modules = ModuleRegistry::ids($classes);

        return $dto;
    }
}
