<?php

declare(strict_types=1);

/**
 * PHPUnit Upgrade Validation Script
 * Validates that the PHPUnit 12.3 upgrade was successful
 */

echo "=== PHPUnit Upgrade Validation ===" . PHP_EOL;
echo "Date: " . date('Y-m-d H:i:s') . PHP_EOL;
echo "PHP Version: " . PHP_VERSION . PHP_EOL . PHP_EOL;

// Check Composer dependencies
echo "📦 Checking Dependencies..." . PHP_EOL;

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
    echo "✅ Vendor directory found" . PHP_EOL;
    require_once __DIR__ . '/../vendor/autoload.php';
    
    // Check PHPUnit version
    if (class_exists('PHPUnit\Runner\Version')) {
        $version = PHPUnit\Runner\Version::id();
        echo "✅ PHPUnit Version: $version" . PHP_EOL;
        
        if (version_compare($version, '12.0', '>=')) {
            echo "✅ PHPUnit 12+ confirmed" . PHP_EOL;
        } else {
            echo "❌ PHPUnit version too old" . PHP_EOL;
        }
    } else {
        echo "❌ PHPUnit\Runner\Version class not found" . PHP_EOL;
    }
    
} else {
    echo "❌ Vendor autoload not found - run 'composer install'" . PHP_EOL;
}

echo PHP_EOL . "🧪 Testing Framework Configuration..." . PHP_EOL;

// Check configuration files
$configFiles = [
    '../phpunit.xml' => 'PHPUnit Configuration',
    'bootstrap.php' => 'Test Bootstrap'
];

foreach ($configFiles as $file => $description) {
    $fullPath = __DIR__ . '/' . $file;
    if (file_exists($fullPath)) {
        echo "✅ $description found" . PHP_EOL;
        
        // Check schema version in phpunit.xml
        if ($file === '../phpunit.xml') {
            $content = file_get_contents($fullPath);
            if (strpos($content, '12.3/phpunit.xsd') !== false) {
                echo "✅ Schema version 12.3 confirmed" . PHP_EOL;
            } else {
                echo "⚠️  Schema version might need updating" . PHP_EOL;
            }
        }
    } else {
        echo "❌ $description missing: $fullPath" . PHP_EOL;
    }
}

echo PHP_EOL . "🔍 Checking Test Files..." . PHP_EOL;

$testFiles = glob(__DIR__ . '/*Test.php');
$validTests = 0;
$totalTests = count($testFiles);

foreach ($testFiles as $testFile) {
    $fileName = basename($testFile);
    
    // Check syntax
    $output = [];
    $returnVar = 0;
    exec("php -l \"$testFile\" 2>&1", $output, $returnVar);
    
    if ($returnVar === 0) {
        echo "✅ $fileName - syntax OK" . PHP_EOL;
        $validTests++;
        
        // Check for namespace
        $content = file_get_contents($testFile);
        if (strpos($content, 'namespace DeskMoloni\\Tests;') !== false) {
            echo "   ✅ PSR-4 namespace found" . PHP_EOL;
        } else {
            echo "   ⚠️  PSR-4 namespace missing" . PHP_EOL;
        }
        
        if (strpos($content, 'declare(strict_types=1);') !== false) {
            echo "   ✅ Strict types enabled" . PHP_EOL;
        } else {
            echo "   ⚠️  Strict types not enabled" . PHP_EOL;
        }
        
    } else {
        echo "❌ $fileName - syntax errors" . PHP_EOL;
        echo "   " . implode("\n   ", $output) . PHP_EOL;
    }
}

echo PHP_EOL . "📋 Validation Summary" . PHP_EOL;
echo "===================" . PHP_EOL;
echo "Test Files: $validTests/$totalTests valid" . PHP_EOL;

// Check required PHP extensions for PHPUnit 12
echo PHP_EOL . "🔧 PHP Extensions Check..." . PHP_EOL;
$requiredExtensions = ['dom', 'json', 'libxml', 'mbstring', 'tokenizer', 'xml', 'xmlwriter'];
$missingExtensions = [];

foreach ($requiredExtensions as $ext) {
    if (extension_loaded($ext)) {
        echo "✅ $ext extension loaded" . PHP_EOL;
    } else {
        echo "❌ $ext extension MISSING" . PHP_EOL;
        $missingExtensions[] = $ext;
    }
}

if (empty($missingExtensions)) {
    echo PHP_EOL . "🎉 All checks passed! PHPUnit 12.3 upgrade is complete and ready." . PHP_EOL;
    echo "You can run tests with: vendor/bin/phpunit" . PHP_EOL;
} else {
    echo PHP_EOL . "⚠️  Upgrade complete but missing extensions prevent execution." . PHP_EOL;
    echo "Install missing extensions: " . implode(', ', $missingExtensions) . PHP_EOL;
    echo "Command: sudo apt-get install php8.3-" . implode(' php8.3-', $missingExtensions) . PHP_EOL;
}

echo PHP_EOL . "=== Validation Complete ===" . PHP_EOL;