<?php
/**
 * Sitemap Dinâmico - Lampa Imóveis
 */

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

require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/app/Core/Database.php';

$db = Database::getInstance();
$baseUrl = BASE_URL;

$urls = [
    ['loc' => $baseUrl . '/index.php?route=home', 'changefreq' => 'daily', 'priority' => '1.0'],
    ['loc' => $baseUrl . '/index.php?route=imoveis', 'changefreq' => 'daily', 'priority' => '0.9'],
    ['loc' => $baseUrl . '/index.php?route=imoveis&finalidade=venda', 'changefreq' => 'daily', 'priority' => '0.9'],
    ['loc' => $baseUrl . '/index.php?route=imoveis&finalidade=aluguel', 'changefreq' => 'daily', 'priority' => '0.9'],
    ['loc' => $baseUrl . '/index.php?route=lancamentos', 'changefreq' => 'weekly', 'priority' => '0.9'],
    ['loc' => $baseUrl . '/index.php?route=anunciar', 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['loc' => $baseUrl . '/index.php?route=contato', 'changefreq' => 'monthly', 'priority' => '0.7'],
];

$imoveis = $db->query("SELECT slug, updated_at FROM imoveis WHERE status_imovel = 'disponivel' ORDER BY id DESC LIMIT 1000")->all();

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

foreach ($urls as $url) {
    echo '<url>';
    echo '<loc>' . htmlspecialchars($url['loc']) . '</loc>';
    echo '<changefreq>' . $url['changefreq'] . '</changefreq>';
    echo '<priority>' . $url['priority'] . '</priority>';
    echo '</url>';
}

foreach ($imoveis as $imov) {
    $date = !empty($imov['updated_at']) ? date('c', strtotime($imov['updated_at'])) : date('c');
    echo '<url>';
    echo '<loc>' . htmlspecialchars($baseUrl . '/index.php?route=imovel/' . urlencode($imov['slug'])) . '</loc>';
    echo '<lastmod>' . $date . '</lastmod>';
    echo '<changefreq>weekly</changefreq>';
    echo '<priority>0.8</priority>';
    echo '</url>';
}

echo '</urlset>';
