<?php

declare(strict_types=1);

use App\Framework\Attributes\Route;
use App\Framework\Core\AppBootstrapper;
use App\Framework\Discovery\Results\DiscoveryRegistry;
use App\Framework\Performance\EnhancedPerformanceCollector;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\SystemHighResolutionClock;
use App\Framework\Performance\MemoryMonitor;

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/src/Framework/Debug/helpers.php';

echo "DEBUG: Starting container debug\n";

// Create dependencies for enhanced performance collector
$basePath = __DIR__;
$clock = new SystemClock();
$highResClock = new SystemHighResolutionClock();
$memoryMonitor = new MemoryMonitor();
$collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: true);

echo "DEBUG: About to create AppBootstrapper\n";
$bootstrapper = new AppBootstrapper($basePath, $collector, $memoryMonitor);

echo "DEBUG: About to bootstrap web app\n";
$app = $bootstrapper->bootstrapWeb();

echo "DEBUG: Web app bootstrapped successfully\n";

// Get container from the app
$reflection = new ReflectionObject($app);
$containerProperty = $reflection->getProperty('container');
$containerProperty->setAccessible(true);
$container = $containerProperty->getValue($app);

echo "DEBUG: Got container from app\n";

// Check if DiscoveryRegistry is available
if ($container->has(DiscoveryRegistry::class)) {
    echo "DEBUG: DiscoveryRegistry is available in container\n";
    $registry = $container->get(DiscoveryRegistry::class);
    
    $routeCount = $registry->attributes->getCount(Route::class);
    echo "DEBUG: Found $routeCount route attributes in registry\n";
    
    if ($routeCount > 0) {
        $routes = $registry->attributes->get(Route::class);
        echo "DEBUG: First few routes:\n";
        foreach (array_slice($routes, 0, 3) as $i => $route) {
            echo "  Route $i: " . $route->className->getFullyQualified() . "::" . $route->methodName?->toString() . "\n";
        }
        
        // Look for the home route specifically
        echo "DEBUG: Looking for home route (/)\n";
        $homeFound = false;
        foreach ($routes as $route) {
            $instance = $route->createAttributeInstance();
            if ($instance instanceof \App\Framework\Attributes\Route && $instance->path === '/') {
                echo "  HOME ROUTE FOUND: " . $route->className->getFullyQualified() . "::" . $route->methodName?->toString() . " -> " . $instance->path . "\n";
                $homeFound = true;
            }
        }
        if (!$homeFound) {
            echo "  HOME ROUTE NOT FOUND in discovery results\n";
        }
    } else {
        echo "DEBUG: No routes found in registry\n";
    }
    
    echo "DEBUG: Total discovery items: " . count($registry) . "\n";
} else {
    echo "DEBUG: DiscoveryRegistry is NOT available in container\n";
}

echo "DEBUG: Debug complete\n";