<?php

namespace App\Services\Calculation;

class CalculationPipeline
{
    protected array $steps = [];

    public function addStep(CalculationStep $step): static
    {
        $this->steps[] = $step;
        return $this;
    }

    public function execute(array $initialData): CalculationResult
    {
        $data = $initialData;
        $result = new CalculationResult();

        foreach ($this->steps as $step) {
            $stepResult = $step->execute($data);
            $result->addStep($stepResult);
            $data[$step->outputKey()] = $stepResult['rounded_value'];
        }

        return $result;
    }

    public function getSteps(): array
    {
        return $this->steps;
    }
}
