<?php
require_once 'connection.php';

// ============================================
// SITE SETTINGS VARIABLES - EASY TO CALL IN OTHER FILES
// ============================================

// Fetch ALL site settings from database
$site_settings_query = "SELECT setting_key, setting_value FROM site_settings";
$site_settings_result = $conn->query($site_settings_query);
$SITE_SETTINGS = [];
if ($site_settings_result && $site_settings_result->num_rows > 0) {
    while ($row = $site_settings_result->fetch_assoc()) {
        $SITE_SETTINGS[$row['setting_key']] = $row['setting_value'];
    }
}

// GENERAL SETTINGS - These are the most commonly used
$SITE_NAME = isset($SITE_SETTINGS['site_name']) ? $SITE_SETTINGS['site_name'] : 'Trading Platform';
$SITE_EMAIL = isset($SITE_SETTINGS['site_email']) ? $SITE_SETTINGS['site_email'] : 'support@example.com';
$SITE_PHONE = isset($SITE_SETTINGS['site_phone']) ? $SITE_SETTINGS['site_phone'] : '+1 (555) 123-4567';
$SITE_ADDRESS = isset($SITE_SETTINGS['site_address']) ? $SITE_SETTINGS['site_address'] : '123 Business Street, City, Country';
$SITE_URL = isset($SITE_SETTINGS['site_url']) ? $SITE_SETTINGS['site_url'] : 'https://yourdomain.com';
$COPYRIGHT_TEXT = isset($SITE_SETTINGS['copyright_text']) ? $SITE_SETTINGS['copyright_text'] : '© ' . date('Y') . ' All rights reserved';
$TIMEZONE = isset($SITE_SETTINGS['timezone']) ? $SITE_SETTINGS['timezone'] : 'UTC';
$DATE_FORMAT = isset($SITE_SETTINGS['date_format']) ? $SITE_SETTINGS['date_format'] : 'F j, Y';
$LOGO_PATH = isset($SITE_SETTINGS['logo_path']) ? $SITE_SETTINGS['logo_path'] : 'assets/images/logo.png';
$FAVICON_PATH = isset($SITE_SETTINGS['favicon_path']) ? $SITE_SETTINGS['favicon_path'] : 'assets/images/favicon.ico';

// REGISTRATION & SECURITY SETTINGS
$ALLOW_REGISTRATION = isset($SITE_SETTINGS['allow_registration']) ? ($SITE_SETTINGS['allow_registration'] == '1') : true;
$EMAIL_VERIFICATION_REQUIRED = isset($SITE_SETTINGS['email_verification_required']) ? ($SITE_SETTINGS['email_verification_required'] == '1') : true;
$MAINTENANCE_MODE = isset($SITE_SETTINGS['maintenance_mode']) ? ($SITE_SETTINGS['maintenance_mode'] == '1') : false;

// PAYMENT SETTINGS
$ENABLE_CRYPTO_PAYMENTS = isset($SITE_SETTINGS['enable_crypto_payments']) ? ($SITE_SETTINGS['enable_crypto_payments'] == '1') : true;
$ENABLE_BANK_PAYMENTS = isset($SITE_SETTINGS['enable_bank_payments']) ? ($SITE_SETTINGS['enable_bank_payments'] == '1') : true;
$MIN_DEPOSIT_AMOUNT = isset($SITE_SETTINGS['min_deposit_amount']) ? floatval($SITE_SETTINGS['min_deposit_amount']) : 10.00;
$MAX_DEPOSIT_AMOUNT = isset($SITE_SETTINGS['max_deposit_amount']) ? floatval($SITE_SETTINGS['max_deposit_amount']) : 10000.00;
$DEPOSIT_INSTRUCTION = isset($SITE_SETTINGS['deposit_instruction']) ? $SITE_SETTINGS['deposit_instruction'] : 'Send exact amount to the wallet address below. Include your User ID in the memo/description.';

// EMAIL SETTINGS (SMTP)
$SMTP_HOST = isset($SITE_SETTINGS['smtp_host']) ? $SITE_SETTINGS['smtp_host'] : 'smtp.gmail.com';
$SMTP_PORT = isset($SITE_SETTINGS['smtp_port']) ? intval($SITE_SETTINGS['smtp_port']) : 587;
$SMTP_USERNAME = isset($SITE_SETTINGS['smtp_username']) ? $SITE_SETTINGS['smtp_username'] : '';
$SMTP_ENCRYPTION = isset($SITE_SETTINGS['smtp_encryption']) ? $SITE_SETTINGS['smtp_encryption'] : 'tls';

// DISPLAY SETTINGS
$ITEMS_PER_PAGE = isset($SITE_SETTINGS['items_per_page']) ? intval($SITE_SETTINGS['items_per_page']) : 20;

// ============================================
// HOW TO USE THESE VARIABLES IN OTHER FILES:
// ============================================
/*
1. Include this file in your files:
   include('site-details.php');

2. Use the variables directly:
   - Site Name: <?php echo $SITE_NAME; ?>
   - Site Email: <?php echo $SITE_EMAIL; ?>
   - Check if registration is allowed: <?php echo $ALLOW_REGISTRATION ? 'Yes' : 'No'; ?>
   - Check if email verification is required: <?php echo $EMAIL_VERIFICATION_REQUIRED ? 'Yes' : 'No'; ?>
   - Minimum deposit: $<?php echo $MIN_DEPOSIT_AMOUNT; ?>
*/

// ============================================
// HELPER FUNCTIONS FOR COMMON OPERATIONS
// ============================================

// Function to format date according to site settings
function formatSiteDate($date_string) {
    global $TIMEZONE, $DATE_FORMAT;
    
    if (empty($date_string)) return 'N/A';
    
    $date = new DateTime($date_string);
    $date->setTimezone(new DateTimeZone($TIMEZONE));
    
    return $date->format($DATE_FORMAT);
}

// Function to format currency according to site settings
function formatSiteCurrency($amount) {
    return '$' . number_format($amount, 2);
}

// Function to check if a feature is enabled
function isFeatureEnabled($feature_key) {
    global $SITE_SETTINGS;
    return isset($SITE_SETTINGS[$feature_key]) && $SITE_SETTINGS[$feature_key] == '1';
}

// Function to get a specific setting value
function getSiteSetting($key, $default = '') {
    global $SITE_SETTINGS;
    return isset($SITE_SETTINGS[$key]) ? $SITE_SETTINGS[$key] : $default;
}

// ============================================
// EXAMPLE USAGE IN OTHER FILES:
// ============================================
/*
<h1>Welcome to <?php echo $SITE_NAME; ?></h1>
<p>Contact us: <?php echo $SITE_EMAIL; ?> | <?php echo $SITE_PHONE; ?></p>

<?php if ($ALLOW_REGISTRATION): ?>
    <p>New registrations are currently accepted.</p>
<?php else: ?>
    <p>New registrations are temporarily disabled.</p>
<?php endif; ?>

<?php if ($EMAIL_VERIFICATION_REQUIRED): ?>
    <p>Email verification is required for new accounts.</p>
<?php else: ?>
    <p>Accounts are auto-verified on registration.</p>
<?php endif; ?>

<p>Minimum deposit: <?php echo formatSiteCurrency($MIN_DEPOSIT_AMOUNT); ?></p>
<p>Maximum deposit: <?php echo formatSiteCurrency($MAX_DEPOSIT_AMOUNT); ?></p>

<?php if ($ENABLE_CRYPTO_PAYMENTS): ?>
    <p>Crypto payments are enabled.</p>
<?php endif; ?>

<?php if ($ENABLE_BANK_PAYMENTS): ?>
    <p>Bank payments are enabled.</p>
<?php endif; ?>
*/

// ============================================
// For debugging: Uncomment to see all settings
// ============================================
/*
echo "<pre>";
print_r($SITE_SETTINGS);
echo "</pre>";
*/
?>