<?php

declare(strict_types=1);

namespace App\Shared\Infrastructure\ApiPlatform\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Shared\Infrastructure\ApiPlatform\Resource\ModulesResource;

/**
 * @implements ProviderInterface<object>
 */
class ModulesProvider implements ProviderInterface
{
    /** @var list<string> */
    private readonly array $activeModuleIds;

    public function __construct()
    {
        $configPath    = dirname(__DIR__, 5).'/config/modules.php';
        $moduleClasses = file_exists($configPath) ? require $configPath : [];

        $ids = [];
        foreach ($moduleClasses as $moduleClass) {
            if (defined($moduleClass.'::ID')) {
                $ids[] = $moduleClass::ID;
            }
        }

        $this->activeModuleIds = $ids;
    }

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object
    {
        return new ModulesResource($this->activeModuleIds);
    }
}
