<?php

declare(strict_types=1);

namespace App\Framework\DI\Exceptions;

use App\Framework\Exception\ExceptionContext;
use App\Framework\Exception\FrameworkException;

final class ParameterResolutionException extends FrameworkException
{
    public function __construct(
        string $paramName,
        string $class,
        array $dependencyChain,
        int $code = 0,
        ?string $reason = null,
        ?\Throwable $previous = null
    ) {

        if (! $reason) {
            $message = "Cannot resolve parameter {$paramName} in $class. Dependency chain: " . implode(' -> ', $dependencyChain);
        } else {
            $message = "Cannot resolve parameter {$paramName} in $class because $reason";
        }

        parent::__construct(
            message: $message,
            context: new ExceptionContext(
                operation: 'dependency_injection',
                component: 'DI Container',
                data: [
                    'paramName' => $paramName,
                    'class' => $class,
                    'dependencyChain' => $dependencyChain,
                ]
            ),
            code: $code,
            previous: $previous
        );
    }
}
