<?php
// get_setting.php
require_once 'config.php';

function get_site_settings($pdo) {
    $settings = [];
    $sql = "SELECT setting_key, setting_value FROM site_config";
    if ($pdo) {
        // Removed non-standard space here
        $stmt = $pdo->query($sql);
        if ($stmt) {
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $settings[$row['setting_key']] = $row['setting_value'];
            }
        }
    }
    
    $default_settings = [
        'site_title' => 'Global Axis Cargo',
        'use_site_logo' => '1',
        'site_logo' => '',
        'site_favicon' => '',
        'use_telegram' => '0',
        'telegram_username' => '@defaultusername',
        'use_whatsapp' => '0',
        'whatsapp_number' => '1234567890',
        'use_smartsupp' => '0',
        'smartsupp_code' => '',
        'brand_color' => '#4f46e5',
        
        // --- SOCIAL LINKS ---
        'use_twitter' => '0',
        'twitter_url' => '',
        'use_facebook' => '0',
        'facebook_url' => '',
        'use_youtube' => '0',
        'youtube_url' => '',
        'use_instagram' => '0',
        'instagram_url' => '',
        
        // --- PAYMENT SETTINGS ---
        'bank_name' => '',
        'bank_account_name' => '',
        'bank_account_number' => '',
        'bank_sort_code' => '',
        'crypto_ltc' => '',
        'crypto_eth' => '',
        'crypto_usdt' => '',
        'crypto_btc' => '',
        'giftcard_note' => 'Contact support for gift card details.',
        'use_bank' => '0',
        'use_crypto' => '0',
        'use_giftcard' => '0',

        'default_language' => 'en', // Default to English
        'maintenance_mode' => '0',
    ];
    
    $site_settings = array_merge($default_settings, $settings);
    
    // Dynamically generate social links if they are empty
    $brand_slug = str_replace(' ', '', $site_settings['site_title'] ?? 'GlobalAxisCargo');
    if (empty($site_settings['twitter_url'])) $site_settings['twitter_url'] = "https://twitter.com/" . $brand_slug;
    if (empty($site_settings['facebook_url'])) $site_settings['facebook_url'] = "https://facebook.com/" . $brand_slug;
    if (empty($site_settings['youtube_url'])) $site_settings['youtube_url'] = "https://youtube.com/" . $brand_slug;
    if (empty($site_settings['instagram_url'])) $site_settings['instagram_url'] = "https://instagram.com/" . $brand_slug;

    return $site_settings;
}

// Load all settings from the database (merged with defaults)
$site_settings = get_site_settings($pdo);

// --- Branding & Logo Paths ---
$use_logo = (
    ($site_settings['use_site_logo'] === '1') &&
    !empty($site_settings['site_logo'])
);
$logo_path = $use_logo ? 'assets/images/' . htmlspecialchars($site_settings['site_logo']) : '';

$use_favicon = !empty($site_settings['site_favicon']);
$favicon_path = $use_favicon ? 'assets/images/' . htmlspecialchars($site_settings['site_favicon']) : '';

$favicon_mime = 'image/x-icon';

if ($use_favicon) {
    
    $extension = pathinfo($site_settings['site_favicon'], PATHINFO_EXTENSION);
    $extension = strtolower($extension);

    
    switch ($extension) {
        case 'png':
            $favicon_mime = 'image/png';
            break;
        case 'jpg':
        case 'jpeg':
            $favicon_mime = 'image/jpeg';
            break;
        case 'gif':
            $favicon_mime = 'image/gif';
            break;
        case 'svg':
            $favicon_mime = 'image/svg+xml';
            break;
        case 'ico':
            $favicon_mime = 'image/x-icon';
            break;
    }
}


// --- Chat/Social Links ---
$telegram_enabled = (
    $site_settings['use_telegram'] === '1' &&
    !empty($site_settings['telegram_username'])
);
$telegram_username_clean = ltrim(htmlspecialchars($site_settings['telegram_username']), '@');
$telegram_link = $telegram_enabled ? 'https://t.me/' . $telegram_username_clean : '';

$whatsapp_enabled = (
    $site_settings['use_whatsapp'] === '1' &&
    !empty($site_settings['whatsapp_number'])
);
$whatsapp_number_clean = preg_replace('/\D/', '', $site_settings['whatsapp_number']);
$whatsapp_link = $whatsapp_enabled ? 'https://wa.me/' . $whatsapp_number_clean : '';

$smartsupp_enabled = (
    $site_settings['use_smartsupp'] === '1' &&
    !empty($site_settings['smartsupp_code'])
);
$smartsupp_script = $smartsupp_enabled ? $site_settings['smartsupp_code'] : '';

$brand_color = htmlspecialchars($site_settings['brand_color']);

$twitter_enabled = (
    $site_settings['use_twitter'] === '1' &&
    !empty($site_settings['twitter_url'])
);
$twitter_url = htmlspecialchars($site_settings['twitter_url']);

$facebook_enabled = (
    $site_settings['use_facebook'] === '1' &&
    !empty($site_settings['facebook_url'])
);
$facebook_url = htmlspecialchars($site_settings['facebook_url']);

$youtube_enabled = (
    $site_settings['use_youtube'] === '1' &&
    !empty($site_settings['youtube_url'])
);
$youtube_url = htmlspecialchars($site_settings['youtube_url']);

$instagram_enabled = (
    $site_settings['use_instagram'] === '1' &&
    !empty($site_settings['instagram_url'])
);
$instagram_url = htmlspecialchars($site_settings['instagram_url']);

// --- Payment Settings ---
$bank_enabled = $site_settings['use_bank'] === '1';
$bank_name = htmlspecialchars($site_settings['bank_name']);
$bank_account_name = htmlspecialchars($site_settings['bank_account_name']);
$bank_account_number = htmlspecialchars($site_settings['bank_account_number']);
$bank_sort_code = htmlspecialchars($site_settings['bank_sort_code']);

$crypto_enabled = $site_settings['use_crypto'] === '1';
$crypto_ltc = htmlspecialchars($site_settings['crypto_ltc']);
$crypto_eth = htmlspecialchars($site_settings['crypto_eth']);
$crypto_usdt = htmlspecialchars($site_settings['crypto_usdt']);
$crypto_btc = htmlspecialchars($site_settings['crypto_btc']);

$giftcard_enabled = $site_settings['use_giftcard'] === '1';
$giftcard_note = htmlspecialchars($site_settings['giftcard_note']);

// NEW: Language Variables
$default_language_code = htmlspecialchars($site_settings['default_language'] ?? 'en');





// --- MAINTENANCE MODE GLOBAL CHECK ---
// 1. Only check if maintenance mode is active (set to '1')
// 2. Exemptions: 
//    - Users already on maintenance.php
//    - Users in the admin directory (admin/)
//    - Logged-in admin users
if (($site_settings['maintenance_mode'] ?? '0') === '1') {
    $current_script = basename($_SERVER['SCRIPT_NAME']);
    $is_admin_dir = (strpos($_SERVER['SCRIPT_NAME'], '/admin/') !== false);
    $is_admin_logged_in = (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true);
    
    if ($current_script !== 'maintenance.php' && !$is_admin_dir && !$is_admin_logged_in) {
        header("Location: maintenance.php");
        exit;
    }
}
?>