<?php
header('Content-Type: application/json');
include "config.php";

if (!isset($conn) && isset($con)) $conn = $con;
$conn->set_charset("utf8mb4");

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['status' => false, 'message' => 'Invalid request method']);
    exit;
}

$username = trim($_POST['username'] ?? '');
$password = trim($_POST['password'] ?? '');

if (empty($username) || empty($password)) {
    http_response_code(403);
    echo json_encode(["status" => false, "message" => "Username and Password required!"]);
    exit;
}

$md5_pass = md5($password);
$has_permission = false;

$admin_stmt = $conn->prepare("SELECT id FROM admin WHERE username=? AND (password=? OR password=?) LIMIT 1");
if ($admin_stmt) {
    $admin_stmt->bind_param("sss", $username, $password, $md5_pass);
    $admin_stmt->execute();
    if ($admin_stmt->get_result()->num_rows > 0) {
        $has_permission = true;
    }
    $admin_stmt->close();
}

if (!$has_permission) {
    $mod_stmt = $conn->prepare("SELECT extra_json FROM modarator_permissions WHERE username=? AND (password=? OR password=?) LIMIT 1");
    if ($mod_stmt) {
        $mod_stmt->bind_param("sss", $username, $password, $md5_pass);
        $mod_stmt->execute();
        $mod_res = $mod_stmt->get_result();
        if ($mod_res->num_rows > 0) {
            $mod_data = $mod_res->fetch_assoc();
            $extra_json = json_decode($mod_data['extra_json'], true);
            
            if (isset($extra_json['bonus_management']) && $extra_json['bonus_management'] === 'Yes') {
                $has_permission = true;
            }
        }
        $mod_stmt->close();
    }
}

if (!$has_permission) {
    http_response_code(403);
    echo json_encode(["status" => false, "message" => "Access Denied! Bonus Management Permission is required."]);
    exit;
}

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

if ($type === 'add') {
    $deposit = $_POST['deposit_amount'] ?? null;
    $bonus   = $_POST['bonus_amount'] ?? null;

    if ($deposit === null || $bonus === null) {
        echo json_encode(['status' => false, 'message' => 'Missing parameters']);
        exit;
    }

    $stmt = $conn->prepare("INSERT INTO deposits (deposit_amount, bonus_amount) VALUES (?, ?)");
    if (!$stmt) {
        echo json_encode(['status' => false, 'message' => 'Prepare failed']);
        exit;
    }
    $stmt->bind_param("ii", $deposit, $bonus);
    if ($stmt->execute()) {
        echo json_encode(['status' => true, 'message' => 'Data added successfully']);
    } else {
        echo json_encode(['status' => false, 'message' => 'Insert failed']);
    }
    $stmt->close();

} elseif ($type === 'update') {
    $id      = $_POST['id'] ?? null;
    $deposit = $_POST['deposit_amount'] ?? null;
    $bonus   = $_POST['bonus_amount'] ?? null;

    if ($id === null || $deposit === null || $bonus === null) {
        echo json_encode(['status' => false, 'message' => 'Missing parameters']);
        exit;
    }

    $stmt = $conn->prepare("UPDATE deposits SET deposit_amount = ?, bonus_amount = ? WHERE id = ?");
    if (!$stmt) {
        echo json_encode(['status' => false, 'message' => 'Prepare failed']);
        exit;
    }
    $stmt->bind_param("iii", $deposit, $bonus, $id);
    if ($stmt->execute()) {
        echo json_encode(['status' => true, 'message' => 'Data updated successfully']);
    } else {
        echo json_encode(['status' => false, 'message' => 'Update failed']);
    }
    $stmt->close();

} elseif ($type === 'delete') {
    $id = $_POST['id'] ?? null;

    if ($id === null) {
        echo json_encode(['status' => false, 'message' => 'Missing id']);
        exit;
    }

    $stmt = $conn->prepare("DELETE FROM deposits WHERE id = ?");
    if (!$stmt) {
        echo json_encode(['status' => false, 'message' => 'Prepare failed']);
        exit;
    }
    $stmt->bind_param("i", $id);
    if ($stmt->execute()) {
        echo json_encode(['status' => true, 'message' => 'Data deleted successfully']);
    } else {
        echo json_encode(['status' => false, 'message' => 'Delete failed']);
    }
    $stmt->close();

} else {
    echo json_encode(['status' => false, 'message' => 'Invalid type']);
}

$conn->close();
?>
