<?php
require_once __DIR__ . '/db.php';

try {
    $db = getDB();

    // Users table
    try {
        $db->exec("ALTER TABLE users ADD COLUMN google_id VARCHAR(255) DEFAULT NULL AFTER email");
        echo "✅ Added google_id to users table\n";
    } catch (PDOException $e) {
        if (strpos($e->getMessage(), 'Duplicate column') !== false) {
            echo "ℹ️ google_id already exists in users\n";
        } else {
            throw $e;
        }
    }

    // Students table
    $columns = [
        'email' => "ALTER TABLE students ADD COLUMN email VARCHAR(255) DEFAULT NULL AFTER name_en",
        'password' => "ALTER TABLE students ADD COLUMN password VARCHAR(255) DEFAULT NULL AFTER email",
        'google_id' => "ALTER TABLE students ADD COLUMN google_id VARCHAR(255) DEFAULT NULL AFTER password",
        'advisor_id' => "ALTER TABLE students ADD COLUMN advisor_id INT DEFAULT NULL AFTER curriculum_id",
        'previous_qualification' => "ALTER TABLE students ADD COLUMN previous_qualification VARCHAR(255) DEFAULT NULL AFTER graduated_institution",
    ];

    foreach ($columns as $name => $sql) {
        try {
            $db->exec($sql);
            echo "✅ Added $name to students table\n";
        } catch (PDOException $e) {
            if (strpos($e->getMessage(), 'Duplicate column') !== false) {
                echo "ℹ️ $name already exists in students\n";
            } else {
                throw $e;
            }
        }
    }

    echo "\n✅ Migration completed successfully!\n";

} catch (Exception $e) {
    die("❌ Error: " . $e->getMessage() . "\n");
}
