<?php

namespace App\Provisioning;

use App\Provisioning\Contracts\ProvisioningStep;
use InvalidArgumentException;

/**
 * Maps a pipeline name (host | customer) to its ordered list of step classes.
 */
class PipelineRegistry
{
    /** @param array<string, array<int, class-string<ProvisioningStep>>> $pipelines */
    public function __construct(private array $pipelines) {}

    /** @return array<int, class-string<ProvisioningStep>> */
    public function stepsFor(string $pipeline): array
    {
        if (! isset($this->pipelines[$pipeline])) {
            throw new InvalidArgumentException("Unknown pipeline [{$pipeline}].");
        }

        return $this->pipelines[$pipeline];
    }

    public function count(string $pipeline): int
    {
        return count($this->stepsFor($pipeline));
    }

    public function resolve(string $pipeline, int $index): ProvisioningStep
    {
        $steps = $this->stepsFor($pipeline);

        if (! isset($steps[$index])) {
            throw new InvalidArgumentException("No step at index {$index} for pipeline [{$pipeline}].");
        }

        return app($steps[$index]);
    }
}
