<?php

declare(strict_types=1);

/**
 * Bootstrap Discovery System
 *
 * This script runs the discovery scanners and stores results
 * Run this ONCE to initialize the new discovery system
 */

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

use App\Framework\BuildTime\Discovery\Scanners\AttributeScanner;
use App\Framework\BuildTime\Discovery\Scanners\InterfaceScanner;
use App\Framework\BuildTime\Discovery\Scanners\TemplateScanner;
use App\Framework\Core\PathProvider;
use App\Framework\Discovery\Storage\DiscoveryStorageService;
use App\Framework\Filesystem\FileScanner;
use App\Framework\Filesystem\FileSystemService;
use App\Framework\Logging\NullLogger;
use App\Framework\Reflection\CachedReflectionProvider;

echo "🚀 Bootstrapping Discovery System...\n\n";
$totalStart = microtime(true);

// Create dependencies
$basePath = __DIR__ . '/../..';
$pathProvider = new PathProvider($basePath);
$storage = new DiscoveryStorageService($pathProvider);
$fileSystemService = new FileSystemService();
$logger = null;
$fileScanner = new FileScanner($logger, null, $fileSystemService);
$reflectionProvider = new CachedReflectionProvider();

// 1. Discover Attributes
echo "📦 Discovering attributes...\n";
$attrStart = microtime(true);
$attributeScanner = new AttributeScanner($fileScanner, $reflectionProvider);
$paths = [$pathProvider->getSourcePath()];
$attributeRegistry = $attributeScanner->scan($paths);
$storage->storeAttributes($attributeRegistry);
$attrDuration = round((microtime(true) - $attrStart) * 1000, 2);
echo "  ✅ {$attributeRegistry->count()} attributes in {$attrDuration}ms\n\n";

// 2. Discover Templates
echo "📄 Discovering templates...\n";
$tplStart = microtime(true);
$templateScanner = new TemplateScanner($fileScanner);
$templatePaths = [
    $pathProvider->getSourcePath(),
    $pathProvider->getBasePath() . '/resources'
];
$templateRegistry = $templateScanner->scan($templatePaths);
$storage->storeTemplates($templateRegistry);
$tplDuration = round((microtime(true) - $tplStart) * 1000, 2);
echo "  ✅ " . count($templateRegistry->getAll()) . " templates in {$tplDuration}ms\n\n";

// 3. Discover Interfaces
echo "🔌 Discovering interface implementations...\n";
$intStart = microtime(true);
$interfaceScanner = new InterfaceScanner($fileScanner, $reflectionProvider, []);
$interfaceRegistry = $interfaceScanner->scan($paths);
$storage->storeInterfaces($interfaceRegistry);
$intDuration = round((microtime(true) - $intStart) * 1000, 2);
echo "  ✅ {$interfaceRegistry->count()} implementations in {$intDuration}ms\n\n";

// Summary
$totalDuration = round((microtime(true) - $totalStart) * 1000, 2);
echo str_repeat("=", 60) . "\n";
echo "🎉 Discovery bootstrap complete in {$totalDuration}ms\n";
echo "   📁 Stored in: storage/discovery/\n";
echo str_repeat("=", 60) . "\n";
