<?php
require_once __DIR__ . '/../includes/init.php';

try {
    // Add permissions column if not exists
    $stmt = $pdo->query("SHOW COLUMNS FROM users LIKE 'permissions'");
    if ($stmt->rowCount() == 0) {
        $pdo->exec("ALTER TABLE users ADD COLUMN permissions TEXT DEFAULT NULL");
        echo "Added permissions column.\n";
    }

    // Modify role enum if needed (this is harder to check, but we can just run ALTER IGNORE or similar, but MySQL doesn't like that for ENUMs easily without full definition)
    // Let's just try to modify it.
    $pdo->exec("ALTER TABLE users MODIFY COLUMN role ENUM('admin', 'student', 'teacher') NOT NULL DEFAULT 'student'");
    echo "Updated role enum.\n";

    // Create student_activity_log
    $pdo->exec("
    CREATE TABLE IF NOT EXISTS student_activity_log (
        log_id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        activity VARCHAR(255) NOT NULL,
        details TEXT DEFAULT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        CONSTRAINT fk_log_user FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
    ) ENGINE=InnoDB;
    ");
    echo "Created student_activity_log.\n";

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>