<?php
session_start();
/* PAGE: TRADING MARKETPLACE (Category ID 2) */

require_once 'api/db_config.php';
require_once 'api/config_helpers.php'; 

$is_logged_in = isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true;
$conn = getDbConnection();
$products = [];

if ($conn) {
    // FILTER BY CATEGORY ID = 2 (Trading Tools)
    $query = "SELECT id, name, description, image_url, stripe_price_id, price_amount, billing_interval, features_json 
              FROM products 
              WHERE is_active = 1 AND category_id = 2 
              ORDER BY sort_order ASC, price_amount ASC";
              
    $result = $conn->query($query);
    if ($result) {
        while($row = $result->fetch_assoc()) {
            $products[] = $row;
        }
    }
    $conn->close();
}

$cta_checkout_base = 'create_checkout_session.php?product_id='; 
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Trading Tools - 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 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">Trading Marketplace</h2>
                <p class="mt-3 text-xl text-gray-400">
                    Expert Advisors, Pinescript Indicators, and custom trading tools.
                </p>
            </div>

            <div class="mt-10 grid grid-cols-1 gap-10 lg:grid-cols-3">
                <?php if (empty($products)): ?>
                    <div class="col-span-3 text-center text-gray-500 py-10">No trading tools available at the moment. Check back soon!</div>
                <?php endif; ?>

                <?php foreach ($products as $product): 
                    $features = json_decode($product['features_json'], true) ?? [];
                ?>
                <div class="bg-gray-800 rounded-lg shadow-xl overflow-hidden border border-gray-700 flex flex-col">
                    <?php if (!empty($product['image_url'])): ?>
                        <img src="<?= htmlspecialchars($product['image_url']) ?>" class="w-full h-48 object-cover">
                    <?php else: ?>
                        <div class="w-full h-48 bg-gray-700 flex items-center justify-center text-gray-500">No Image</div>
                    <?php endif; ?>
                    
                    <div class="p-6 flex-1 flex flex-col">
                        <h3 class="text-xl font-bold text-white"><?= htmlspecialchars($product['name']) ?></h3>
                        <p class="mt-2 text-sm text-gray-400 flex-1"><?php echo formatDescription($product['description']); ?>
                        
                        <div class="mt-4 mb-4">
                            <span class="text-3xl font-bold text-white">£<?= number_format($product['price_amount'], 2) ?></span>
                            <?php if($product['billing_interval'] != 'once'): ?>
                                <span class="text-sm text-gray-400">/ <?= htmlspecialchars($product['billing_interval']) ?></span>
                            <?php endif; ?>
                        </div>

                        <div class="mt-auto">
                            <?php if ($is_logged_in): ?>
                                <a href="<?= $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-green-600 hover:bg-green-700">
                                    <?= ($product['billing_interval'] == 'once') ? "Buy Now" : "Subscribe" ?>
                                </a>
                            <?php else: ?>
                                <a href="login.php" class="block w-full text-center px-6 py-3 border border-gray-600 text-base font-medium rounded-md shadow-sm text-white bg-gray-700 hover:bg-gray-600">
                                    Log in to Buy
                                </a>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </main>
</body>
</html>