<?php

namespace App\Services\Calculation;

class CalculationResult
{
    protected array $steps = [];
    protected array $values = [];

    public function addStep(array $stepResult): void
    {
        $this->steps[] = $stepResult;
        $this->values[$stepResult['output_key']] = $stepResult['rounded_value'];
    }

    public function get(string $key): ?int
    {
        return $this->values[$key] ?? null;
    }

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

    public function getValues(): array
    {
        return $this->values;
    }

    public function toArray(): array
    {
        return [
            'steps' => $this->steps,
            'final_values' => $this->values,
        ];
    }

    public function toPriceSheet(): array
    {
        $sheet = [];
        foreach ($this->steps as $step) {
            $sheet[] = [
                'description' => $step['name'],
                'value' => $step['rounded_value'],
                'is_overridden' => $step['is_overridden'],
            ];
        }
        return $sheet;
    }
}
