<?php
/**
 * XML Sitemap Generator
 *
 * Generates a dynamic sitemap for search engines
 * Access at: https://foodbankfinder.net/sitemap.xml
 */

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

// Get all food banks for individual pages
try {
    $stmt = $pdo->query("
        SELECT id, updated_at, city, state, name
        FROM food_banks
        ORDER BY updated_at DESC
    ");
    $foodBanks = $stmt->fetchAll();
} catch (PDOException $e) {
    $foodBanks = [];
}

// Get unique states for state pages
try {
    $stateStmt = $pdo->query("
        SELECT DISTINCT state, MAX(updated_at) as updated_at
        FROM food_banks
        GROUP BY state
        ORDER BY state
    ");
    $states = $stateStmt->fetchAll();
} catch (PDOException $e) {
    $states = [];
}

// Get unique cities for city pages
try {
    $cityStmt = $pdo->query("
        SELECT DISTINCT city, state, MAX(updated_at) as updated_at
        FROM food_banks
        GROUP BY city, state
        ORDER BY city
    ");
    $cities = $cityStmt->fetchAll();
} catch (PDOException $e) {
    $cities = [];
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

    <!-- Home Page -->
    <url>
        <loc><?php echo SITE_URL; ?>/index.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <!-- About Page -->
    <url>
        <loc><?php echo SITE_URL; ?>/about.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <!-- Submit Page -->
    <url>
        <loc><?php echo SITE_URL; ?>/submit.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <!-- Emergency Help Pages - HIGHEST PRIORITY -->
    <url>
        <loc><?php echo SITE_URL; ?>/emergency/need-food-today.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <url>
        <loc><?php echo SITE_URL; ?>/emergency/hungry-now.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <url>
        <loc><?php echo SITE_URL; ?>/emergency/open-now.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>hourly</changefreq>
        <priority>1.0</priority>
    </url>

    <url>
        <loc><?php echo SITE_URL; ?>/emergency/weekend-help.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <url>
        <loc><?php echo SITE_URL; ?>/emergency/24-hour-assistance.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>

    <!-- Individual Food Bank Pages -->
    <?php foreach ($foodBanks as $foodBank): ?>
    <url>
        <loc><?php echo SITE_URL; ?>/detail.php?id=<?php echo $foodBank['id']; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($foodBank['updated_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
        <image:image>
            <image:loc><?php echo SITE_URL; ?>/assets/img/logo.png</image:loc>
            <image:title><?php echo htmlspecialchars($foodBank['name']); ?></image:title>
        </image:image>
    </url>
    <?php endforeach; ?>

    <!-- State Pages -->
    <?php foreach ($states as $state): ?>
    <url>
        <loc><?php echo SITE_URL; ?>/index.php?state=<?php echo urlencode($state['state']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($state['updated_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>

    <!-- City Pages -->
    <?php foreach ($cities as $city): ?>
    <url>
        <loc><?php echo SITE_URL; ?>/index.php?city=<?php echo urlencode($city['city']); ?>&amp;state=<?php echo urlencode($city['state']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($city['updated_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>

</urlset>
