<?php
/**
 * 777menu.com - Menu Items Listing
 */

require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/database.php';
require_once __DIR__ . '/includes/ads.php';

$db = Database::getInstance();

// Pagination
$page = max(1, intval($_GET['page'] ?? 1));
$perPage = ITEMS_PER_PAGE;
$offset = ($page - 1) * $perPage;

// Filters
$search = trim($_GET['q'] ?? '');
$cuisine = trim($_GET['cuisine'] ?? '');
$priceMin = isset($_GET['price_min']) ? floatval($_GET['price_min']) : null;
$priceMax = isset($_GET['price_max']) ? floatval($_GET['price_max']) : null;

// Get menu items
$menus = $db->getMenuItems($perPage, $offset, $search ?: null, $priceMin, $priceMax);
$totalItems = $db->getTotalMenuItems($search ?: null);
$totalPages = ceil($totalItems / $perPage);

// Get cuisines for filter
$cuisines = $db->getCuisines();

$pageTitle = $search ? "Search: $search" : ($cuisine ? ucfirst($cuisine) . " Menus" : "Browse All Menus");
$breadcrumb = [['Home','/'],['Menus','/menus']];
$pageDescription = "Browse " . number_format($totalItems) . " menu items. Find prices, descriptions, and more.";

require_once __DIR__ . '/includes/header.php';
?>

<section class="section" style="padding-top: var(--space-6);">
    <div class="container">
        <div class="section-header">
            <div>
                <h1 class="section-title"><?= e($pageTitle) ?></h1>
                <p class="section-subtitle"><?= number_format($totalItems) ?> menu items found</p>
            </div>
        </div>

        <?php if (empty($search) && empty($cuisine)): ?>
        <div style="max-width: 760px; color: var(--gray-600); line-height: 1.75; margin-bottom: var(--space-6);">
            <p>777Menu is the world's largest menu database — real dishes with the prices restaurants actually charge, pulled straight from their menus. Search by dish, filter by price range or cuisine, and compare what the same plate costs across the country. Every item links back to its restaurant's full menu, so you can see the whole lineup and order.</p>
        </div>
        <?php endif; ?>

        <!-- Filters -->
        <div class="filters">
            <form action="/menus" method="GET" class="filters-row">
                <div class="filter-group" style="flex: 2;">
                    <label class="filter-label">Search</label>
                    <input type="text" name="q" class="filter-input" placeholder="Search menu items..." value="<?= e($search) ?>">
                </div>
                <div class="filter-group">
                    <label class="filter-label">Min Price</label>
                    <input type="number" name="price_min" class="filter-input" placeholder="$0" step="0.01" value="<?= $priceMin ?>">
                </div>
                <div class="filter-group">
                    <label class="filter-label">Max Price</label>
                    <input type="number" name="price_max" class="filter-input" placeholder="$100" step="0.01" value="<?= $priceMax ?>">
                </div>
                <div class="filter-group" style="align-self: flex-end;">
                    <button type="submit" class="btn btn-primary">
                        <span class="material-icons" style="font-size:18px">filter_list</span>
                        Filter
                    </button>
                </div>
            </form>
        </div>

        <?php if ($search || $priceMin || $priceMax): ?>
        <div class="alert alert-info" style="display: flex; justify-content: space-between; align-items: center;">
            <span>
                Showing filtered results
                <?php if ($search): ?> for "<?= e($search) ?>"<?php endif; ?>
                <?php if ($priceMin): ?> from <?= formatPrice($priceMin) ?><?php endif; ?>
                <?php if ($priceMax): ?> to <?= formatPrice($priceMax) ?><?php endif; ?>
            </span>
            <a href="/menus" class="btn btn-sm btn-secondary">Clear Filters</a>
        </div>
        <?php endif; ?>

        <!-- Ad -->
        <?php displayHeaderAd(); ?>

        <?php if (empty($menus)): ?>
        <div class="card">
            <div class="card-body text-center" style="padding: var(--space-16);">
                <span class="material-icons" style="font-size:64px; color: var(--gray-300); margin-bottom: var(--space-4);">search_off</span>
                <h3>No menu items found</h3>
                <p style="color: var(--gray-500); margin-bottom: var(--space-4);">Try adjusting your search or filters</p>
                <a href="/menus" class="btn btn-primary">View All Menus</a>
            </div>
        </div>
        <?php else: ?>

        <!-- Menu Grid -->
        <div class="menu-grid">
            <?php foreach ($menus as $index => $menu): ?>
            <?php if ($index > 0 && $index % 8 === 0): ?>
            <!-- Inline Ad -->
            <div style="grid-column: 1 / -1;"><?php displayInlineAd(); ?></div>
            <?php endif; ?>

            <article class="menu-card">
                <div class="menu-card-image">
                    <span class="material-icons" style="font-size:48px">restaurant_menu</span>
                </div>
                <div class="menu-card-body">
                    <h3 class="menu-card-title"><?= e($menu['name']) ?></h3>
                    <p class="menu-card-desc"><?= e($menu['description']) ?></p>
                    <div class="menu-card-footer">
                        <span class="menu-price"><?= formatPrice($menu['price']) ?></span>
                        <?php if (!empty($menu['is_priority'])): ?>
                        <span class="priority-badge">Featured</span>
                        <?php endif; ?>
                    </div>
                </div>
            </article>
            <?php endforeach; ?>
        </div>

        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
        <nav class="pagination">
            <?php if ($page > 1): ?>
            <a href="?page=<?= $page - 1 ?>&q=<?= urlencode($search) ?>&price_min=<?= $priceMin ?>&price_max=<?= $priceMax ?>">
                <span class="material-icons">chevron_left</span> Prev
            </a>
            <?php endif; ?>

            <?php
            $start = max(1, $page - 2);
            $end = min($totalPages, $page + 2);

            if ($start > 1): ?>
            <a href="?page=1&q=<?= urlencode($search) ?>">1</a>
            <?php if ($start > 2): ?><span>...</span><?php endif; ?>
            <?php endif; ?>

            <?php for ($i = $start; $i <= $end; $i++): ?>
            <a href="?page=<?= $i ?>&q=<?= urlencode($search) ?>&price_min=<?= $priceMin ?>&price_max=<?= $priceMax ?>" class="<?= $i === $page ? 'active' : '' ?>">
                <?= $i ?>
            </a>
            <?php endfor; ?>

            <?php if ($end < $totalPages): ?>
            <?php if ($end < $totalPages - 1): ?><span>...</span><?php endif; ?>
            <a href="?page=<?= $totalPages ?>&q=<?= urlencode($search) ?>"><?= $totalPages ?></a>
            <?php endif; ?>

            <?php if ($page < $totalPages): ?>
            <a href="?page=<?= $page + 1 ?>&q=<?= urlencode($search) ?>&price_min=<?= $priceMin ?>&price_max=<?= $priceMax ?>">
                Next <span class="material-icons">chevron_right</span>
            </a>
            <?php endif; ?>
        </nav>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</section>

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