<?php
session_start();

/*
 * Confluence King - Pricing Page
 * V2.0 - FIX: Removed 'currency' column from SQL select to match database schema.
 * Hardcoded GBP symbol (£).
 */

// --- CRITICAL CONFIGURATION INCLUDES ---
require_once 'api/db_config.php';
require_once 'api/config_helpers.php'; 

// Check if user is logged in
$is_logged_in = isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true;

$conn = getDbConnection();
$products = [];

// Initialize action links
$cta_register = 'register.php';
$cta_dashboard = 'dashboard.php';
$cta_checkout_base = 'create_checkout_session.php?product_id='; 

if ($conn) {
    // FIX: Removed 'currency' from the SELECT list
    $query = "SELECT id, name, description, image_url, stripe_price_id, price_amount, billing_interval, features_json 
              FROM products 
              WHERE is_active = 1 
              ORDER BY sort_order ASC, price_amount ASC";
              
    $result = $conn->query($query);
    
    if ($result) {
        while($row = $result->fetch_assoc()) {
            $products[] = $row;
        }
    }
    $conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pricing Plans - Confluence King</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>body { font-family: 'Inter', sans-serif; }</style>
</head>
<body class="bg-gray-900 text-gray-200">

    <?php 
    // Include Navbar
    require_once 'includes/header.php'; 
    ?>

    <main class="py-16">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center mb-12">
                <h2 class="text-4xl font-extrabold text-white">Find Your Perfect Plan</h2>
                <p class="mt-3 text-xl text-gray-400">
                    All paid plans include a secure, guaranteed service level.
                </p>
            </div>

            <div class="mt-10 grid grid-cols-1 gap-10 lg:grid-cols-4">
                
                <div class="bg-gray-800 rounded-lg shadow-2xl p-6 flex flex-col border-2 border-gray-600">
                    <h3 class="text-2xl font-bold text-white">Free Trial</h3>
                    <p class="mt-2 text-sm text-gray-400">Test the platform with full power for 7 days.</p>
                    <div class="mt-6 mb-8">
                        <span class="text-5xl font-extrabold text-white">£0</span>
                        <span class="text-base font-medium text-gray-400">/ 7 days</span>
                    </div>
                    
                    <ul class="space-y-3 text-sm flex-grow">
                        <li class="flex items-center text-yellow-400"><svg class="flex-shrink-0 h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg><span class="ml-3">Full Webhook Functionality</span></li>
                        <li class="flex items-center text-yellow-400"><svg class="flex-shrink-0 h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg><span class="ml-3">7-Day Expiration</span></li>
                        <li class="flex items-center text-red-400"><svg class="flex-shrink-0 h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" /></svg><span class="ml-3">No Advanced Features</span></li>
                    </ul>

                    <div class="mt-8">
                        <?php if ($is_logged_in): ?>
                            <a href="<?php echo htmlspecialchars($cta_dashboard); ?>" class="block w-full text-center px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-green-600 hover:bg-green-700">
                                Go to Dashboard
                            </a>
                        <?php else: ?>
                            <a href="<?php echo htmlspecialchars($cta_register); ?>" class="block w-full text-center px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700">
                                Start 7-Day Trial
                            </a>
                        <?php endif; ?>
                    </div>
                </div>

                <?php 
                // 2. DYNAMICALLY RENDER PAID PLANS
                foreach ($products as $product):
                    $is_ultimate = strpos($product['name'], 'Ultimate') !== false;
                    $border_class = $is_ultimate ? 'border-4 border-purple-500' : 'border-2 border-gray-700';
                    
                    // Decode features safely
                    $features = json_decode($product['features_json'], true);
                    if (!is_array($features)) { $features = []; }
                ?>
                <div class="bg-gray-800 rounded-lg shadow-2xl p-6 flex flex-col <?php echo $border_class; ?>">
                    
                    <?php if (!empty($product['image_url'])): ?>
                    <div class="mb-4 text-center">
                        <img src="<?php echo htmlspecialchars($product['image_url']); ?>" alt="Thumbnail" 
                             class="mx-auto h-20 w-20 rounded-full object-cover border border-blue-500">
                    </div>
                    <?php endif; ?>

                    <h3 class="text-2xl font-bold text-white"><?php echo htmlspecialchars($product['name']); ?></h3>
                    <p class="mt-2 text-sm text-gray-400">
                        
                      <?php echo formatDescription($product['description']); ?>
                    </p>
                    
                    <div class="mt-6 mb-8">
                        <span class="text-5xl font-extrabold text-white">£<?php echo number_format($product['price_amount'], 2); ?></span>
                        <span class="text-base font-medium text-gray-400">/ <?php echo ucfirst(htmlspecialchars($product['billing_interval'])); ?></span>
                    </div>

                    <ul class="space-y-3 text-sm flex-grow">
                        <?php foreach($features as $feature): ?>
                        <li class="flex items-center <?php echo $is_ultimate ? 'text-purple-400' : 'text-green-400'; ?>">
                            <svg class="flex-shrink-0 h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg>
                            <span class="ml-3"><?php echo htmlspecialchars($feature); ?></span>
                        </li>
                        <?php endforeach; ?>
                    </ul>

                    <div class="mt-8">
                        <?php if ($is_logged_in): ?>
                            <a href="<?php echo htmlspecialchars($cta_checkout_base . $product['id']); ?>" 
                               class="block w-full text-center px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700">
                                Upgrade Now
                            </a>
                        <?php else: ?>
                            <a href="<?php echo htmlspecialchars($cta_register); ?>" 
                               class="block w-full text-center px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-gray-600 hover:bg-gray-700">
                                Log in to Subscribe
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endforeach; ?>
                
            </div>
        </div>
    </main>
</body>
</html>