<?php
/**
 * User's Purchased Library
 * Displays approved orders for the logged-in user.
 */

require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';
require_once 'includes/auth.php';

require_login();

try {
    $stmt = $pdo->prepare("
        SELECT p.id, p.title, p.slug, p.main_image, p.version, o.approved_at 
        FROM orders o 
        JOIN products p ON o.product_id = p.id 
        WHERE o.user_id = :uid AND o.status = 'approved' 
        ORDER BY o.approved_at DESC
    ");
    $stmt->execute([':uid' => $_SESSION['user_id']]);
    $purchased_products = $stmt->fetchAll();
} catch (PDOException $e) {
    error_log("My Products Fetch Error: " . $e->getMessage());
    $purchased_products = [];
}

$page_title = "My Library";
require_once 'includes/header.php';
?>

<style>
    .page-header { margin-bottom: 40px; padding-bottom: 24px; border-bottom: 1px solid var(--border-color); }
    
    /* Product Grid: Desktop View (3 per row) */
    .product-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 32px;
        margin-bottom: 48px;
    }
    .product-card { display: flex; flex-direction: column; height: 100%; padding: 0; overflow: hidden; border-color: rgba(16, 185, 129, 0.3); }
    .product-image-wrapper { width: 100%; height: 200px; overflow: hidden; border-bottom: 1px solid var(--border-color); background: #000; }
    .product-image { width: 100%; height: 100%; object-fit: cover; transition: transform var(--transition-smooth); }
    .product-card:hover .product-image { transform: scale(1.05); }
    .product-content { padding: 24px; flex: 1; display: flex; flex-direction: column; }
    .product-title { font-size: 1.25rem; margin-bottom: 8px; }
    .product-title a { color: var(--text-primary); }
    .product-meta-info { color: #10b981; font-size: 0.85rem; margin-bottom: 16px; font-weight: 500; }
    .product-footer { margin-top: auto; display: flex; gap: 12px; }
    .product-footer .btn { flex: 1; text-align: center; padding: 10px 0; font-size: 0.9rem; }

    /* Responsive Mobile Fixes */
    @media (max-width: 768px) {
        .product-grid {
            grid-template-columns: repeat(2, 1fr);
            gap: 12px;
        }
        .product-image-wrapper { height: 130px; }
        .product-content { padding: 12px; }
        .product-title { font-size: 0.95rem; margin-bottom: 4px; line-height: 1.3; }
        .product-meta-info { font-size: 0.75rem; margin-bottom: 12px; }
        .product-footer { flex-direction: column; gap: 8px; }
        .product-footer .btn { padding: 6px 10px; font-size: 0.8rem; }
    }
</style>

<div class="container" style="padding: 40px 15px;">
    
    <div class="page-header">
        <h1 style="font-size: 2.5rem; margin-bottom: 8px;">My Library</h1>
        <p class="text-secondary">Access your purchased source codes and premium setup videos here.</p>
    </div>

    <?php if (!empty($purchased_products)): ?>
        <div class="product-grid">
            <?php foreach ($purchased_products as $product): ?>
                <div class="glass-card product-card">
                    <div class="product-image-wrapper">
                        <a href="<?php echo SITE_URL; ?>/product.php?slug=<?php echo htmlspecialchars($product['slug']); ?>">
                            <img src="<?php echo SITE_URL . '/storage/product_images/' . htmlspecialchars($product['main_image']); ?>" class="product-image" onerror="this.src='<?php echo SITE_URL; ?>/assets/images/placeholder.jpg';">
                        </a>
                    </div>
                    <div class="product-content">
                        <h3 class="product-title">
                            <a href="<?php echo SITE_URL; ?>/product.php?slug=<?php echo htmlspecialchars($product['slug']); ?>"><?php echo htmlspecialchars($product['title']); ?></a>
                        </h3>
                        <div class="product-meta-info">
                            Access Granted • v<?php echo htmlspecialchars($product['version']); ?>
                        </div>
                        <div class="product-footer">
                            <a href="<?php echo SITE_URL; ?>/download.php?id=<?php echo $product['id']; ?>" class="btn btn-glow">Download File</a>
                            <a href="<?php echo SITE_URL; ?>/product.php?slug=<?php echo htmlspecialchars($product['slug']); ?>" class="btn btn-outline">Watch Setup</a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="glass-card" style="text-align: center; padding: 80px 24px; border-style: dashed;">
            <svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="var(--text-muted)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" style="margin-bottom: 16px;"><path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"></path></svg>
            <h3 style="font-size: 1.5rem; margin-bottom: 8px;">Your library is empty</h3>
            <p class="text-secondary">You haven't purchased any products yet, or your orders are still pending approval.</p>
            <a href="<?php echo SITE_URL; ?>/products.php" class="btn btn-glow" style="margin-top: 24px;">Browse Catalog</a>
        </div>
    <?php endif; ?>

</div>

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