<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI\Validation;

use PpPhp\Core\Platform\DI\Graph\DependencyGraph;
use PpPhp\Core\Platform\DI\Repository\DefinitionRepository;

final class MissingDependencyDetector
{
    public function detect(DependencyGraph $graph, DefinitionRepository $repository): array
    {
        $missing = [];

        foreach ($graph->edges() as $edge) {
            $target = $edge->to();

            if (!$repository->has($target) && !class_exists($target) && !interface_exists($target)) {
                $missing[] = [
                    'from' => $edge->from(),
                    'to' => $target,
                    'reason' => $edge->reason(),
                ];
            }
        }

        return $missing;
    }
}
