<?php
if (session_status() === PHP_SESSION_NONE) session_start();
require_once __DIR__ . '/../../../config/config.php';

if (!isset($_SESSION['invoice_data']) || !isset($_SESSION['user_id'])) {
    header("Location: ../user/billing.php");
    exit();
}

$invoice = $_SESSION['invoice_data'];
$user_id = $_SESSION['user_id'];

// Fetch live Stripe Publishable Key from Database
$stmt = $pdo->query("SELECT key_value FROM payment_settings WHERE key_name = 'stripe_public_key'");
$publishable_key = $stmt->fetchColumn();
?>
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
    <meta charset="UTF-8">
    <title>Stripe Checkout - Football Pred Pro</title>
    <script src="https://js.stripe.com/v3/"></script>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-[#020617] text-white flex flex-col items-center justify-center min-h-screen">
    <div class="bg-slate-900 border border-slate-800 rounded-3xl p-8 max-w-md w-full text-center shadow-2xl">
        <h3 class="text-lg font-black tracking-wider uppercase text-indigo-400 mb-2">🏦 Stripe Secured Checkout</h3>
        <p class="text-xs text-slate-400 mb-6">Processing global card payments securely.</p>

        <div class="bg-slate-950 p-4 rounded-2xl border border-slate-800 mb-6 flex justify-between items-center text-xs font-bold">
            <span class="text-slate-400">Subscription Package</span>
            <span class="text-indigo-400"><?= htmlspecialchars($invoice['plan_name']); ?></span>
        </div>

        <button id="checkout-button" class="w-full bg-gradient-to-r from-indigo-600 to-indigo-500 hover:from-indigo-500 hover:to-indigo-400 text-white font-black py-4 rounded-2xl text-xs uppercase tracking-widest transition shadow-lg">Proceed to Stripe Checkout</button>
        
        <a href="../user/billing.php" class="block mt-6 text-[9px] font-black tracking-widest uppercase text-slate-500 hover:text-white transition">Cancel & Return to Billing Ledger</a>
    </div>

    <script type="text/javascript">
        var stripe = Stripe("<?= $publishable_key; ?>");
        var checkoutButton = document.getElementById("checkout-button");

        checkoutButton.addEventListener("click", function () {
            // Call your backend to create the Stripe Checkout Session
            fetch("create-stripe-session.php", {
                method: "POST",
            })
            .then(function (response) {
                return response.json();
            })
            .then(function (session) {
                if (session.error) {
                    alert(session.error);
                    return;
                }
                return stripe.redirectToCheckout({ sessionId: session.id });
            })
            .then(function (result) {
                if (result.error) {
                    alert(result.error.message);
                }
            })
            .catch(function (error) {
                console.error("Error:", error);
            });
        });
    </script>
</body>
</html>