<?php
session_start();

/* ================= ACCESS CONTROL ================= */
if(!isset($_SESSION['role']) || $_SESSION['role'] != "admin"){
    exit("No access");
}

/* ================= REQUIRED FILES ================= */
include("../../../config/db.php");
include($_SERVER['DOCUMENT_ROOT']."/SEAT/modules/Notification/notification_helper.php");

/* ================= INPUT ================= */
$id     = (int)($_POST['id'] ?? 0);
$status = $_POST['status'] ?? '';

if($id <= 0){
    exit("Invalid user");
}

if($status != "active" && $status != "inactive"){
    exit("Invalid status");
}

/* ================= UPDATE USER STATUS ================= */
mysqli_query($conn,"
UPDATE users
SET status='$status'
WHERE id=$id
");

/* ================= SEND NOTIFICATION ================= */
if($status == "active"){

    notify(
        $conn,
        $id,
        "Account Activated",
        "Your account has been activated.",
        "account",
        $id
    );

}else{

    notify(
        $conn,
        $id,
        "Account Deactivated",
        "Your account has been deactivated. Please contact admin if you need further assistance.",
        "account",
        $id
    );
}

echo "ok";
?>