<?php
// Use the explicit category variables from the controller
// These variables are set in CategoryController::show()
// $categoryName, $categorySlug, $categoryId are available

// Set page title and description using the explicit category name
$pageTitle = htmlspecialchars($categoryName) . ' Games';
$pageDescription = 'Play the best ' . htmlspecialchars($categoryName) . ' games online for free. New games added daily!';

// Include the header
include __DIR__ . '/layout/header.php';
?>

<div class="container mx-auto px-4 py-8">
    <div class="mb-8">
        <nav class="flex mb-4" aria-label="Breadcrumb">
            <ol class="inline-flex items-center space-x-1 md:space-x-3">
                <li class="inline-flex items-center">
                    <a href="/" class="text-gray-400 hover:text-white transition-colors">
                        <i class="fas fa-home mr-2"></i>Home
                    </a>
                </li>
                <li class="breadcrumb-item">
                    <div class="flex items-center">
                        <i class="fas fa-chevron-right text-gray-400 mx-2 text-xs"></i>
                        <a href="/categories" class="text-gray-400 hover:text-white transition-colors">
                            <span class="breadcrumb-text">Categories</span>
                        </a>
                    </div>
                </li>
                <li aria-current="page" class="breadcrumb-item">
                    <div class="flex items-center">
                        <i class="fas fa-chevron-right text-gray-400 mx-2 text-xs"></i>
                        <span class="text-white font-medium breadcrumb-text-last"><?= htmlspecialchars($categoryName) ?></span>
                    </div>
                </li>
            </ol>
        </nav>

        <div class="hero-section">
            <h1 class="text-3xl font-bold mb-2 category-title"><?= htmlspecialchars($categoryName) ?> Games</h1>
            <p class="text-lg text-gray-200">
                Browse and play the best <?= htmlspecialchars($categoryName) ?> games online for free.
                We have a collection of <?= $totalGames ?> <?= htmlspecialchars($categoryName) ?> games for you to enjoy.
            </p>
        </div>
    </div>

    <!-- Category Page Ad -->
    <?php if (isset($settings['enable_ads']) && $settings['enable_ads'] == '1' && isset($settings['category_page_ad']) && !empty($settings['category_page_ad'])): ?>
    <div class="ad-container category-page-ad mb-8">
        <?= $settings['category_page_ad'] ?>
    </div>
    <?php endif; ?>

    <!-- Games Grid -->
    <?php if (count($games) > 0): ?>
    <div class="game-grid mb-8">
        <?php foreach ($games as $game): ?>
        <a href="/game/<?= $game['slug'] ?>" class="game-card group">
            <div class="relative aspect-square overflow-hidden rounded-xl">
                <img src="<?= htmlspecialchars($game['thumbnail']) ?>" alt="<?= htmlspecialchars($game['title']) ?>"
                     class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-110" loading="lazy">
                <div class="absolute top-0 left-0 m-3 z-1">
                    <span class="category-badge">
                        <i class="fas fa-tag mr-1"></i>
                        <?= htmlspecialchars($game['category_name']) ?>
                    </span>
                </div>
                <div class="game-card-overlay">
                    <h3 class="game-card-title"><?= htmlspecialchars($game['title']) ?></h3>
                </div>
                <div class="absolute inset-0 bg-gradient-to-t from-black/80 to-transparent opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center">
                    <button class="w-10 h-10 bg-primary rounded-full flex items-center justify-center">
                        <i class="fas fa-play text-white"></i>
                    </button>
                </div>
            </div>
        </a>
        <?php endforeach; ?>
    </div>

    <!-- Between Content Ad -->
    <?php if (isset($settings['enable_ads']) && $settings['enable_ads'] == '1' && isset($settings['between_content_ad']) && !empty($settings['between_content_ad'])): ?>
    <div class="ad-container between-content-ad mb-8">
        <?= $settings['between_content_ad'] ?>
    </div>
    <?php endif; ?>

    <!-- Pagination -->
    <?php if ($totalPages > 1): ?>
    <div class="mt-8 flex justify-center">
        <div class="inline-flex rounded-full overflow-hidden shadow-lg">
            <?php if ($page > 1): ?>
                <a href="/category/<?= $categorySlug ?>?page=<?= $page - 1 ?>" class="px-4 py-2 text-sm font-medium text-white bg-card-bg hover:bg-card-hover transition-colors">
                    <i class="fas fa-chevron-left mr-1"></i> Previous
                </a>
            <?php else: ?>
                <span class="px-4 py-2 text-sm font-medium text-gray-500 bg-card-bg cursor-not-allowed">
                    <i class="fas fa-chevron-left mr-1"></i> Previous
                </span>
            <?php endif; ?>

            <span class="px-4 py-2 text-sm font-medium text-white bg-primary">
                Page <?= $page ?> of <?= $totalPages ?>
            </span>

            <?php if ($page < $totalPages): ?>
                <a href="/category/<?= $categorySlug ?>?page=<?= $page + 1 ?>" class="px-4 py-2 text-sm font-medium text-white bg-card-bg hover:bg-card-hover transition-colors">
                    Next <i class="fas fa-chevron-right ml-1"></i>
                </a>
            <?php else: ?>
                <span class="px-4 py-2 text-sm font-medium text-gray-500 bg-card-bg cursor-not-allowed">
                    Next <i class="fas fa-chevron-right ml-1"></i>
                </span>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>
    <?php else: ?>
    <div class="text-center py-12">
        <div class="text-5xl mb-4"><i class="fas fa-gamepad opacity-30"></i></div>
        <h2 class="text-2xl font-bold mb-2">No Games Found</h2>
        <p class="text-gray-400">We couldn't find any games in this category. Please check back later.</p>
    </div>
        <p class="text-gray-400 mb-4">We couldn't find any games in this category.</p>
        <a href="/" class="btn-primary">
            <i class="fas fa-home mr-2"></i>Return to Home
        </a>
    </div>
    <?php endif; ?>

    <!-- Other Categories -->
    <div class="mt-12">
        <h2 class="section-title">
            <i class="fas fa-th-large"></i>Other Categories
        </h2>

        <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4">
            <?php foreach ($popularCategories as $popularCategory): ?>
            <?php if ($popularCategory['id'] != $category['id']): ?>
            <a href="/category/<?= $popularCategory['slug'] ?>" class="bg-card-bg hover:bg-card-hover rounded-xl p-4 text-center transition-all">
                <div class="w-12 h-12 bg-primary/20 rounded-full flex items-center justify-center mx-auto mb-3">
                    <i class="fas fa-gamepad text-primary"></i>
                </div>
                <h3 class="font-medium"><?= htmlspecialchars($popularCategory['name']) ?></h3>
                <?php if (isset($popularCategory['game_count'])): ?>
                <p class="text-sm text-gray-400 mt-1"><?= $popularCategory['game_count'] ?> Games</p>
                <?php endif; ?>
            </a>
            <?php endif; ?>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<!-- We no longer need the JavaScript fix since we're handling this server-side -->

<?php include __DIR__ . '/layout/footer.php'; ?>
