<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';

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

$base = rtrim(SITE_URL, '/');
$now  = date('Y-m-d');

$urls = [];

// Páginas estáticas
$static = [
    ['/',                    '1.0', 'daily'],
    ['/booking/',            '0.9', 'weekly'],
    ['/pages/services.php', '0.8', 'weekly'],
    ['/pages/team.php',     '0.7', 'monthly'],
    ['/pages/contact.php',  '0.7', 'monthly'],
    ['/pages/faq.php',      '0.6', 'monthly'],
    ['/pages/about.php',    '0.6', 'monthly'],
    ['/pages/terms.php',    '0.3', 'yearly'],
    ['/pages/privacy.php',  '0.3', 'yearly'],
    ['/reviews/',           '0.7', 'weekly'],
    ['/fleet/',             '0.6', 'weekly'],
    ['/destinations/',      '0.7', 'weekly'],
    ['/blog/',              '0.7', 'daily'],
];
foreach ($static as [$path, $pri, $freq]) {
    $urls[] = ['url' => $base . $path, 'priority' => $pri, 'freq' => $freq, 'date' => $now];
}

// Serviços
try {
    $services = DB::fetchAll("SELECT slug, updated_at FROM services WHERE is_active=1");
    foreach ($services as $s) {
        $urls[] = [
            'url'      => $base . '/pages/service-detail.php?slug=' . urlencode($s['slug']),
            'priority' => '0.8',
            'freq'     => 'monthly',
            'date'     => $s['updated_at'] ? date('Y-m-d', strtotime($s['updated_at'])) : $now,
        ];
    }
} catch (Exception $e) {}

// Destinos
try {
    $dests = DB::fetchAll("SELECT slug, updated_at FROM destinations WHERE is_active=1");
    foreach ($dests as $d) {
        $urls[] = [
            'url'      => $base . '/destinations/detail.php?slug=' . urlencode($d['slug']),
            'priority' => '0.7',
            'freq'     => 'monthly',
            'date'     => $d['updated_at'] ? date('Y-m-d', strtotime($d['updated_at'])) : $now,
        ];
    }
} catch (Exception $e) {}

// Blog posts
try {
    $posts = DB::fetchAll("SELECT slug, updated_at FROM blog_posts WHERE status='published' ORDER BY created_at DESC");
    foreach ($posts as $p) {
        $urls[] = [
            'url'      => $base . '/blog/post.php?slug=' . urlencode($p['slug']),
            'priority' => '0.6',
            'freq'     => 'monthly',
            'date'     => $p['updated_at'] ? date('Y-m-d', strtotime($p['updated_at'])) : $now,
        ];
    }
} catch (Exception $e) {}

// Equipa pública
try {
    $members = DB::fetchAll("SELECT slug FROM team_members WHERE is_active=1 AND slug IS NOT NULL AND slug != ''");
    foreach ($members as $m) {
        $urls[] = [
            'url'      => $base . '/pages/team-member.php?slug=' . urlencode($m['slug']),
            'priority' => '0.5',
            'freq'     => 'monthly',
            'date'     => $now,
        ];
    }
} catch (Exception $e) {}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n";
echo '        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n";
echo '        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . "\n";
echo '        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ($urls as $u):
echo "  <url>\n";
echo "    <loc>" . htmlspecialchars($u['url']) . "</loc>\n";
echo "    <lastmod>" . $u['date'] . "</lastmod>\n";
echo "    <changefreq>" . $u['freq'] . "</changefreq>\n";
echo "    <priority>" . $u['priority'] . "</priority>\n";
echo "  </url>\n";
endforeach;
echo '</urlset>';
