<?php
session_start();
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';

$user_id = $_SESSION['user_id'] ?? null;
$transfer_id = $_SESSION['transfer_id'] ?? null;

if (!$user_id || !$transfer_id) {
    header("Location: /user/dashboard.php");
    exit;
}

$conn = db();
if (!$conn) die("Database connection failed");

// Fetch transfer (optional safety check)
$stmt = $conn->prepare("
    SELECT id FROM transfers 
    WHERE id = ? AND user_id = ? LIMIT 1
");
$stmt->bind_param("ii", $transfer_id, $user_id);
$stmt->execute();
$transfer = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$transfer) {
    header("Location: /user/dashboard.php");
    exit;
}

// Processing steps
$steps = [
    "🔍 Validating account details...",
    "👤 Validating account name...",
    "🌐 Validating account number...",
    "🏦 Verifying bank connection...",
    "⚙️ Processing transfer..."
];

// ==========================
// UI CONTENT
// ==========================
ob_start();
?>

<div style="max-width:500px;margin:40px auto;background:#fff;padding:30px;border-radius:12px;text-align:center;box-shadow:0 10px 30px rgba(0,0,0,0.2);">
    
    <h2 style="margin-bottom:20px;">🔄 Processing Transfer</h2>

    <div id="step" style="font-size:18px;color:#1a73e8;margin-top:20px;">
        Initializing...
    </div>

</div>

<script>
const steps = <?= json_encode($steps) ?>;
let current = 0;
const stepDiv = document.getElementById("step");

function showNextStep() {
    if (current < steps.length) {
        stepDiv.textContent = steps[current];
        current++;
        setTimeout(showNextStep, 5000); // 5 seconds per step
    } else {
        // Redirect to token verification
        window.location.href = "/transfer/token.php";
    }
}

// Start after short delay
setTimeout(showNextStep, 1000);
</script>

<?php
$content = ob_get_clean();
renderLayout("Processing Transfer", $content, $conn);