<?php
include 'config.php';

function createLudoKingTableIfNotExists($conn) {
    $check = "SHOW TABLES LIKE 'ludoking'";
    $result = $conn->query($check);

    if ($result->num_rows == 0) {
        $createTableSQL = "CREATE TABLE `ludoking` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `match_title` varchar(100) NOT NULL,
            `match_time` varchar(50) NOT NULL,
            `total_prize` int(11) NOT NULL,
            `entry_fee` int(11) NOT NULL,
            `entry_type` varchar(10) NOT NULL,
            `match_type` varchar(15) NOT NULL,
            `play_map` varchar(15) NOT NULL,
            `registration` varchar(10) NOT NULL,
            `prize_details` varchar(100) NOT NULL,
            `total_player` int(11) NOT NULL,
            `total_joiner` int(11) NOT NULL DEFAULT 0,
            `match_result` varchar(10) NOT NULL,
            `room_details` varchar(50) NOT NULL,
            `timer` varchar(50) NOT NULL,
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";

        if ($conn->query($createTableSQL) === TRUE) {
            // Table created
        } else {
            die("Error creating table: " . $conn->error);
        }
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Ensure table exists
    createLudoKingTableIfNotExists($conn);

    $type = $_POST['type'];
    
    
    
    $admin_username = $_POST['admin_username']; // POST থেকে username নেওয়া
    $admin_password = $_POST['admin_password'];

    // Prepared statement — username ব্যবহার করবে (email নয়)
    $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();
    
    
    

    if ($type === 'getdata') {
        $sql = "SELECT * FROM ludoking";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            $rows = array();
            while ($row = $result->fetch_assoc()) {
                $rows[] = $row;
            }
            echo json_encode($rows);
        } else {
            echo "No Matches Found";
        }
    }

    if ($type === 'add') {
        $match_title = $_POST['match_title'];
        $match_time = $_POST['match_time'];
        $total_prize = $_POST['total_prize'];
        $entry_fee = $_POST['entry_fee'];
        $entry_type = $_POST['entry_type'];
        $match_type = $_POST['match_type'];
        $play_map = $_POST['play_map'];
        $registration = $_POST['registration'];
        $total_player = $_POST['total_player'];
        $prize_details = $_POST['prize_details'];
        $timer = $_POST['timer'];
        $room_details = "NO";
        $match_result = "NO";

        $sql = "INSERT INTO ludoking (match_title, match_time, total_prize, entry_fee, entry_type, match_type, play_map, registration, total_player, prize_details, room_details, match_result, timer) 
        VALUES ('$match_title', '$match_time', '$total_prize', '$entry_fee', '$entry_type', '$match_type', '$play_map', '$registration', '$total_player', '$prize_details', '$room_details', '$match_result', '$timer')";

        if ($conn->query($sql) === TRUE) {
            echo "Add Matches Successfully";
        } else {
            echo "Error Add Matches: " . $conn->error;
        }
    }

    if ($type === 'delete') {
        $id = $_POST['id'];
        $sql = "DELETE FROM ludoking WHERE id='$id'";

        if ($conn->query($sql) === TRUE) {
            echo "Match Deleted Successfully";
        } else {
            echo "Error Deleting Match: " . $conn->error;
        }
    }

    if ($type === 'move') {
        $result = $_POST['result'];
        $id = $_POST['id'];
        $sql = "UPDATE ludoking SET match_result = '$result' WHERE id='$id'";

        if ($conn->query($sql) === TRUE) {
            echo "Match Moved Successfully";
        } else {
            echo "Error Updating Record: " . $conn->error;
        }
    }

    if ($type === 'update') {
        $id = $_POST['id'];
        $match_title = $_POST['match_title'];
        $match_time = $_POST['match_time'];
        $total_prize = $_POST['total_prize'];
        $entry_fee = $_POST['entry_fee'];
        $registration = $_POST['registration'];
        $total_player = $_POST['total_player'];
        $prize_details = $_POST['prize_details'];
        $room_details = $_POST['room_details'];
        $timer = $_POST['timer'];

        $sql = "UPDATE ludoking SET 
                match_title='$match_title', 
                match_time='$match_time', 
                total_prize='$total_prize', 
                entry_fee='$entry_fee', 
                registration='$registration', 
                total_player='$total_player', 
                prize_details='$prize_details', 
                room_details='$room_details',
                timer='$timer' 
                WHERE id='$id'";

        if ($conn->query($sql) === TRUE) {
            echo "Match Updated Successfully";
        } else {
            echo "Error Updating Match: " . $conn->error;
        }
    }

    $conn->close();
} else {
    echo "Invalid Request Method";
}
?>