<?php 
    // Database connection
	include "../../conn.php";
	include "../../functions2.php";
	
	header('Content-Type: application/json; charset=utf-8');
	header('Access-Control-Allow-Origin: *'); 
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    header('Access-Control-Allow-Headers: *');
	
	date_default_timezone_set("Asia/Kolkata");
	
    // Default response setup
	$res = [
		'code' => 0, 
		'msg' => 'Success',
        'data' => null
	];
	
    // Input read karein
	$shonubody = file_get_contents("php://input");
	$shonupost = json_decode($shonubody, true);

    // Agar JSON empty hai to bhi crash nahi hone denge
    if ($shonupost === null) {
        $shonupost = []; // Empty array banado taaki code aage chale
    }
	
    // Game Code nikaalo (Agar nahi mila to default 800 maan lenge)
    $gameCode = isset($shonupost['gameCode']) ? (string)$shonupost['gameCode'] : '800'; 
    $vendorCode = isset($shonupost['vendorCode']) ? $shonupost['vendorCode'] : '';
    
    // --- BYPASS LOGIC START ---

    // 1. GAME URL SETTINGS (Yahan apne Links change karo)
    $gameLaunchUrl = "";
    
    if ($gameCode == "800,") {
        // Aviator (Jaisa pehle tha)
        $gameLaunchUrl = "https://51bcgames.online/av/index.html"; 
    } 
    else if ($gameCode == "229") {
        // Game 100 (Yahan apna link daalo)
        $gameLaunchUrl = "https://51bcgames.online/game100/mine.html"; 
    }
    else if ($gameCode == "121") {
        // Game 121 (Yahan apna link daalo)
        $gameLaunchUrl = "https://51bcgames.online/game121/index.html"; 
    }
    else if ($vendorCode == "TB_Chess") {
        // Vendor Code check (Optional)
        $gameLaunchUrl = "https://51bcgames.online/chess/index.html";
    }
    else {
        // Agar koi aisa code aaye jo upar nahi hai, to Default Aviator khol do
        $gameLaunchUrl = "https://51bcgames.online/av/";
    }

    // 2. FAKE BALANCE UPDATE (Optional - Balance 0 dikhane ke liye)
    if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
        $bearer = explode(" ", $_SERVER['HTTP_AUTHORIZATION']);
        if (isset($bearer[1])) {
            $author = $bearer[1];
            // Error suppression (@) ke sath update query
            @$conn->query("UPDATE shonu_kaichila SET motta = 0 WHERE balakedara = (SELECT mobile FROM shonu_subjects WHERE akshinak = '$author' LIMIT 1)");
        }
    }

    // 3. SEND SUCCESS RESPONSE
    $res['code'] = 0;
    $res['msg'] = 'Game launched successfully';
    $res['data'] = [
        'url' => $gameLaunchUrl,
    ];

    http_response_code(200);
    echo json_encode($res);
    exit;
?>