<?php

declare(strict_types=1);

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

use App\Framework\Core\AppBootstrapper;
use App\Framework\Router\CompiledRoutes;
use App\Framework\Performance\EnhancedPerformanceCollector;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\SystemHighResolutionClock;
use App\Framework\Performance\MemoryMonitor;

echo "=== Analytics Route Discovery Debug ===\n\n";

// Bootstrap the container
echo "1. Bootstrapping container...\n";
$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);
$container = $bootstrapper->bootstrapWorker();

// Get CompiledRoutes
echo "2. Getting compiled routes...\n";
$compiledRoutes = $container->get(CompiledRoutes::class);

// Get all routes
$staticRoutes = $compiledRoutes->getStaticRoutes();
$dynamicRoutes = $compiledRoutes->getDynamicRoutes();

echo "   Static routes: " . count($staticRoutes) . "\n";
echo "   Dynamic routes: " . count($dynamicRoutes) . "\n\n";

// Search for analytics/dashboard routes
echo "3. Searching for dashboard/analytics routes:\n";
$found = [];

// Search static routes
foreach ($staticRoutes as $path => $methodMap) {
    if (str_contains($path, 'dashboard') || str_contains($path, 'analytics')) {
        foreach ($methodMap as $method => $handler) {
            $found[] = [
                'type' => 'STATIC',
                'method' => $method,
                'path' => $path,
                'handler' => get_class($handler)
            ];
        }
    }
}

// Search dynamic routes
foreach ($dynamicRoutes as $route) {
    $path = $route['path'] ?? $route['pattern'] ?? 'N/A';
    if (str_contains($path, 'dashboard') || str_contains($path, 'analytics')) {
        $method = $route['method'] ?? ($route['methods'][0] ?? 'N/A');
        $found[] = [
            'type' => 'DYNAMIC',
            'method' => $method,
            'path' => $path,
            'handler' => $route['class'] ?? 'N/A'
        ];
    }
}

if (empty($found)) {
    echo "   ❌ No dashboard/analytics routes found!\n\n";
} else {
    echo "   ✅ Found " . count($found) . " routes:\n";
    foreach ($found as $route) {
        echo sprintf(
            "      [%s] %s %s -> %s\n",
            $route['type'],
            $route['method'],
            $route['path'],
            $route['handler']
        );
    }
    echo "\n";
}

// Check if AnalyticsDashboard class exists
echo "4. Class existence check:\n";
$className = 'App\\Application\\SmartLink\\Dashboard\\AnalyticsDashboard';
echo "   Class: $className\n";
echo "   Exists: " . (class_exists($className) ? '✅ YES' : '❌ NO') . "\n\n";

// Check if class is in any route
echo "5. Searching all routes for AnalyticsDashboard:\n";
$analyticsDashboardFound = false;

foreach ($staticRoutes as $path => $methodMap) {
    foreach ($methodMap as $method => $handler) {
        if (get_class($handler) === $className) {
            $analyticsDashboardFound = true;
            echo "   ✅ Found in static routes: [$method] $path\n";
        }
    }
}

foreach ($dynamicRoutes as $route) {
    if (isset($route['class']) && $route['class'] === $className) {
        $analyticsDashboardFound = true;
        $method = $route['method'] ?? 'N/A';
        $path = $route['path'] ?? $route['pattern'] ?? 'N/A';
        echo "   ✅ Found in dynamic routes: [$method] $path\n";
    }
}

if (!$analyticsDashboardFound) {
    echo "   ❌ AnalyticsDashboard NOT found in any compiled routes!\n\n";
}

// Check the Route attribute on the class
echo "6. Checking Route attribute on AnalyticsDashboard:\n";
$reflection = new ReflectionClass($className);
$method = $reflection->getMethod('__invoke');
$attributes = $method->getAttributes(\App\Framework\Attributes\Route::class);

if (empty($attributes)) {
    echo "   ❌ No Route attributes found on __invoke method!\n";
} else {
    echo "   ✅ Found " . count($attributes) . " Route attribute(s):\n";
    foreach ($attributes as $attr) {
        $route = $attr->newInstance();
        echo "      Path: " . $route->path . "\n";
        echo "      Method: " . $route->method->value . "\n";
    }
}

echo "\n=== End Debug ===\n";
