<?php
	include "../../conn.php";
	mysqli_set_charset($conn, "utf8mb4");
			
	header('Content-Type: application/json; charset=utf-8');
	header('Strict-Transport-Security: max-age=31536000');
	header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
	header('Access-Control-Allow-Credentials: true');
	$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
	header('Access-Control-Allow-Origin: ' . $origin);
	header('vary: Origin');
	
	date_default_timezone_set('Asia/Kolkata');
	$shnunc = date("Y-m-d H:i:s");
	$res = [
		'code' => 11,
		'msg' => 'Method not allowed',
		'msgCode' => 12,
		'serviceNowTime' => $shnunc,
	];
	
	// Fetch data from web_setting (assuming id = 1)
$query = "SELECT telegram_active, telegram_link, web_name, web_logo, favicon_logo FROM web_setting WHERE id = 1 LIMIT 1";
$result = mysqli_query($conn, $query);

if ($result && mysqli_num_rows($result) > 0) {
    $settingRow = mysqli_fetch_assoc($result);

    $telegram_active = $settingRow['telegram_active'];
    $telegram_link = $settingRow['telegram_link'];
    $web_name = $settingRow['web_name'];
    $web_logo = "https://bluewin.sbs/assets/png/logohu.png";
    $favicon_logo = "https://bluewin.sbs/assets/png/logohu.png";

    $data['telegramExternalLink'] = ($telegram_active == 1);
    $data['telegram_link'] = $telegram_link;
} else {
    // Default values if nothing found
    $telegram_active = 0;
    $telegram_link = '';
    $web_name = 'My Website';
    $web_logo = 'default-logo.png';
    $favicon_logo = 'default-favicon.ico';
}

$data = [];

$result = mysqli_query($conn, $query);

if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);

    // ✅ Yeh line important hai
    $data['telegramExternalLink'] = ($telegram_active == 1);
    $data['telegram_link'] = $telegram_link;

}

	
	$shonubody = file_get_contents("php://input");
	$shonupost = json_decode($shonubody, true);
	if ($_SERVER['REQUEST_METHOD'] != 'GET') {		
		if (isset($shonupost['language']) && isset($shonupost['random']) && isset($shonupost['signature']) && isset($shonupost['timestamp'])) {
			$language = htmlspecialchars(mysqli_real_escape_string($conn, $shonupost['language']));
			$random = htmlspecialchars(mysqli_real_escape_string($conn, $shonupost['random']));
			$signature = htmlspecialchars(mysqli_real_escape_string($conn, $shonupost['signature']));
			$shonustr = '{"language":'.$language.',"random":"'.$random.'"}';
			$shonusign = strtoupper(md5($shonustr));
			if($shonusign == $signature){
				$data['isShowAppDownloadUp'] = true;
				$data['isShowAppDownloadDown'] = true;
				$data['isShowLotteryDragon'] = true;
				$data['isSplitLocalEWallet'] = true;
				$data['jackportMaxReswadAmount'] = 500;
				$data['projectName'] = $web_name;
				$data['projectLogo'] = $web_logo;
				
				$data['webIco'] = $favicon_logo;
                $data['headLogo'] = $web_logo;

				
				$data['upperOrLower'] = '0';
				
				
				
				//db fatch currecy and lang Start// 
				
				// Initialize variables
$languageData = [];
$data['dollarSign'] = '₹'; // fallback
$data['defaultCurrentLanguage'] = 'en'; // fallback

// Fetch from langcurrency table
$result = mysqli_query($conn, "SELECT * FROM langcurrency WHERE status = 1");
while ($row = mysqli_fetch_assoc($result)) {
    $languageData[] = $row['lang_code'];

    if ($row['is_default'] == 1) {
        $data['dollarSign'] = $row['currency_symbol'];
        $data['defaultCurrentLanguage'] = $row['lang_code'];
    }
}

// Format: en|hi|ar
$data['languages'] = implode('|', $languageData);


             //db fatch currecy and lang End // 


				
				$data['registerMobile'] = '1';
				$data['registerEmail'] = '0';
				$data['areaPhoneLenList'][0]['area'] = '+91';
				$data['areaPhoneLenList'][0]['len'] = '9-10';

// Query to fetch active country codes
// $query = "SELECT country_code, number_length FROM country_codes WHERE status = 'Active'";
// $result = mysqli_query($conn, $query);

// if ($result && mysqli_num_rows($result) > 0) {
//     while ($row = mysqli_fetch_assoc($result)) {
//         $area = trim($row['country_code']);
//         $length = trim($row['number_length']);

//         if (!empty($area) && !empty($length)) {
//             $areaPhoneLenList[] = [
//                 'area' => $area,
//                 'len' => $length
//             ];
//         }
//     }
// } else {
//     // If no active records found, use default
//     $areaPhoneLenList[] = [
//         'area' => '+91',
//         'len'  => '9-10'
//     ];
// }
// $data['areaPhoneLenList'] = $areaPhoneLenList;
			
				$data['registerSms'] = '0';
				$data['isOpenLoginChangeLanguage'] = '1';
				$data['rewardValidityTime'] = 30;
				$data['electronicWinRateExternalLink'] = '';
				$data['electronicWinRateImgUrl'] = 'https://boom92.online/sikkim';
				$data['isShowElectronicWinRateExternalLink'] = false;
				$data['isShowAppHandCodeWashingSwitch'] = true;
				$data['isShowHotGameWinOdds'] = true;
				$data['ossUrl'] = 'https://boom92.online';
				$data['bigTurntableLink'] = null;
				$data['telegramExternalLink'] = ($row['telegram_active'] == 1) ? true : false;
				$data['isOpenActivityAward'] = true;
				$data['isOpenTurntable'] = true;
				$data['isPartnerReward'] = false;
				
				$data['firstDepositRewardCodeAmount'] = "1";
				$data['isOpenAdjustEvent'] = true;
				
				$res['data'] = $data;
				$res['code'] = 0;
				$res['msg'] = 'Succeed';
				$res['msgCode'] = 0;
				http_response_code(200);
				echo json_encode($res);
			}
			else{
				$res['code'] = 5;
				$res['msg'] = 'Wrong signature';
				$res['msgCode'] = 3;
				http_response_code(200);
				echo json_encode($res);
			}
		}
		else{
			$res['code'] = 7;
			$res['msg'] = 'Param is Invalid';
			$res['msgCode'] = 6;
			http_response_code(200);
			echo json_encode($res);
		}		
	} else {		
		http_response_code(405);
		echo json_encode($res);
	}
?>