<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION["role"]) || $_SESSION["role"] !== "superadmin") {
    header("Location: index.php");
    exit();
}

require_once "../php/db_connect.php";

$id = $_GET["id"] ?? null;
$action = $_GET["action"] ?? null;

if (!$id || !is_numeric($id) || !$action) {
    die("Invalid request.");
}

if ($action === "deactivate") {
    $newStatus = "inactive";
    $message = "deactivated";
} elseif ($action === "activate") {
    $newStatus = "active";
    $message = "activated";
} else {
    die("Invalid action.");
}

$stmt = $conn->prepare("UPDATE users SET status = ? WHERE id = ? AND role != 'superadmin'");
$stmt->bind_param("si", $newStatus, $id);

if ($stmt->execute()) {
    header("Location: superadmin.php?page=users&message=" . urlencode($message));
    exit();
} else {
    echo "Update failed: " . $stmt->error;
}
?>