<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
require_once __DIR__ . '/inc/config.php';

$baseUrl = 'https://' . ($_SERVER['HTTP_HOST'] ?? 'sigortabaz.com');

// Ana sayfalar
$staticPages = [
    ['loc' => '/', 'priority' => '1.00', 'changefreq' => 'daily'],
    ['loc' => '/hakkimizda', 'priority' => '0.80', 'changefreq' => 'monthly'],
    ['loc' => '/iletisim', 'priority' => '0.80', 'changefreq' => 'monthly'],
    ['loc' => '/subelerimiz', 'priority' => '0.80', 'changefreq' => 'weekly'],
    ['loc' => '/sirketler', 'priority' => '0.70', 'changefreq' => 'weekly'],
    ['loc' => '/blog', 'priority' => '0.80', 'changefreq' => 'daily'],
    ['loc' => '/urunler', 'priority' => '0.90', 'changefreq' => 'weekly'],
    ['loc' => '/sube-basvuru', 'priority' => '0.60', 'changefreq' => 'monthly'],
    ['loc' => '/teklif', 'priority' => '0.90', 'changefreq' => 'weekly'],
];

foreach ($staticPages as $page):
?>
    <url>
        <loc><?php echo $baseUrl . $page['loc']; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq><?php echo $page['changefreq']; ?></changefreq>
        <priority><?php echo $page['priority']; ?></priority>
    </url>
<?php endforeach; ?>

<?php
// Veritabanindan sayfalar
$dbPages = dbFetchAll("SELECT slug, updated_at FROM pages WHERE is_published = 'true' ORDER BY updated_at DESC");
foreach ($dbPages as $p):
?>
    <url>
        <loc><?php echo $baseUrl . '/' . urlencode($p['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($p['updated_at'] ?? 'now')); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.70</priority>
    </url>
<?php endforeach; ?>

<?php
// Veritabanindan urunler
$dbProducts = dbFetchAll("SELECT slug, updated_at FROM products WHERE is_active = 'true' ORDER BY updated_at DESC");
foreach ($dbProducts as $p):
?>
    <url>
        <loc><?php echo $baseUrl . '/urun/' . urlencode($p['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($p['updated_at'] ?? 'now')); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.80</priority>
    </url>
<?php endforeach; ?>

<?php
// Veritabanindan blog yazilari
$dbPosts = dbFetchAll("SELECT slug, updated_at FROM posts WHERE is_active = 'true' ORDER BY updated_at DESC");
foreach ($dbPosts as $p):
?>
    <url>
        <loc><?php echo $baseUrl . '/blog/' . urlencode($p['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($p['updated_at'] ?? 'now')); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.60</priority>
    </url>
<?php endforeach; ?>

</urlset>
