<?php
// sitemap.php
header("Content-Type: application/xml; charset=utf-8");

require_once 'config/database.php';

// Tentukan Domain Utama Website Anda
$baseUrl = "https://trafikkita.com";

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    
    <!-- Halaman Utama / Landing Page -->
    <url>
        <loc><?= $baseUrl; ?>/</loc>
        <lastmod><?= date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <!-- Halaman Blog Index -->
    <url>
        <loc><?= $baseUrl; ?>/blog.php</loc>
        <lastmod><?= date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>

    <!-- Halaman Tentang Kami -->
    <url>
        <loc><?= $baseUrl; ?>/about.php</loc>
        <lastmod>2026-10-03</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>

    <!-- Halaman Login -->
    <url>
        <loc><?= $baseUrl; ?>/login.php</loc>
        <lastmod>2026-10-03</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.4</priority>
    </url>

    <!-- Halaman Register -->
    <url>
        <loc><?= $baseUrl; ?>/register.php</loc>
        <lastmod>2026-10-03</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.4</priority>
    </url>

    <!-- Halaman Kebijakan Privasi & Syarat Ketentuan -->
    <url>
        <loc><?= $baseUrl; ?>/privacy.php</loc>
        <lastmod>2026-10-03</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.3</priority>
    </url>
    <url>
        <loc><?= $baseUrl; ?>/terms.php</loc>
        <lastmod>2026-10-03</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.3</priority>
    </url>

    <!-- ARTIKEL BLOG DINAMIS DARI DATABASE -->
    <?php
    try {
        $stmt = $pdo->query("SELECT slug, created_at FROM blogs WHERE status = 'published' ORDER BY created_at DESC");
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $slug = htmlspecialchars($row['slug']);
            // Format tanggal update/buat artikel
            $lastMod = date('Y-m-d', strtotime($row['created_at']));
            ?>
            <url>
                <loc><?= $baseUrl; ?>/blog-detail.php?slug=<?= $slug; ?></loc>
                <lastmod><?= $lastMod; ?></lastmod>
                <changefreq>weekly</changefreq>
                <priority>0.7</priority>
            </url>
            <?php
        }
    } catch (Exception $e) {
        // Abaikan jika terjadi error koneksi saat fetch sitemap
    }
    ?>

</urlset>