<?php
/**
 * Генератор Sitemap.xml
 */

require_once __DIR__ . '/config/database.php';

header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Главная -->
    <url>
        <loc>https://new.tfsr.by/</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Каталог -->
    <url>
        <loc>https://new.tfsr.by/catalog</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <!-- О нас -->
    <url>
        <loc>https://new.tfsr.by/about</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <!-- Контакты -->
    <url>
        <loc>https://new.tfsr.by/contact</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <!-- Вход -->
    <url>
        <loc>https://new.tfsr.by/login</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
    
    <?php
    // Товары
    $stmt = $pdo->query("
        SELECT id, slug, updated_at 
        FROM products 
        WHERE status = 'active' OR status IS NULL
        ORDER BY id
    ");
    
    while ($row = $stmt->fetch()) {
        $slug = $row['slug'] ?? '';
        if (empty($slug)) {
            $slug = 'product-' . $row['id'];
        }
        $url = 'https://new.tfsr.by/product/' . $row['id'] . '-' . $slug . '/';
        $date = $row['updated_at'] ? date('Y-m-d', strtotime($row['updated_at'])) : date('Y-m-d');
        ?>
        <url>
            <loc><?= htmlspecialchars($url) ?></loc>
            <lastmod><?= $date ?></lastmod>
            <changefreq>weekly</changefreq>
            <priority>0.8</priority>
        </url>
        <?php
    }
    ?>
</urlset>