<?php
/**
 * Advanced Payment Callback & Order Approval
 * Handles automatic order success/fail from any gateway
 */
require_once 'includes/db.php';

// ১. গেটওয়ে থেকে আসা ট্রানজেকশন আইডি এবং স্ট্যাটাস ধরা
$status = $_GET['status'] ?? '';
$trx_id = $_GET['trx'] ?? '';

if (empty($status) || empty($trx_id)) {
    header("Location: index.php");
    exit;
}

$is_success = false;
$message = '';

if ($status === 'success') {
    try {
        $pdo->beginTransaction();

        // ২. অর্ডারের স্ট্যাটাস 'approved' করা
        $stmt = $pdo->prepare("UPDATE orders SET status = 'approved', approved_at = NOW() WHERE transaction_id = ? AND status = 'pending'");
        $stmt->execute([$trx_id]);

        if ($stmt->rowCount() > 0) {
            // ৩. ট্রানজেকশন হিস্ট্রি রেকর্ড করা
            $stmt_order = $pdo->prepare("SELECT user_id, amount FROM orders WHERE transaction_id = ? LIMIT 1");
            $stmt_order->execute([$trx_id]);
            $order_data = $stmt_order->fetch();

            if ($order_data) {
                $stmt_trx = $pdo->prepare("INSERT INTO transactions (user_id, type, amount, trx_id, status) VALUES (?, 'purchase', ?, ?, 'completed')");
                $stmt_trx->execute([$order_data['user_id'], $order_data['amount'], $trx_id]);
            }
            
            $is_success = true;
            $message = "Your payment was successful and order has been approved!";
        } else {
            $message = "Order already processed or not found.";
        }

        $pdo->commit();
    } catch (PDOException $e) {
        $pdo->rollBack();
        $message = "Database Error: " . $e->getMessage();
    }
} else {
    // পেমেন্ট ফেইল হলে স্ট্যাটাস আপডেট
    $pdo->prepare("UPDATE orders SET status = 'failed' WHERE transaction_id = ?")->execute([$trx_id]);
    $message = "Payment failed or was canceled by user.";
}

// ৪. রেজাল্ট দেখানোর জন্য হেডার লোড করা
$page_title = 'Payment Status';
require_once 'includes/header.php';
?>

<div class="container" style="margin: 60px auto; max-width: 600px; text-align: center;">
    <div style="background: #fff; padding: 40px; border-radius: 15px; border: 1px solid #eee; box-shadow: 0 4px 15px rgba(0,0,0,0.05);">
        
        <?php if ($is_success): ?>
            <div style="color: #10b981; margin-bottom: 20px;">
                <svg width="80" height="80" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
            </div>
            <h1 style="color: #1e293b; margin-bottom: 10px;">Payment Successful!</h1>
        <?php else: ?>
            <div style="color: #ef4444; margin-bottom: 20px;">
                <svg width="80" height="80" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
            </div>
            <h1 style="color: #1e293b; margin-bottom: 10px;">Payment Failed</h1>
        <?php endif; ?>

        <p style="color: #64748b; font-size: 1.1rem; margin-bottom: 30px;"><?php echo $message; ?></p>
        
        <div style="background: #f8fafc; padding: 15px; border-radius: 8px; border: 1px dashed #cbd5e1; margin-bottom: 30px; font-weight: 700;">
            Transaction ID: <span style="color: #2563eb;"><?php echo htmlspecialchars($trx_id); ?></span>
        </div>

        <div style="display: flex; gap: 15px;">
            <a href="account.php?tab=downloads" style="flex: 1; background: #1e3a8a; color: #fff; padding: 14px; border-radius: 8px; font-weight: 700; text-decoration: none;">Go to Downloads</a>
            <a href="index.php" style="flex: 1; border: 2px solid #1e3a8a; color: #1e3a8a; padding: 14px; border-radius: 8px; font-weight: 700; text-decoration: none;">Back Home</a>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>