<?php
require_once __DIR__ . '/server/conn.php';

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

$site_url = 'https://seamaraboattrips.com';
$today    = date('Y-m-d');

// Fetch tours for dynamic URLs
$tours = $pdo->query("SELECT title_en FROM tours ORDER BY id ASC")->fetchAll();

function makeSlug($str) {
    $str = strtolower(trim($str));
    $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    $str = preg_replace('/[^a-z0-9]+/', '-', $str);
    return trim($str, '-');
}

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

    <!-- ══ STATIC PAGES ════════════════════════════════════════════════════ -->
    <url>
        <loc><?= $site_url ?>/</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
        <xhtml:link rel="alternate" hreflang="en" href="<?= $site_url ?>/"/>
        <xhtml:link rel="alternate" hreflang="it" href="<?= $site_url ?>/"/>
        <xhtml:link rel="alternate" hreflang="es" href="<?= $site_url ?>/"/>
    </url>

    <url>
        <loc><?= $site_url ?>/tours</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.95</priority>
    </url>

    <url>
        <loc><?= $site_url ?>/booking</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.90</priority>
    </url>

    <url>
        <loc><?= $site_url ?>/about</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.80</priority>
    </url>

    <url>
        <loc><?= $site_url ?>/contact</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.75</priority>
    </url>

    <url>
        <loc><?= $site_url ?>/faq</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.70</priority>
    </url>

    <!-- ══ TOUR PAGES ═══════════════════════════════════════════════════════ -->
    <?php foreach ($tours as $t):
        $slug = makeSlug($t['title_en']);
    ?>
    <url>
        <loc><?= $site_url ?>/tour/<?= htmlspecialchars($slug) ?></loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.90</priority>
        <xhtml:link rel="alternate" hreflang="en" href="<?= $site_url ?>/tour/<?= htmlspecialchars($slug) ?>"/>
        <xhtml:link rel="alternate" hreflang="it" href="<?= $site_url ?>/tour/<?= htmlspecialchars($slug) ?>"/>
        <xhtml:link rel="alternate" hreflang="es" href="<?= $site_url ?>/tour/<?= htmlspecialchars($slug) ?>"/>
    </url>
    <?php endforeach; ?>

</urlset>
