<?php

declare(strict_types=1);

require_once __DIR__ . '/../../vendor/autoload.php';

use App\Framework\Core\ValueObjects\ClassName;
use App\Framework\Core\ValueObjects\MethodName;
use App\Framework\DI\ValueObjects\DependencyGraphNode;

echo "=== Testing DependencyGraphNode Value Object ===\n\n";

try {
    // Test basic node creation
    $node = new DependencyGraphNode(
        returnType: 'App\\Service\\DatabaseService',
        className: ClassName::create('App\\Config\\DatabaseInitializer'),
        methodName: MethodName::create('createDatabaseService'),
        dependencies: ['App\\Database\\Connection', 'App\\Logging\\Logger']
    );

    echo "✅ Node created successfully:\n";
    echo "   " . $node->toString() . "\n\n";

    // Test methods
    echo "=== Testing Node Methods ===\n";
    echo "Return Type: " . $node->returnType . "\n";
    echo "Class Name: " . $node->getClassName() . "\n";
    echo "Method Name: " . $node->getMethodName() . "\n";
    echo "Has Dependencies: " . ($node->hasDependencies() ? 'Yes' : 'No') . "\n";
    echo "Depends on Logger: " . ($node->dependsOn('App\\Logging\\Logger') ? 'Yes' : 'No') . "\n";
    echo "Depends on Redis: " . ($node->dependsOn('App\\Cache\\Redis') ? 'Yes' : 'No') . "\n\n";

    // Test array conversion
    echo "=== Testing Array Conversion ===\n";
    $arrayData = $node->toArray();
    echo "Array format:\n";
    print_r($arrayData);

    // Test from array creation
    echo "\n=== Testing From Array Creation ===\n";
    $nodeFromArray = DependencyGraphNode::fromArray('App\\Service\\DatabaseService', $arrayData);
    echo "Recreated from array: " . $nodeFromArray->toString() . "\n";
    echo "Equal to original: " . ($node->toString() === $nodeFromArray->toString() ? 'Yes' : 'No') . "\n\n";

    // Test with additional dependencies
    echo "=== Testing Additional Dependencies ===\n";
    $nodeWithExtraDeps = $node->withDependencies(['App\\Cache\\Redis', 'App\\Events\\EventDispatcher']);
    echo "Original dependencies: " . count($node->dependencies) . "\n";
    echo "New dependencies: " . count($nodeWithExtraDeps->dependencies) . "\n";
    echo "New node toString: " . $nodeWithExtraDeps->toString() . "\n\n";

    // Test debug info
    echo "=== Testing Debug Info ===\n";
    $debugInfo = $node->getDebugInfo();
    print_r($debugInfo);

    echo "\n✅ All DependencyGraphNode tests passed!\n";

} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
    echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
}
