<?php

declare(strict_types=1);

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

use App\Framework\Core\ValueObjects\ClassName;
use App\Framework\Reflection\CachedReflectionProvider;

echo "=== Debug VisitClass Method ===" . PHP_EOL;

// Test the specific class
$className = ClassName::create('App\\Framework\\Mcp\\Console\\McpServerCommand');
echo "Testing class: " . $className->getFullyQualified() . PHP_EOL;

// Test if class exists
if (! $className->exists()) {
    echo "❌ Class does not exist!" . PHP_EOL;
    exit(1);
}

echo "✅ Class exists" . PHP_EOL;

// Test reflection provider
$reflectionProvider = new CachedReflectionProvider();

try {
    $reflection = $reflectionProvider->getClass($className);
    echo "✅ Reflection successful" . PHP_EOL;
    echo "Class name: " . $reflection->getName() . PHP_EOL;

    // Check methods
    $methods = $reflection->getMethods();
    echo "Methods found: " . count($methods) . PHP_EOL;

    foreach ($methods as $method) {
        echo "  Method: " . $method->getName() . PHP_EOL;

        $attributes = $method->getAttributes();
        echo "    Attributes: " . count($attributes) . PHP_EOL;

        foreach ($attributes as $attribute) {
            echo "      - " . $attribute->getName() . PHP_EOL;

            // Try to create instance
            try {
                $instance = $attribute->newInstance();
                if ($instance instanceof App\Framework\Console\ConsoleCommand) {
                    echo "      ✅ ConsoleCommand instance created!" . PHP_EOL;
                    echo "        Name: " . $instance->name . PHP_EOL;
                    echo "        Description: " . $instance->description . PHP_EOL;
                } else {
                    echo "      - Not a ConsoleCommand: " . get_class($instance) . PHP_EOL;
                }
            } catch (Exception $e) {
                echo "      ❌ Error creating instance: " . $e->getMessage() . PHP_EOL;
            }
        }
    }

    // Now let's see what happens with the ConsoleCommandMapper
    $mapper = new App\Framework\Console\ConsoleCommandMapper();
    echo "✅ ConsoleCommandMapper created" . PHP_EOL;
    echo "Target attribute class: " . $mapper->getAttributeClass() . PHP_EOL;

    // Test mapping for each method
    foreach ($methods as $method) {
        $attributes = $method->getAttributes();
        foreach ($attributes as $attribute) {
            if ($attribute->getName() === $mapper->getAttributeClass()) {
                echo "Found matching attribute on method: " . $method->getName() . PHP_EOL;

                $instance = $attribute->newInstance();
                $mapped = $mapper->map($method, $instance);

                if ($mapped) {
                    echo "✅ Mapping successful:" . PHP_EOL;
                    echo "  " . json_encode($mapped, JSON_PRETTY_PRINT) . PHP_EOL;
                } else {
                    echo "❌ Mapping returned null" . PHP_EOL;
                }
            }
        }
    }

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