<?php
/**
 * sitemap.xml — Gerado dinamicamente a partir do banco de dados.
 * Inclui campeonatos públicos, clubes, jogadores e páginas estáticas.
 */
require_once __DIR__ . '/../app/bootstrap.php';

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

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = (string)($_SERVER['HTTP_HOST'] ?? 'vpcleague.com');
$base = $protocol . '://' . $host;

function smap_url(string $loc, string $changefreq = 'weekly', string $priority = '0.6', string $lastmod = ''): string {
    $out = "\n  <url>\n    <loc>" . htmlspecialchars($loc, ENT_XML1) . "</loc>";
    if ($lastmod) $out .= "\n    <lastmod>" . $lastmod . "</lastmod>";
    $out .= "\n    <changefreq>" . $changefreq . "</changefreq>";
    $out .= "\n    <priority>" . $priority . "</priority>";
    $out .= "\n  </url>";
    return $out;
}

$today = date('Y-m-d');

$urls = '';

// ── Páginas estáticas ──────────────────────────────────────────────────────
$staticPages = [
    ['/', 'daily', '1.0'],
    ['/campeonatos', 'daily', '0.9'],
    ['/torneios', 'weekly', '0.8'],
    ['/ranking', 'daily', '0.9'],
    ['/jogadores', 'weekly', '0.8'],
    ['/transferencias', 'weekly', '0.7'],
    ['/anuncios', 'weekly', '0.7'],
    ['/sala-dos-campeoes', 'monthly', '0.6'],
    ['/faq', 'monthly', '0.5'],
    ['/clubes-contratando', 'weekly', '0.7'],
];

foreach ($staticPages as [$path, $freq, $prio]) {
    $urls .= smap_url($base . $path, $freq, $prio, $today);
}

// ── Campeonatos online ─────────────────────────────────────────────────────
try {
    $stmt = db()->query("
        SELECT c.id, c.nome, cc.updated_at
        FROM campeonatos c
        LEFT JOIN campeonatos_config cc ON cc.campeonato_id = c.id
        WHERE cc.status = 'online'
        ORDER BY c.id DESC
        LIMIT 200
    ");
    while ($row = $stmt->fetch()) {
        $loc = $base . '/campeonato/' . (int)$row['id'];
        $lm = !empty($row['updated_at']) ? date('Y-m-d', strtotime($row['updated_at'])) : $today;
        $urls .= smap_url($loc, 'daily', '0.8', $lm);
    }
} catch (Throwable $e) {}

// ── Clubes ativos ──────────────────────────────────────────────────────────
try {
    $stmt = db()->query("SELECT id FROM clubes WHERE status = 'ativo' ORDER BY id DESC LIMIT 500");
    while ($row = $stmt->fetch()) {
        $urls .= smap_url($base . '/clube/' . (int)$row['id'], 'weekly', '0.7', $today);
    }
} catch (Throwable $e) {}

// ── Jogadores ──────────────────────────────────────────────────────────────
try {
    $stmt = db()->query("SELECT id, slug FROM usuarios ORDER BY id DESC LIMIT 1000");
    while ($row = $stmt->fetch()) {
        $slug = !empty($row['slug']) ? $row['slug'] : $row['id'];
        $urls .= smap_url($base . '/jogador/' . $slug, 'weekly', '0.6', $today);
    }
} catch (Throwable $e) {}

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