<?php
session_start();

header("Access-Control-Allow-Origin: https://enerliths.com");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Content-Type: application/json");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

require "../config/db.php";

/* CHECK LOGIN */

if (!isset($_SESSION['user_id'])) {
    echo json_encode([
        "status" => "error",
        "message" => "User not authenticated"
    ]);
    exit;
}

$user_id = $_SESSION['user_id'];

/* DISABLE 2FA */

$stmt = $conn->prepare("
UPDATE user_2fa
SET enabled = 0
WHERE user_id = ?
");

$stmt->bind_param("i", $user_id);

if ($stmt->execute()) {

    echo json_encode([
        "status" => "success",
        "message" => "Two-factor authentication disabled successfully"
    ]);

} else {

    echo json_encode([
        "status" => "error",
        "message" => "Failed to disable 2FA"
    ]);

}

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