<?php
/**
 * Agentic Square - Transaction Cancellation Hub Exception Handler
 * Path: cancel-payment.php (Main Directory)
 */

require_once __DIR__ . '/config/config.php';

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

// Redirect if unauthenticated
if (!isset($_SESSION['user_id'])) {
    header('Location: ' . app_url('login.php'));
    exit;
}

$order_id = isset($_GET['order_id']) ? (int)$_GET['order_id'] : 0;

try {
    if ($order_id > 0) {
        /**
         * LEAK PROTECTION HANDLER
         * Flag transaction frames intentionally as Cancelled inside orders records matrix safely.
         */
        $checkStmt = $pdo->prepare("SELECT id, item_id FROM orders WHERE id = ? AND user_id = ? LIMIT 1");
        $checkStmt->execute([$order_id, (int)$_SESSION['user_id']]);
        $order = $checkStmt->fetch();

        if ($order) {
            $update = $pdo->prepare("UPDATE orders SET payment_status = 'Cancelled', updated_at = CURRENT_TIMESTAMP WHERE id = ? AND payment_status = 'Pending'");
            $update->execute([$order_id]);
            $fallback_item_id = $order['item_id'];
        }
    }
} catch (Exception $e) {
    // Log exception safely or allow flow execution to continue to interface elements
}

require_once __DIR__ . '/sections/header.php';
require_once __DIR__ . '/sections/navbar.php';
?>

<div class="bg-nftDark text-white antialiased min-h-screen pt-28 pb-20 flex items-center justify-center">
    <div class="max-w-md w-full mx-auto px-4">
        
        <div class="glass-card p-8 rounded-3xl border border-rose-500/20 shadow-2xl space-y-6 text-center relative overflow-hidden bg-gradient-to-b from-slate-950/60 to-slate-950/90">
            
            <div class="w-16 h-16 bg-rose-500/10 border border-rose-500/20 text-rose-400 rounded-full flex items-center justify-center text-2xl mx-auto shadow-lg shadow-rose-500/5">
                <i class="fa-solid fa-rectangle-xmark"></i>
            </div>

            <div class="space-y-2">
                <h1 class="text-2xl font-black tracking-tight text-white">Transaction Interrupted</h1>
                <p class="text-xs text-nftTextGray leading-relaxed mx-auto">The payment processing authorization loop was terminated or abandoned by the user profile choice. No funds have been transferred.</p>
            </div>

            <div class="border-t border-slate-900/80 pt-4 space-y-3 text-xs font-bold uppercase tracking-widest">
                <?php if (isset($fallback_item_id)): ?>
                    <a href="<?php echo htmlspecialchars(app_url('buy-now.php?item_id=' . (int)$fallback_item_id), ENT_QUOTES, 'UTF-8'); ?>" class="w-full bg-gradient-to-r from-rose-500/10 to-orange-500/10 hover:from-rose-500/20 hover:to-orange-500/20 text-rose-400 border border-rose-500/20 py-4 rounded-xl block transition-all">
                        <i class="fa-solid fa-rotate-left mr-1.5 text-[10px]"></i> Restart Checkout Module
                    </a>
                <?php endif; ?>
                
                <a href="<?php echo htmlspecialchars(app_url('index.php'), ENT_QUOTES, 'UTF-8'); ?>" class="w-full bg-slate-950 hover:bg-slate-900 border border-slate-900 text-slate-400 hover:text-white py-4 rounded-xl block transition-colors">
                    Return to Marketplace
                </a>
            </div>

        </div>
        
    </div>
</div>

<?php 
require_once __DIR__ . '/sections/footer.php';
?>
