<?php
session_start();
require_once __DIR__ . "/includes/config.php";

/* ---------- ADMIN LOGIN CHECK ---------- */
if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

/* ---------- VALIDATE INPUT ---------- */
if (!isset($_GET['action'], $_GET['id'])) {
    die("Invalid Request");
}

$action = $_GET['action'];
$user_id = (int)$_GET['id'];

if ($user_id <= 0) {
    die("Invalid User ID");
}

/* ---------- ACTION HANDLER ---------- */
if ($action === 'block') {

    $stmt = $conn->prepare("UPDATE users SET status='blocked' WHERE id=? LIMIT 1");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->close();

    header("Location: users.php?msg=blocked");
    exit();

} elseif ($action === 'unblock') {

    $stmt = $conn->prepare("UPDATE users SET status='active' WHERE id=? LIMIT 1");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->close();

    header("Location: users.php?msg=unblocked");
    exit();

} else {
    die("Invalid Action");
}
