<?php
/**
 * 777menu.com - XML Sitemap Generator
 */

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

header('Content-Type: application/xml; charset=utf-8');

$db = Database::getInstance();

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Main Pages -->
    <url>
        <loc><?= SITE_URL ?>/</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/menus</loc>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/cuisines</loc>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/cities</loc>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/trending</loc>
        <changefreq>daily</changefreq>
        <priority>0.6</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/restaurants</loc>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/submit</loc>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/api</loc>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>

    <!-- Cuisines -->
    <?php
    $cuisines = $db->getCuisines();
    foreach (array_slice($cuisines, 0, 50) as $cuisine):
    ?>
    <url>
        <loc><?= SITE_URL ?>/menus?cuisine=<?= urlencode($cuisine) ?></loc>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>

    <!-- Restaurants with real menus (allowlist from bin/build_menu_index.php) -->
    <?php
    $menuRestaurants = $db->getMenuRestaurants();
    foreach ($menuRestaurants as $r):
    ?>
    <url>
        <loc><?= SITE_URL ?>/restaurant/<?= $r['id'] ?></loc>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <?php endforeach; ?>

    <!-- Cities -->
    <?php
    $cities = $db->getCities();
    foreach ($cities as $city):
    ?>
    <url>
        <loc><?= SITE_URL ?>/restaurants?city=<?= urlencode($city['city']) ?>&amp;state=<?= urlencode($city['state']) ?></loc>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    <!-- Pricing intelligence (owner-facing, anonymized) -->
    <?php
    $pidx = @json_decode(@file_get_contents(__DIR__ . '/data/pricing_index.json'), true) ?: ['dishes'=>[]];
    ?>
    <url><loc><?= SITE_URL ?>/pricing</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
    <url><loc><?= SITE_URL ?>/design</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <url><loc><?= SITE_URL ?>/design/menu-maker</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <url><loc><?= SITE_URL ?>/methodology</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <?php $pcz=[]; foreach($pidx['dishes'] as $D){ $pcz[$D['cuisine']]=1; } foreach(array_keys($pcz) as $cz): $csl=strtolower(preg_replace('/[^a-z0-9]/i','',$cz)); ?>
    <url><loc><?= SITE_URL ?>/pricing/cuisine/<?= $csl ?></loc><changefreq>weekly</changefreq><priority>0.7</priority></url>
    <?php endforeach; ?>
    <?php foreach ($pidx['dishes'] as $ds => $D): ?>
    <url><loc><?= SITE_URL ?>/pricing/<?= $ds ?></loc><changefreq>weekly</changefreq><priority>0.7</priority></url>
    <?php foreach ($D['cities'] as $ck => $c):
        $st = strtolower($c['state']); $cs = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($c['city'])), '-'); ?>
    <url><loc><?= SITE_URL ?>/pricing/<?= $ds ?>/<?= $st ?>/<?= $cs ?></loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <?php endforeach; endforeach; ?>
</urlset>
