<?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;
use App\Framework\DI\Initializer;

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

echo "DEBUG: Starting context analysis\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);
    
    $initializerCount = $registry->attributes->getCount(Initializer::class);
    echo "DEBUG: Found $initializerCount initializer attributes in registry\n";
    
    if ($initializerCount > 0) {
        $initializers = $registry->attributes->get(Initializer::class);
        echo "DEBUG: Looking for router-related initializers\n";
        
        foreach ($initializers as $initializer) {
            $className = $initializer->className->getFullyQualified();
            
            if (str_contains($className, 'Router') || str_contains($className, 'Route')) {
                echo "  ROUTER INITIALIZER: $className\n";
                
                // Get the attribute instance to see the contexts
                $attributeInstance = $initializer->createAttributeInstance();
                if ($attributeInstance instanceof \App\Framework\DI\Initializer) {
                    $contexts = $attributeInstance->contexts;
                    if (empty($contexts)) {
                        echo "    CONTEXTS: ALL (no restrictions)\n";
                    } else {
                        $contextNames = array_map(function($ctx) {
                            return $ctx instanceof \App\Framework\Context\ContextType ? $ctx->name : (string)$ctx;
                        }, $contexts);
                        echo "    CONTEXTS: " . implode(', ', $contextNames) . "\n";
                    }
                }
                
                // Check additionalData for debugging
                if (isset($initializer->additionalData['contexts'])) {
                    echo "    STORED CONTEXTS: " . print_r($initializer->additionalData['contexts'], true);
                }
            }
        }
    } else {
        echo "DEBUG: No initializers found in registry\n";
    }
} else {
    echo "DEBUG: DiscoveryRegistry is NOT available in container\n";
}

echo "DEBUG: Context analysis complete\n";