<?php
/**
 * Simple test to verify migration files and structure
 */

echo "Simple API Module Migration Test\n";
echo "=================================\n\n";

// Test 1: Check if migration files exist
echo "Test 1: Checking migration files...\n";
$migration_files = glob('migrations/*_version_*.php');

if (!empty($migration_files)) {
    echo "✅ Found " . count($migration_files) . " migration files:\n";

    $versions = [];
    foreach ($migration_files as $file) {
        $filename = basename($file);
        if (preg_match('/(\d+)_version_(\d+)\.php$/', $filename, $matches)) {
            $version = (int) $matches[1];
            $versions[] = $version;
            echo "   - Version $version: $filename\n";
        }
    }

    // Check version sequence
    sort($versions);
    $expected_versions = range(min($versions), max($versions));
    $missing_versions = array_diff($expected_versions, $versions);

    if (empty($missing_versions)) {
        echo "✅ All versions are sequential\n";
    } else {
        echo "⚠️  Missing versions: " . implode(', ', $missing_versions) . "\n";
    }

} else {
    echo "❌ No migration files found in migrations/ directory\n";
}

// Test 2: Check migration runner files
echo "\nTest 2: Checking migration runner files...\n";
$runner_files = [
    'migration_runner.php' => 'Advanced migration runner',
    'run_migrations.php' => 'Basic migration runner',
    'migration_web_interface.php' => 'Web interface',
    'migration_template.php' => 'Migration template'
];

foreach ($runner_files as $file => $description) {
    if (file_exists($file)) {
        echo "✅ $description: $file\n";
    } else {
        echo "❌ Missing: $file ($description)\n";
    }
}

// Test 3: Check migration template
echo "\nTest 3: Checking migration template...\n";
if (file_exists('migration_template.php')) {
    $template_content = file_get_contents('migration_template.php');

    $required_elements = [
        'Migration_Version_{VERSION}',
        'extends App_module_migration',
        'public function up()',
        'public function down()',
        'db_prefix()'
    ];

    $missing_elements = [];
    foreach ($required_elements as $element) {
        if (strpos($template_content, $element) === false) {
            $missing_elements[] = $element;
        }
    }

    if (empty($missing_elements)) {
        echo "✅ Migration template is complete\n";
    } else {
        echo "⚠️  Migration template missing: " . implode(', ', $missing_elements) . "\n";
    }
} else {
    echo "❌ Migration template not found\n";
}

// Test 4: Check documentation
echo "\nTest 4: Checking documentation...\n";
$doc_files = [
    'MIGRATION_README.md' => 'Migration documentation',
    'API_LICENSE_BYPASS_README.md' => 'License bypass documentation'
];

foreach ($doc_files as $file => $description) {
    if (file_exists($file)) {
        echo "✅ $description: $file\n";
    } else {
        echo "❌ Missing: $file ($description)\n";
    }
}

// Test 5: Validate a sample migration file
echo "\nTest 5: Validating sample migration...\n";
$sample_file = 'migrations/211_version_211.php'; // Most recent migration

if (file_exists($sample_file)) {
    $content = file_get_contents($sample_file);

    $validation_checks = [
        'class Migration_Version_211' => 'Correct class name',
        'extends App_module_migration' => 'Proper inheritance',
        'public function up()' => 'Up method exists',
        'public function down()' => 'Down method exists',
        'db_prefix()' => 'Uses db_prefix function',
        '$this->db->query' => 'Uses database queries'
    ];

    $failed_checks = [];
    foreach ($validation_checks as $check => $description) {
        if (strpos($content, $check) === false) {
            $failed_checks[] = $description;
        }
    }

    if (empty($failed_checks)) {
        echo "✅ Sample migration file is valid\n";
    } else {
        echo "⚠️  Sample migration missing: " . implode(', ', $failed_checks) . "\n";
    }
} else {
    echo "❌ Sample migration file not found\n";
}

// Summary
echo "\n📊 Migration System Validation Summary\n";
echo "=======================================\n";

$checks = [
    'Migration files exist' => !empty($migration_files),
    'Migration runners available' => file_exists('migration_runner.php'),
    'Web interface available' => file_exists('migration_web_interface.php'),
    'Template available' => file_exists('migration_template.php'),
    'Documentation complete' => file_exists('MIGRATION_README.md'),
    'Sample migration valid' => file_exists($sample_file)
];

$passed = 0;
$total = count($checks);

foreach ($checks as $check => $result) {
    echo ($result ? '✅' : '❌') . " $check\n";
    if ($result) $passed++;
}

echo "\n📈 Score: $passed/$total checks passed\n";

if ($passed === $total) {
    echo "\n🎉 Migration system is fully set up and ready to use!\n\n";
    echo "Usage instructions:\n";
    echo "1. Web interface: migration_web_interface.php\n";
    echo "2. CLI: php migration_runner.php\n";
    echo "3. Basic: php run_migrations.php\n";
} else {
    echo "\n⚠️  Some components are missing. Please check the errors above.\n";
}
?>