<?php
require_once __DIR__ . '/../../core/bootstrap.php';

try {
    $con = Database::get('main');
    
    // Check if columns already exist to avoid errors
    $check = $con->query("SHOW COLUMNS FROM adminUser LIKE 'status'");
    if ($check->rowCount() == 0) {
        $sql = "ALTER TABLE adminUser
                  ADD COLUMN status ENUM('pending', 'approved', 'suspended', 'rejected') NOT NULL DEFAULT 'pending' AFTER role,
                  ADD COLUMN phone VARCHAR(50) DEFAULT NULL AFTER name,
                  ADD COLUMN email VARCHAR(255) DEFAULT NULL AFTER phone,
                  ADD COLUMN approved_by VARCHAR(64) DEFAULT NULL AFTER status,
                  ADD COLUMN approved_at DATETIME DEFAULT NULL AFTER approved_by";
                  
        $con->exec($sql);
        
        // Update existing admins to approved and super_admin
        $con->exec("UPDATE adminUser SET status = 'approved', role = 'super_admin' WHERE id IS NOT NULL");
        
        echo json_encode(["status" => "success", "message" => "Migration completed successfully."]);
    } else {
        echo json_encode(["status" => "success", "message" => "Columns already exist."]);
    }
} catch (Exception $e) {
    echo json_encode(["status" => "error", "message" => $e->getMessage()]);
}
