<?php
require_once 'config/database.php';
require_once 'includes/functions.php';

// XML header
header('Content-Type: application/xml; charset=utf-8');

$settings = getSettings();
$baseUrl = SITE_URL;

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    <!-- Ana Sayfa -->
    <url>
        <loc><?php echo $baseUrl; ?>/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <!-- Statik Sayfalar -->
    <url>
        <loc><?php echo $baseUrl; ?>/hakkimizda.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/iletisim.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/blog.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>

    <?php
    // Blog yazıları
    try {
        $stmt = $pdo->query("SELECT slug, updated_at FROM blogs WHERE is_active = 1 ORDER BY updated_at DESC");
        $blogs = $stmt->fetchAll();
        
        foreach ($blogs as $blog) {
            echo '<url>' . "\n";
            echo '    <loc>' . $baseUrl . '/blog/' . htmlspecialchars($blog['slug']) . '.php</loc>' . "\n";
            echo '    <lastmod>' . date('Y-m-d', strtotime($blog['updated_at'])) . '</lastmod>' . "\n";
            echo '    <changefreq>monthly</changefreq>' . "\n";
            echo '    <priority>0.7</priority>' . "\n";
            echo '</url>' . "\n";
        }
    } catch (PDOException $e) {
        // Hata durumunda sessizce devam et
    }

    // Hizmet bölgeleri
    try {
        $stmt = $pdo->query("SELECT slug, updated_at FROM service_areas WHERE is_active = 1 ORDER BY title ASC");
        $serviceAreas = $stmt->fetchAll();
        
        foreach ($serviceAreas as $area) {
            echo '<url>' . "\n";
            echo '    <loc>' . $baseUrl . '/' . htmlspecialchars($area['slug']) . '.php</loc>' . "\n";
            echo '    <lastmod>' . date('Y-m-d', strtotime($area['updated_at'])) . '</lastmod>' . "\n";
            echo '    <changefreq>weekly</changefreq>' . "\n";
            echo '    <priority>0.9</priority>' . "\n";
            echo '</url>' . "\n";
        }
    } catch (PDOException $e) {
        // Hata durumunda sessizce devam et
    }
    ?>

</urlset>



