<?php
/**
 * Dinamik Sitemap.xml
 * Askolsun İlişki Akademisi
 */

require_once 'config/config.php';

// XML header
header('Content-Type: application/xml; charset=utf-8');

// Site base URL'i
$base_url = rtrim(BASE_URL, '/');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
    
    <!-- Ana Sayfa -->
    <url>
        <loc><?php echo $base_url; ?>/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Statik Sayfalar -->
    <url>
        <loc><?php echo $base_url; ?>/hakkimizda</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/hizmetlerimiz</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/egitimler</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/testler</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/blog</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/iletisim</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

<?php
// Blog kategorilerini getir
try {
    $sql = "SELECT * FROM blog_categories ORDER BY name ASC";
    $categories = $db->fetchAll($sql);
    
    if ($categories) {
        foreach ($categories as $category) {
            $lastmod = !empty($category['updated_at']) ? date('Y-m-d', strtotime($category['updated_at'])) : date('Y-m-d', strtotime($category['created_at']));
            echo "    <!-- Blog Kategori: " . htmlspecialchars($category['name']) . " -->\n";
            echo "    <url>\n";
            echo "        <loc>" . $base_url . "/blog/kategori/" . htmlspecialchars($category['slug']) . "</loc>\n";
            echo "        <lastmod>" . $lastmod . "</lastmod>\n";
            echo "        <changefreq>weekly</changefreq>\n";
            echo "        <priority>0.6</priority>\n";
            echo "    </url>\n";
        }
    }
} catch (Exception $e) {
    // Hata durumunda sessizce devam et
}

// Yayınlanmış blog yazılarını getir
try {
    $sql = "SELECT slug, title, published_at, updated_at, created_at 
            FROM blogs 
            WHERE status = 'published' 
            ORDER BY published_at DESC";
    $blogs = $db->fetchAll($sql);
    
    if ($blogs) {
        echo "    <!-- Blog Yazıları -->\n";
        foreach ($blogs as $blog) {
            // Son güncelleme tarihini belirle
            $lastmod = date('Y-m-d');
            if (!empty($blog['updated_at'])) {
                $lastmod = date('Y-m-d', strtotime($blog['updated_at']));
            } elseif (!empty($blog['published_at'])) {
                $lastmod = date('Y-m-d', strtotime($blog['published_at']));
            } elseif (!empty($blog['created_at'])) {
                $lastmod = date('Y-m-d', strtotime($blog['created_at']));
            }
            
            echo "    <url>\n";
            echo "        <loc>" . $base_url . "/blog/" . htmlspecialchars($blog['slug']) . "</loc>\n";
            echo "        <lastmod>" . $lastmod . "</lastmod>\n";
            echo "        <changefreq>yearly</changefreq>\n";
            echo "        <priority>0.5</priority>\n";
            echo "    </url>\n";
        }
    }
} catch (Exception $e) {
    // Hata durumunda sessizce devam et
}
?>
    
    <!-- Yasal Sayfalar -->
    <url>
        <loc><?php echo $base_url; ?>/gizlilik-politikasi</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
    <url>
        <loc><?php echo $base_url; ?>/kullanim-kosullari</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
</urlset>