<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
        xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"
        xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">
    
    <!-- Página Principal -->
    <url>
        <loc>https://acheiaqui.net.br/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Páginas Estáticas -->
    <url>
        <loc>https://acheiaqui.net.br/buscar.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <!-- Seções do Site -->
    <url>
        <loc>https://acheiaqui.net.br/#comercios</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc>https://acheiaqui.net.br/#servicos</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc>https://acheiaqui.net.br/#produtos</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc>https://acheiaqui.net.br/#topatudo</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    
    <!-- Páginas Dinâmicas (Comércios) -->
    <?php
    // Buscar comércios para adicionar no sitemap
    try {
        require_once 'config/database.php';
        $db = Database::getInstance()->getConnection();
        
        $tables = ['comercios', 'servicos', 'produtos', 'topatudo'];
        foreach ($tables as $table) {
            $stmt = $db->query("SELECT id, titulo, data_cadastro FROM $table ORDER BY id DESC LIMIT 50");
            $items = $stmt->fetchAll(PDO::FETCH_ASSOC);
            
            foreach ($items as $item) {
                $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $item['titulo'])));
                ?>
                <url>
                    <loc>https://acheiaqui.net.br/<?php echo $table; ?>/<?php echo $item['id']; ?>/<?php echo $slug; ?></loc>
                    <lastmod><?php echo date('Y-m-d', strtotime($item['data_cadastro'])); ?></lastmod>
                    <changefreq>weekly</changefreq>
                    <priority>0.6</priority>
                </url>
                <?php
            }
        }
    } catch (Exception $e) {
        // Ignora erro se tabela não existir
    }
    ?>
    
</urlset>