<?php
require_once "config.php";

$type = $_POST['type'] ?? null;

/* =======================
   TYPE : CHANGE (UNBAN)
   ======================= */
if ($type === "change") {

    if (!isset($_POST['id'])) {
        echo "ID Missing";
        exit;
    }

    $id = intval($_POST['id']);

    $stmt = $conn->prepare(
        "UPDATE device_ban SET ban_status = 0 WHERE id = ?"
    );
    $stmt->bind_param("i", $id);

    if ($stmt->execute() && $stmt->affected_rows > 0) {
        echo "Device Unbanned Successfully";
    } else {
        echo "Invalid ID or Already Unbanned";
    }

    $stmt->close();
    $conn->close();
    exit;
}

/* =======================
   TYPE : BAN (DEFAULT)
   ======================= */

if (!isset($_POST['device_id'])) {
    echo "Device ID Missing";
    exit;
}

$device_id = $_POST['device_id'];

$stmt = $conn->prepare(
    "INSERT INTO device_ban (device_id, ban_status)
     VALUES (?, 1)
     ON DUPLICATE KEY UPDATE ban_status = 1"
);
$stmt->bind_param("s", $device_id);

if ($stmt->execute()) {
    echo "Device Banned Successfully";
} else {
    echo "Failed to Ban Device";
}

$stmt->close();
$conn->close();