<?php
ini_set('display_errors', 0);

require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';

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

$user_id = $_SESSION['user_id'] ?? null;
if (!$user_id) {
    header("Location: /auth/login.php");
    exit;
}

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

// ==========================
// GET TRANSFER
// ==========================
$transfer_id = intval($_GET['transfer_id'] ?? 0);

$stmt = $conn->prepare("
    SELECT * 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;
}

// ==========================
// CURRENCY SYMBOL
// ==========================
$symbols = [
    'USD'=>'$','EUR'=>'€','GBP'=>'£','JPY'=>'¥','CNY'=>'¥',
    'NGN'=>'₦','GHS'=>'₵','INR'=>'₹','CAD'=>'C$','AUD'=>'A$'
];

$symbol = $symbols[$transfer['currency']] ?? '$';

// ==========================
// HANDLE ACTIONS
// ==========================
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // CANCEL
    if (isset($_POST['cancel'])) {
        $stmt = $conn->prepare("
            UPDATE transfers 
            SET status = 'cancelled' 
            WHERE id = ? AND user_id = ?
        ");
        $stmt->bind_param("ii", $transfer_id, $user_id);
        $stmt->execute();
        $stmt->close();

        header("Location: /user/processing.php");
        exit;
    }

    // CONFIRM → GO TO TOKEN VERIFICATION
if (isset($_POST['confirm'])) {

    // MARK AS CONFIRMED FOR ADMIN
    $stmt = $conn->prepare("
        UPDATE transfers
        SET 
            admin_reference = 1,
            confirmed_at = NOW(),
            status = 'processing'
        WHERE id = ? AND user_id = ?
    ");

    $stmt->bind_param("ii", $transfer_id, $user_id);
    $stmt->execute();
    $stmt->close();

    // SAVE ACTIVE TRANSFER
    $_SESSION['transfer_id'] = $transfer_id;
    $_SESSION['user_id'] = $user_id;

    // CONTINUE TRANSFER
    header("Location: /transfer/processing.php");
    exit;
}
}

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

<h2>🔐 Confirm Transfer</h2>

<div style="background:#fff;padding:20px;border-radius:8px;max-width:600px;">
    
    <p><strong>Transfer Type:</strong> <?= htmlspecialchars($transfer['type']) ?></p>
    <p><strong>From Account:</strong> <?= htmlspecialchars($transfer['from_account']) ?></p>
    <p><strong>Recipient Name:</strong> <?= htmlspecialchars($transfer['recipient_name']) ?></p>
    <p><strong>Recipient Account:</strong> <?= htmlspecialchars($transfer['recipient_account']) ?></p>
    <p><strong>Bank:</strong> <?= htmlspecialchars($transfer['recipient_bank']) ?></p>

    <?php if ($transfer['type'] === 'international'): ?>
        <p><strong>IBAN:</strong> <?= htmlspecialchars($transfer['iban']) ?></p>
        <p><strong>SWIFT:</strong> <?= htmlspecialchars($transfer['swift_code']) ?></p>
        <p><strong>Routing:</strong> <?= htmlspecialchars($transfer['routing']) ?></p>
    <?php endif; ?>

    <hr>

    <p><strong>Amount:</strong> <?= $symbol . number_format((float)$transfer['amount'], 2) ?></p>
    <p><strong>Fee:</strong> <?= $symbol . number_format((float)$transfer['fee'], 2) ?></p>
    <p><strong>Total:</strong> <?= $symbol . number_format((float)$transfer['total'], 2) ?></p>

    <hr>

    <form method="POST" style="display:flex;gap:10px;margin-top:20px;">
        <button type="submit" name="confirm" 
            style="background:#1a73e8;color:white;padding:10px 20px;border:none;border-radius:5px;">
            ✅ Confirm
        </button>

        <button type="submit" name="cancel" 
            style="background:#dc3545;color:white;padding:10px 20px;border:none;border-radius:5px;">
            ❌ Cancel
        </button>
    </form>

</div>

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