<?php

declare(strict_types=1);

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

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

// Bootstrap the application to get discovery results
$basePath = dirname(__DIR__, 2);
$clock = new SystemClock();
$highResClock = new SystemHighResolutionClock();
$memoryMonitor = new MemoryMonitor();
$collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: true);
$bootstrapper = new AppBootstrapper($basePath, $collector, $memoryMonitor);
$app = $bootstrapper->bootstrapWeb();
$container = $app->getContainer();

try {
    // Get the discovery registry
    $discoveryRegistry = $container->resolve(DiscoveryRegistry::class);

    echo "=== Discovery Registry Analysis ===\n";

    // Check total Route attributes discovered
    $routeCount = $discoveryRegistry->attributes->getCount(Route::class);
    echo "Total Route attributes found: $routeCount\n\n";

    if ($routeCount > 0) {
        $routes = $discoveryRegistry->attributes->get(Route::class);

        echo "=== Route Details ===\n";
        foreach ($routes as $index => $discoveredRoute) {
            $filePath = $discoveredRoute->filePath->normalized ?? 'unknown';
            $className = $discoveredRoute->additionalData['class'] ?? 'unknown';
            $method = $discoveredRoute->additionalData['method'] ?? 'unknown';
            $path = $discoveredRoute->additionalData['path'] ?? 'unknown';
            $httpMethod = $discoveredRoute->additionalData['http_method'] ?? 'unknown';

            echo sprintf("Route %d:\n", $index + 1);
            echo "  File: $filePath\n";
            echo "  Class: $className\n";
            echo "  Method: $method\n";
            echo "  Path: $path\n";
            echo "  HTTP Method: $httpMethod\n";

            // Check if this is the ShowImage route
            if (str_contains($filePath, 'ShowImage.php')) {
                echo "  *** THIS IS THE SHOWIMAGE ROUTE ***\n";
            }
            echo "\n";
        }

        // Search specifically for ShowImage
        $showImageRoutes = array_filter($routes, function ($route) {
            return str_contains($route->filePath->normalized ?? '', 'ShowImage.php');
        });

        echo "=== ShowImage Routes Found ===\n";
        echo "Count: " . count($showImageRoutes) . "\n";

        if (count($showImageRoutes) > 0) {
            foreach ($showImageRoutes as $route) {
                echo "Found ShowImage route:\n";
                echo "  Path: " . ($route->additionalData['path'] ?? 'unknown') . "\n";
                echo "  Method: " . ($route->additionalData['http_method'] ?? 'unknown') . "\n";
                echo "  Class: " . ($route->additionalData['class'] ?? 'unknown') . "\n";
            }
        } else {
            echo "No ShowImage routes found in discovery!\n";
        }

    } else {
        echo "No routes found in discovery registry!\n";
    }

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