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

try {
    global $pdo;
    
    // Fetch all settings from database
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM site_settings");
    $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
    
    // Text Settings
    $appName = $settings['site_name'] ?? '45WIN';
    $currency = '₹';
    $nowTime = date("Y-m-d H:i:s");

    $telegramLink = $settings['telegram_link'] ?? 'https://t.me/';
    $chatLink = $settings['chat_link'] ?? 'https://tawk.to/';

    // 🔥 IMAGE SETTINGS (Admin control - favicon, logo, icons) 🔥
    $faviconUrl = !empty($settings['favicon']) ? $settings['favicon'] : '';
    $siteLogoUrl = !empty($settings['site_logo']) ? $settings['site_logo'] : 'assets/images/logo.png';
    $telegramImg = !empty($settings['telegram_icon']) ? $settings['telegram_icon'] : 'https://cdn-icons-png.flaticon.com/512/2111/2111646.png'; 
    $chatImg = !empty($settings['chat_icon']) ? $settings['chat_icon'] : 'https://cdn-icons-png.flaticon.com/512/589/589708.png'; 
    $wheelImg = !empty($settings['wheel_icon']) ? $settings['wheel_icon'] : 'https://cdn-icons-png.flaticon.com/512/10636/10636280.png';
    $primaryColor = $settings['primary_color'] ?? '#ff4a4a';
    $boxShadow = $settings['box_shadow'] ?? 'soft';

    // Exact JSON Structure
    $response = [
        "code" => 0,
        "msg" => "Succeed",
        "msgCode" => 0,
        "serviceNowTime" => $nowTime,
        "data" => [
            "projectName" => $appName,
            "projectLogo" => $siteLogoUrl,
            "faviconUrl" => $faviconUrl,
            "primaryColor" => $primaryColor,
            "boxShadow" => $boxShadow,
            "dollarSign" => $currency,
            "languages" => "en|hi",
            "defaultCurrentLanguage" => "en",
            
            // Telegram URL & Image
            "telegramExternalLink" => $telegramLink,
            "telegramImgUrl" => $telegramImg,
            
            // Live Chat URL & Image
            "isSelfCustomerService" => true, 
            "customerServiceLink" => $chatLink,
            "customerServiceImgUrl" => $chatImg,
            
            // Spin Wheel Settings
            "isOpenTurntable" => true,
            "homeBigTurntableSwitch" => true,
            "homeBigTurntableImgUrl" => $wheelImg,
            "isOpenInvitedWheel" => true,
            "invitedWheelTotalPrizeAmount" => 500.0,
            "invitedWheelImgUrl" => $wheelImg,
            
            "isShowAppDownloadUp" => true,
            "isShowAppDownloadDown" => true,
            "isShowRewardCenter" => true,
            "downAppBonusAmount" => 50.0,
            "isOpenBrowserConsoleDebug" => false,
            
            "registerMobile" => "1",
            "registerEmail" => "0",
            "areaPhoneLenList" => [
                ["area" => "+91", "len" => "10"]
            ]
        ]
    ];

    echo json_encode($response);
} catch (Throwable $e) {
    echo json_encode(["code" => 1, "msg" => "Database Error", "data" => null]);
}
?>