<?php
/**
 * Dual Database Configuration File (Reversed Roles)
 * Server A (Main): Local Hosting (65.108.110.225) - Code yahan run ho raha hai
 * Server B (Backup): Remote Hosting (45.113.226.95)
 */

mysqli_report(MYSQLI_REPORT_OFF); 

// --- SERVER A (MAIN - LOCALHOST: 65.108.110.225) ---
// Kyuki code isi server par hai, isliye host 'localhost' rahega
$dbA_config = [
    'host' => 'localhost', 
    'user' => 'cybsoul_loyality',
    'pass' => 'Digital@1270',
    'name' => 'cybsoul_taskapp'
];

// --- SERVER B (BACKUP - REMOTE: 45.113.226.95) ---
$dbB_config = [
    'host' => '45.113.226.95', 
    'user' => 'pragatis_task',
    'pass' => 'Hvsk@9024',
    'name' => 'pragatis_cybsoul_taskapp'
];

// 1. Connection to Main Server (A - Local)
$connA = new mysqli($dbA_config['host'], $dbA_config['user'], $dbA_config['pass'], $dbA_config['name']);

// 2. Connection to Backup Server (B - Remote)
$connB = mysqli_init();
$connB->options(MYSQLI_OPT_CONNECT_TIMEOUT, 3); // 3 seconds timeout
$connB->real_connect($dbB_config['host'], $dbB_config['user'], $dbB_config['pass'], $dbB_config['name']);

/**
 * FAILOVER LOGIC:
 * Agar Local Server (A) down hai, toh Remote Backup (B) use hoga.
 */
$connection_status = ["A" => false, "B" => false];

if ($connA->connect_error) {
    $connection_status["A"] = false;
    if ($connB->connect_error) {
        $connection_status["B"] = false;
        die("Critical Error: Dono Database Servers band hain!");
    }
    $conn = $connB; 
    $active_server = "Backup (Remote Server B)";
    $connection_status["B"] = true;
} else {
    $connection_status["A"] = true;
    $conn = $connA; 
    $active_server = "Main (Local Server A)";
    $connection_status["B"] = ($connB && !$connB->connect_error);
}

// Session Security & Timeout
ini_set('session.gc_maxlifetime', 36000);
session_set_cookie_params(36000);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/**
 * SYNC SAVE FUNCTION:
 * Data ko dono database mein ek saath save karne ke liye.
 */
function dualQuery($sql, $types, $params, $connA, $connB) {
    $results = ['A' => false, 'B' => false];

    // Server A (Local)
    if ($connA && !$connA->connect_error) {
        $stmtA = $connA->prepare($sql);
        if ($stmtA) {
            $stmtA->bind_param($types, ...$params);
            $results['A'] = $stmtA->execute();
            $stmtA->close();
        }
    }

    // Server B (Remote)
    if ($connB && !$connB->connect_error) {
        $stmtB = $connB->prepare($sql);
        if ($stmtB) {
            $stmtB->bind_param($types, ...$params);
            $results['B'] = $stmtB->execute();
            $stmtB->close();
        }
    }
    
    return $results;
}

// Debug Mode Helper
if (isset($_GET['check_sync'])) {
    echo "<div style='position:fixed; top:10px; right:10px; background:#0f172a; color:#fff; padding:15px; border-radius:12px; font-family:sans-serif; font-size:12px; z-index:9999; border:1px solid #334155; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);'>";
    echo "<b style='color:#6366f1; font-size:14px;'>Reverse Sync Status</b><hr style='border:0; border-top:1px solid #334155; margin:10px 0;'>";
    echo "Main Server A (Local): " . ($connection_status["A"] ? "✅ Online" : "❌ Offline") . "<br>";
    echo "Backup Server B (Remote): " . ($connection_status["B"] ? "✅ Online" : "❌ Offline") . "<br>";
    echo "Current Active: <span style='color:#fbbf24; font-weight:bold;'>" . $active_server . "</span>";
    echo "</div>";
}
?>