<?php
// migrate_images.php - Untuk migrasi ke server baru
require_once 'includes/auth.php';
require_once 'includes/config.php';

if (!isAdmin()) {
    die('Unauthorized');
}

echo "<h2>🔄 Image URL Migration Tool</h2>";

$old_base_url = 'http://10.52.0.63/soca-osce-system'; // URL lama
$new_base_url = getBaseUrl(); // URL baru (otomatis)

echo "<p>Migrating from: <code>$old_base_url</code></p>";
echo "<p>Migrating to: <code>$new_base_url</code></p>";

if ($old_base_url === $new_base_url) {
    echo "<div style='background: #d4edda; padding: 15px;'>✅ No migration needed - URLs are already correct</div>";
    exit;
}

// Proses migrasi...
$tables = [
    'aspek_penilaian' => ['nama_aspek', 'deskripsi_0', 'deskripsi_1', 'deskripsi_2'],
    'stations' => ['deskripsi', 'instruksi_peserta', 'instruksi_penguji']
];

$total_updated = 0;

foreach ($tables as $table => $columns) {
    foreach ($columns as $column) {
        $stmt = $pdo->prepare("UPDATE $table SET $column = REPLACE($column, ?, ?)");
        $stmt->execute([$old_base_url, $new_base_url]);
        $total_updated += $stmt->rowCount();
    }
}

echo "<div style='background: #d4edda; padding: 15px;'>";
echo "✅ Migration completed! Updated $total_updated records";
echo "</div>";
?>