<?php

declare(strict_types=1);

// Simulate web environment
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['SCRIPT_NAME'] = '/index.php';

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

use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\GeneralCache;
use App\Framework\Context\ExecutionContext;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\SystemClock;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Reflection\CachedReflectionProvider;
use App\Framework\Serializer\Json\JsonSerializer;

echo "=== Testing Web Context Initializer Execution ===\n\n";

try {
    $projectRoot = dirname(__DIR__, 2);

    echo "Project root: $projectRoot\n";
    echo "Simulated context: " . ExecutionContext::detect()->getType()->value . "\n\n";

    $container = new DefaultContainer();
    $pathProvider = new PathProvider($projectRoot);
    $clock = new SystemClock();

    // Create cache for discovery
    $cacheDriver = new InMemoryCache();
    $serializer = new JsonSerializer();
    $cache = new GeneralCache($cacheDriver, $serializer);

    // Register required dependencies
    $container->instance(PathProvider::class, $pathProvider);
    $container->instance(\App\Framework\Cache\CacheDriver::class, $cacheDriver);
    $container->instance(\App\Framework\Serializer\Serializer::class, $serializer);
    $container->instance(\App\Framework\Cache\Cache::class, $cache);
    $container->instance(\App\Framework\DateTime\Clock::class, $clock);
    $container->instance(\App\Framework\Reflection\ReflectionProvider::class, new CachedReflectionProvider());

    // Create bootstrapper
    $bootstrapper = new DiscoveryServiceBootstrapper($container, $clock);

    echo "Starting web bootstrap process...\n";

    // This should discover AND execute initializers for web context
    $registry = $bootstrapper->bootstrap();

    echo "\nWeb Bootstrap completed!\n\n";

    // Check what initializers were found
    $initializerAttributeClass = 'App\\Framework\\DI\\Initializer';
    $initializers = $registry->attributes->get($initializerAttributeClass);

    echo "=== Web Context Initializer Results ===\n";
    echo "Initializers found: " . count($initializers) . "\n\n";

    // Check if web-specific services are available
    $webServices = [
        'App\\Framework\\Http\\MiddlewareManagerInterface',
        'App\\Framework\\Http\\Session\\SessionManager',
        'App\\Framework\\Security\\RequestSigning\\RequestSigning',
        'App\\Framework\\Waf\\WafEngine',
        'App\\Framework\\Http\\RequestFactory',
    ];

    echo "=== Web-Specific Services ===\n";
    foreach ($webServices as $service) {
        $exists = $container->has($service);
        echo "- $service: " . ($exists ? "✅ Available" : "❌ Missing") . "\n";
    }

    echo "\n=== Core Services ===\n";
    $coreServices = [
        'App\\Framework\\Cache\\Cache',
        'App\\Framework\\Logging\\Logger',
        'App\\Framework\\Database\\EntityManager',
    ];

    foreach ($coreServices as $service) {
        $exists = $container->has($service);
        echo "- $service: " . ($exists ? "✅ Available" : "❌ Missing") . "\n";
    }

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