<?php
function run_sql_file($file) {
    include 'db.php'; // Adjust path if needed
    mysqli_report(MYSQLI_REPORT_OFF); // Suppress exceptions, handle errors manually
    $sql = file_get_contents($file);
    $statements = array_filter(array_map('trim', explode(';', $sql)));
    foreach ($statements as $statement) {
        if (empty($statement)) continue;
        $result = $conn->query($statement);
        if ($result === false) {
            $error = $conn->error;
            // Skip already exists/duplicate errors
            if (preg_match('/Duplicate column|already exists|Duplicate entry|column exists|Cannot add foreign key constraint/i', $error)) {
                echo "<div class='alert alert-warning mb-2'>⏭️ Skipped: <code>" . htmlspecialchars($statement) . "</code><br><small>Reason: $error</small></div>\n";
                continue;
            } else {
                echo "<div class='alert alert-danger mb-2'>❌ Error: <code>" . htmlspecialchars($statement) . "</code><br><small>Reason: $error</small></div>\n";
            }
        } else {
            echo "<div class='alert alert-success mb-2'>✅ Success: <code>" . htmlspecialchars($statement) . "</code></div>\n";
        }
    }
    $conn->close();
}
