<?php
include 'config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $type = $_POST['type'];
    $admin_username = $_POST['admin_username'];
    $admin_password = $_POST['admin_password'];

    // ===== Admin login check =====
    $admin_stmt = $conn->prepare(
        "SELECT 1 FROM admin WHERE username = ? AND password = ?"
    );
    $admin_stmt->bind_param("ss", $admin_username, $admin_password);
    $admin_stmt->execute();
    $admin_result = $admin_stmt->get_result();

    if ($admin_result->num_rows === 0) {
        http_response_code(403);
        echo json_encode([
            "status" => "error",
            "message" => "Access Denied"
        ], JSON_PRETTY_PRINT);
        $admin_stmt->close();
        $conn->close();
        exit();
    }
    $admin_stmt->close();

    // ================= FREE FIRE =================
    if ($type === 'freefire') {

        $total_kill = $_POST['total_kill'];
        $total_win  = $_POST['total_win'];
        $email      = $_POST['email'];
        $id         = $_POST['id'];
        $match_id   = $_POST['match_id'];

        $winaddmsg  = 'Prize money has been successfully distributed';

        $sql = "
            UPDATE users SET
                winning     = winning + $total_win,
                total_win   = total_win + $total_win,
                total_kill  = total_kill + $total_kill,
                total_paid  = total_paid + $total_win
            WHERE email = '$email'
        ";

        $update = "
            UPDATE joiners SET
                total_kill = '$total_kill',
                winning    = '$total_win'
            WHERE id = '$id'
        ";

        $statistics = "
            INSERT INTO statistics (type, match_id, email, winnings, winaddmsg)
            VALUES ('$type', '$match_id', '$email', '$total_win', '$winaddmsg')
            ON DUPLICATE KEY UPDATE winnings = VALUES(winnings), winaddmsg = VALUES(winaddmsg)
        ";

        if (
            $conn->query($sql) === TRUE &&
            $conn->query($update) === TRUE &&
            $conn->query($statistics) === TRUE
        ) {
            echo "Winner Added Successfully";
        } else {
            echo "Error Adding Winner: " . $conn->error;
        }
    }

    // ================= LUDO KING =================
    if ($type === 'ludoking') {

        $total_win = $_POST['total_win'];
        $email     = $_POST['email'];
        $match_id  = $_POST['match_id'];

        $winaddmsg = 'Prize money has been successfully distributed';

        $sql = "
            UPDATE users SET
                winning    = winning + $total_win,
                total_win  = total_win + $total_win,
                total_paid = total_paid + $total_win
            WHERE email = '$email'
        ";

        $update = "
            UPDATE joiners SET
                winning = '$total_win'
            WHERE type = '$type'
              AND match_id = '$match_id'
        ";

        $statistics = "
            INSERT INTO statistics (type, match_id, email, winnings, winaddmsg)
            VALUES ('$type', '$match_id', '$email', '$total_win', '$winaddmsg')
            ON DUPLICATE KEY UPDATE winnings = VALUES(winnings), winaddmsg = VALUES(winaddmsg)
        ";

        if (
            $conn->query($sql) === TRUE &&
            $conn->query($update) === TRUE &&
            $conn->query($statistics) === TRUE
        ) {
            echo "Winner Added Successfully";
        } else {
            echo "Error Adding Winner: " . $conn->error;
        }
    }

    $conn->close();

} else {
    echo "Invalid Request Method";
}
?>