<?php
/**
 * Configuration File - Binary Options Platform
 */

// Start session FIRST before anything else
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Prevent constant redeclaration
if (!defined('ROOT_PATH')) {
    // Path Settings
    define('ROOT_PATH', dirname(__DIR__));
}

if (!defined('DB_HOST')) {
    // Database Settings (Change for your server)
    define('DB_HOST', 'localhost');
    define('DB_NAME', 'binary_options');
    define('DB_USER', 'root');
    define('DB_PASS', '');  // Empty for XAMPP
    define('DB_CHARSET', 'utf8mb4');
}

if (!defined('BINANCE_API_URL')) {
    // Binance API Settings
    define('BINANCE_API_URL', 'https://api.binance.com/api/v3');
}

if (!defined('SITE_URL')) {
    // Site Settings
    define('SITE_URL', 'http://localhost/binary-options');
    define('SITE_NAME', 'BinaryPro');
    define('APP_NAME', 'BinaryPro');
}

if (!defined('SESSION_LIFETIME')) {
    // Security Settings
    define('SESSION_LIFETIME', 86400);
    define('ENCRYPTION_KEY', 'binary_options_secure_key_2024');
    define('REMEMBER_ME_LIFETIME', 2592000);
    define('CSRF_TOKEN_NAME', 'csrf_token');
}

if (!defined('CURRENCY_SYMBOL')) {
    // Currency & Balance Settings
    define('CURRENCY_SYMBOL', '₹');
    define('CURRENCY_CODE', 'INR');
    define('DEMO_BALANCE', 10000);
    define('WELCOME_BONUS_PERCENT', 0);
    define('REFERRAL_COMMISSION_PERCENT', 5);
    define('MIN_TRADE_AMOUNT', 10);
    define('MAX_TRADE_AMOUNT', 50000);
    define('MAX_UPLOAD_SIZE', 5242880);
    define('ALLOWED_IMAGE_TYPES', ['image/jpeg', 'image/png', 'image/gif', 'image/webp']);
}

// URL Settings
if (!defined('ADMIN_URL')) {
    define('ADMIN_URL', SITE_URL . '/admin');
    define('USER_URL', SITE_URL . '/user');
    define('ASSETS_URL', SITE_URL . '/assets');
    define('UPLOADS_URL', SITE_URL . '/uploads');
    define('API_URL', SITE_URL . '/api');
}

// Error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

/**
 * Get setting from database with fallback
 */
if (!function_exists('getSetting')) {
    function getSetting($key, $default = '') {
        try {
            if (!function_exists('dbFetch')) return $default;
            $result = dbFetch("SELECT setting_value FROM settings WHERE setting_key = ?", [$key]);
            return $result ? ($result['setting_value'] ?? $default) : $default;
        } catch (Exception $e) {
            return $default;
        }
    }
}
