#!/usr/bin/env php
<?php
/**
 * Post-ERG Test Runner
 * Runs all tests in the tests/ directory
 */

// Tests always run against the test database; require an explicit opt-in so
// that a stray test.db on disk never silently redirects a production session.
putenv('DB_ENV=test');

echo "╔════════════════════════════════════════════╗\n";
echo "║         Post-ERG Test Suite                ║\n";
echo "╚════════════════════════════════════════════╝\n\n";

$testFiles = [
    ['name' => 'Database (Unit)', 'path' => __DIR__ . '/Unit/DatabaseTest.php'],
    ['name' => 'Rate Limit (Unit)', 'path' => __DIR__ . '/Unit/RateLimitTest.php'],
    ['name' => 'Search (Integration)', 'path' => __DIR__ . '/Integration/SearchTest.php'],
    ['name' => 'Security', 'path' => __DIR__ . '/Security/SecurityTest.php'],
];

$totalTests = 0;
$passedTests = 0;
$failedTests = 0;
$skippedTests = 0;

foreach ($testFiles as $test) {
    echo "┌─────────────────────────────────────────┐\n";
    echo "│ " . str_pad($test['name'], 41) . "│\n";
    echo "└─────────────────────────────────────────┘\n\n";

    $totalTests++;
    $path = $test['path'];
    $file = basename($path);

    if (!file_exists($path)) {
        echo "⚠️  SKIP: $file (not found)\n\n";
        $skippedTests++;
        continue;
    }

    ob_start();
    $exitCode = 0;
    $testResult = false;

    try {
        $testResult = include $path;
        
        // Check if test returned false or had error indicators in output
        $output = ob_get_contents();
        if ($testResult === false || 
            strpos($output, '❌') !== false ||
            strpos($output, 'FAIL:') !== false) {
            $exitCode = 1;
        }
    } catch (Exception $e) {
        $exitCode = 1;
        echo "❌ EXCEPTION: " . $e->getMessage() . "\n";
    }

    $output = ob_get_clean();
    echo $output;

    if ($exitCode === 0 && $testResult !== false) {
        echo "\n✅ TEST PASSED\n\n";
        $passedTests++;
    } else {
        echo "\n❌ TEST FAILED\n\n";
        $failedTests++;
    }
}

echo "╔════════════════════════════════════════════╗\n";
echo "║              Test Summary                  ║\n";
echo "╠════════════════════════════════════════════╣\n";
echo "║ Total:   " . str_pad($totalTests, 34) . "║\n";
echo "║ Passed:  " . str_pad($passedTests . " ✅", 35) . "║\n";
echo "║ Failed:  " . str_pad($failedTests . ($failedTests > 0 ? " ❌" : ""), 35) . "║\n";
if ($skippedTests > 0) {
    echo "║ Skipped: " . str_pad($skippedTests . " ⚠️", 36) . "║\n";
}
echo "╚════════════════════════════════════════════╝\n\n";

if ($failedTests > 0) {
    echo "❌ Some tests failed!\n";
    exit(1);
} else {
    echo "✅ All tests passed!\n";
    exit(0);
}
