<?php

declare(strict_types=1);

require __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;

echo "=== PreSaveCampaign Discovery Test ===" . PHP_EOL . PHP_EOL;

// Bootstrap application
$basePath = dirname(__DIR__, 2);
$clock = new SystemClock();
$highResClock = new SystemHighResolutionClock();
$memoryMonitor = new MemoryMonitor();
$collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: false);
$bootstrapper = new AppBootstrapper($basePath, $collector, $memoryMonitor);
$app = $bootstrapper->bootstrapWeb();

// Get Discovery Registry from Container
$reflection = new ReflectionClass($app);
$containerProperty = $reflection->getProperty('container');
$containerProperty->setAccessible(true);
$container = $containerProperty->getValue($app);

/** @var DiscoveryRegistry $registry */
$registry = $container->get(DiscoveryRegistry::class);

echo "Discovery Registry Stats:" . PHP_EOL;
echo "  Total Attributes: " . count($registry->attributes->getAll()) . PHP_EOL;

// Get all Route attributes
$routes = $registry->attributes->get(Route::class);
echo "  Total Routes: " . count($routes) . PHP_EOL . PHP_EOL;

// Search for Campaign routes
echo "Campaign Routes:" . PHP_EOL;
$campaignRoutes = [];

foreach ($routes as $route) {
    $className = $route->className->getFullyQualified();

    if (str_contains($className, 'Campaign')) {
        $campaignRoutes[] = $route;

        // Get route details
        $routeAttr = $route->createAttributeInstance();
        if ($routeAttr instanceof Route) {
            echo "  ✅ Found: {$className}::{$route->methodName}" . PHP_EOL;
            echo "     Path: {$routeAttr->path}" . PHP_EOL;
            echo "     Method: {$routeAttr->method->value}" . PHP_EOL;
        }
    }
}

if (empty($campaignRoutes)) {
    echo "  ❌ NO Campaign routes found in discovery!" . PHP_EOL;
}

echo PHP_EOL;

// Specifically check for PreSaveCampaign
echo "PreSaveCampaign Check:" . PHP_EOL;
$found = false;

foreach ($routes as $route) {
    if ($route->className->getShortName() === 'PreSaveCampaign') {
        $found = true;
        $routeAttr = $route->createAttributeInstance();

        echo "  ✅ PreSaveCampaign found!" . PHP_EOL;
        echo "     Full Class: {$route->className->getFullyQualified()}" . PHP_EOL;
        echo "     Method: {$route->methodName}" . PHP_EOL;

        if ($routeAttr instanceof Route) {
            echo "     Path: {$routeAttr->path}" . PHP_EOL;
            echo "     HTTP Method: {$routeAttr->method->value}" . PHP_EOL;
        }

        break;
    }
}

if (! $found) {
    echo "  ❌ PreSaveCampaign NOT found in discovery!" . PHP_EOL;
    echo PHP_EOL;
    echo "Checking if file exists:" . PHP_EOL;

    $file = $basePath . '/src/Application/Campaign/PreSaveCampaign.php';
    if (file_exists($file)) {
        echo "  ✅ File exists: $file" . PHP_EOL;

        // Check if class can be loaded
        if (class_exists('App\\Application\\Campaign\\PreSaveCampaign')) {
            echo "  ✅ Class can be loaded" . PHP_EOL;

            $reflection = new ReflectionClass('App\\Application\\Campaign\\PreSaveCampaign');
            $attributes = $reflection->getAttributes(Route::class);

            echo "  Found " . count($attributes) . " Route attributes on class" . PHP_EOL;

            foreach ($reflection->getMethods() as $method) {
                $methodAttrs = $method->getAttributes(Route::class);
                if (count($methodAttrs) > 0) {
                    echo "  ✅ Found Route on method: {$method->getName()}" . PHP_EOL;

                    foreach ($methodAttrs as $attr) {
                        $routeInstance = $attr->newInstance();
                        echo "     Path: {$routeInstance->path}" . PHP_EOL;
                    }
                }
            }
        } else {
            echo "  ❌ Class cannot be loaded!" . PHP_EOL;
        }
    } else {
        echo "  ❌ File does NOT exist: $file" . PHP_EOL;
    }
}

echo PHP_EOL . "=== Summary ===" . PHP_EOL;
echo "Discovery funktioniert für Campaign-Routes: " . ($found ? "✅ JA" : "❌ NEIN") . PHP_EOL;
