<?php
/**
 * Unified Configuration File for ZForex Trading Platform
 * Combines database, mail system, and application settings
 */

// ================================
// DATABASE CONFIGURATION
// ================================
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "crmcentrioglobal_ecotradefx");

// ================================
// MAIL SYSTEM CONFIGURATION
// ================================
define('MAIL_HOST', 'send.smtp.com');
define('MAIL_PORT', 465);
define("MAIL_USERNAME", "support@mykfmarkets.com"); // Support email address
define("MAIL_PASSWORD", "support@mykfmarkets.com"); 
define('MAIL_ENCRYPTION', 'ssl');
define('MAIL_FROM_EMAIL', 'support@mykfmarkets.com');
define('MAIL_FROM_NAME', 'EcoTradeFX');

// ================================
// EMAIL TEMPLATE SETTINGS
// ================================
define('MAIL_LOGO_URL', 'https://ecotradefx.com/assets/images/logo.png');
define('MAIL_COMPANY_NAME', 'EcoTradeFX');
define('MAIL_COMPANY_ADDRESS', 'Your Company Address');
define('MAIL_SUPPORT_EMAIL', 'support@ecotradefx.com');
define('MAIL_WEBSITE_URL', 'https://ecotradefx.com');

// ================================
// APPLICATION SETTINGS
// ================================
define('APP_NAME', 'EcoTradeFX');
define('APP_URL', 'https://ecotradefx.com');
define('APP_ENVIRONMENT', 'production'); // development, staging, production

// ================================
// MT5 API CONFIGURATION
// ================================
define('MT5_SERVER_IP', '127.0.0.1');
define('MT5_SERVER_PORT', 443);
define('MT5_TIMEOUT', 5);
define('MT5_SERVER_WEB_LOGIN', 'your_mt5_login');
define('MT5_SERVER_WEB_PASSWORD', 'your_mt5_password');

// ================================
// EMAIL AUTOMATION SETTINGS
// ================================
define('EMAIL_AUTOMATION_ENABLED', true);
define('EMAIL_QUEUE_ENABLED', false); // Set to true for high-volume environments
define('EMAIL_LOG_ENABLED', true);
define('EMAIL_DEBUG_MODE', false);

// ================================
// SECURITY SETTINGS
// ================================
define('ENCRYPTION_KEY', 'your-32-character-encryption-key-here');
define('JWT_SECRET', 'your-jwt-secret-key-here');
define('SESSION_TIMEOUT', 3600); // 1 hour

// ================================
// FILE UPLOAD SETTINGS
// ================================
define('UPLOAD_MAX_SIZE', 5242880); // 5MB
define('UPLOAD_ALLOWED_TYPES', ['jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx']);
define('KYC_UPLOAD_PATH', __DIR__ . '/uploads/kyc/');
define('TRANSACTION_PROOF_PATH', __DIR__ . '/uploads/transactions/');

// ================================
// LOGGING CONFIGURATION
// ================================
define('LOG_PATH', __DIR__ . '/logs/');
define('LOG_LEVEL', 'INFO'); // DEBUG, INFO, WARNING, ERROR
define('LOG_MAX_FILES', 30); // Keep logs for 30 days

// ================================
// BUSINESS LOGIC SETTINGS
// ================================
define('MIN_WITHDRAWAL_AMOUNT', 10);
define('MAX_WITHDRAWAL_AMOUNT', 50000);
define('WITHDRAWAL_PROCESSING_TIME', '1-3 business days');
define('KYC_REQUIRED_FOR_WITHDRAWAL', true);
define('DEFAULT_CURRENCY', 'USD');
define('DEFAULT_LEVERAGE', 100);

// ================================
// NOTIFICATION SETTINGS
// ================================
define('ADMIN_EMAIL', 'admin@ecotradefx.com');
define('ADMIN_NOTIFICATIONS_ENABLED', true);
define('USER_NOTIFICATIONS_ENABLED', true);
define('SMS_NOTIFICATIONS_ENABLED', false);

// ================================
// RATE LIMITING
// ================================
define('API_RATE_LIMIT', 100); // requests per minute
define('EMAIL_RATE_LIMIT', 10); // emails per minute per user

// ================================
// BACKUP AND MAINTENANCE
// ================================
define('BACKUP_ENABLED', true);
define('BACKUP_FREQUENCY', 'daily');
define('MAINTENANCE_MODE', false);

// ================================
// THIRD-PARTY INTEGRATIONS
// ================================
define('PAYMENT_GATEWAY_ENABLED', true);
define('SOCIAL_LOGIN_ENABLED', false);
define('ANALYTICS_ENABLED', true);

// ================================
// DATABASE CONNECTION
// ================================
try {
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        error_log("Database connection failed: " . $conn->connect_error);
        if (APP_ENVIRONMENT === 'development') {
            die("Database connection failed: " . $conn->connect_error);
        } else {
            die("Database connection failed. Please contact support.");
        }
    }
    $conn->set_charset("utf8mb4");
} catch (Exception $e) {
    error_log("Database connection exception: " . $e->getMessage());
    if (APP_ENVIRONMENT === 'development') {
        die("Database connection exception: " . $e->getMessage());
    } else {
        die("Database connection failed. Please contact support.");
    }
}

// ================================
// UTILITY FUNCTIONS
// ================================

/**
 * Get configuration value with fallback
 */
function getConfig($key, $default = null) {
    return defined($key) ? constant($key) : $default;
}

/**
 * Check if feature is enabled
 */
function isFeatureEnabled($feature) {
    $key = strtoupper($feature) . '_ENABLED';
    return getConfig($key, false);
}

/**
 * Get upload path for specific type
 */
function getUploadPath($type = 'general') {
    switch ($type) {
        case 'kyc':
            return KYC_UPLOAD_PATH;
        case 'transaction':
            return TRANSACTION_PROOF_PATH;
        default:
            return __DIR__ . '/uploads/';
    }
}

/**
 * Log message with timestamp
 */
function logMessage($message, $level = 'INFO', $file = 'application.log') {
    if (!LOG_ENABLED) return;
    
    $timestamp = date('Y-m-d H:i:s');
    $logEntry = "[$timestamp] [$level] $message" . PHP_EOL;
    
    $logFile = LOG_PATH . $file;
    if (!file_exists(LOG_PATH)) {
        mkdir(LOG_PATH, 0755, true);
    }
    
    file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
}

// ================================
// ENVIRONMENT-SPECIFIC SETTINGS
// ================================
if (APP_ENVIRONMENT === 'development') {
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    define('LOG_ENABLED', true);
} else {
    error_reporting(0);
    ini_set('display_errors', 0);
    define('LOG_ENABLED', EMAIL_LOG_ENABLED);
}

// ================================
// CORS HEADERS FOR API
// ================================
function setCorsHeaders() {
    $origin = $_SERVER['HTTP_ORIGIN'] ?? '';
    
    $allowedOrigins = [
        'https://ecotradefx.com',
        'https://admin.ecotradefx.com',
        'http://localhost:3000',
        'http://localhost:3001'
    ];
    
    if (in_array($origin, $allowedOrigins)) {
        header("Access-Control-Allow-Origin: $origin");
        header("Access-Control-Allow-Credentials: true");
    } else {
        header("Access-Control-Allow-Origin: *");
    }
    
    header('Content-Type: application/json');
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
    header('Access-Control-Max-Age: 1728000');
    
    if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
        http_response_code(200);
        exit;
    }
}

// ================================
// AUTO-INCLUDE MAIL SYSTEM
// ================================
require_once __DIR__ . '/mail_system/integrated_mail_system.php';

?>