<?php

declare(strict_types=1);

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

use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\GeneralCache;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\SystemClock;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\Factory\DiscoveryServiceFactory;
use App\Framework\Discovery\ValueObjects\DiscoveryConfiguration;
use App\Framework\Serializer\Json\JsonSerializer;

echo "=== Debugging DiscoveryServiceFactory Configuration ===\n\n";

try {
    $projectRoot = dirname(__DIR__, 2);
    $srcPath = $projectRoot . '/src';

    $container = new DefaultContainer();
    $pathProvider = new PathProvider($projectRoot);
    $clock = new SystemClock();
    $cacheDriver = new InMemoryCache();
    $serializer = new JsonSerializer();
    $cache = new GeneralCache($cacheDriver, $serializer);

    // Test 1: Check development configuration
    echo "=== Development Configuration ===\n";
    $devConfig = DiscoveryConfiguration::development();
    echo "Config array: " . json_encode($devConfig->toArray(), JSON_PRETTY_PRINT) . "\n";
    echo "Attribute mappers count: " . count($devConfig->attributeMappers) . "\n";
    echo "Target interfaces count: " . count($devConfig->targetInterfaces) . "\n\n";

    // Test 2: Check factory
    echo "=== Factory Analysis ===\n";
    $factory = new DiscoveryServiceFactory($container, $pathProvider, $cache, $clock);

    // Create service and debug
    $devConfigWithPaths = $devConfig->withPaths([$srcPath]);
    echo "Config with paths - Paths count: " . count($devConfigWithPaths->paths) . "\n";
    echo "Config with paths - Mappers count: " . count($devConfigWithPaths->attributeMappers) . "\n\n";

    // Test 3: Direct reflection on the factory's buildAttributeMappers method
    echo "=== Reflecting Factory's buildAttributeMappers ===\n";
    $reflection = new ReflectionClass($factory);
    $buildMappersMethod = $reflection->getMethod('buildAttributeMappers');
    $buildMappersMethod->setAccessible(true);

    // Call with empty array (what happens in development config)
    $emptyMappers = $buildMappersMethod->invoke($factory, []);
    echo "buildAttributeMappers([]) returns " . count($emptyMappers) . " mappers:\n";
    foreach ($emptyMappers as $mapper) {
        echo "- " . get_class($mapper) . "\n";
    }
    echo "\n";

    // Test 4: Test discovery with this factory
    echo "=== Testing Discovery with Factory ===\n";
    $discoveryService = $factory->createForDevelopment([$srcPath]);

    // Get the actual configuration used
    $reflectionService = new ReflectionClass($discoveryService);
    $configProperty = $reflectionService->getProperty('configuration');
    $configProperty->setAccessible(true);
    $actualConfig = $configProperty->getValue($discoveryService);

    $mappersProperty = $reflectionService->getProperty('attributeMappers');
    $mappersProperty->setAccessible(true);
    $actualMappers = $mappersProperty->getValue($discoveryService);

    echo "UnifiedDiscoveryService configuration:\n";
    echo "- Config paths: " . count($actualConfig->paths) . "\n";
    echo "- Config mappers: " . count($actualConfig->attributeMappers) . "\n";
    echo "- Service mappers: " . count($actualMappers) . "\n";

    echo "\nService mappers:\n";
    foreach ($actualMappers as $mapper) {
        echo "- " . get_class($mapper) . "\n";
    }

    echo "\n=== Running Discovery ===\n";
    $registry = $discoveryService->discover();

    echo "Results:\n";
    echo "- Total: " . count($registry) . "\n";
    echo "- Initializers: " . $registry->attributes->getCount('App\\Framework\\DI\\Initializer') . "\n";

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