<?php
// /sitemap.xml — dinamik, önceliklendirilmiş, image support
header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex'); // Sitemap kendisi index'lenmesin
require_once __DIR__ . '/includes/config.php';

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemaps/image/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml">

    <url>
        <loc><?= SITE_URL ?>/</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <url>
        <loc><?= SITE_URL ?>/hizmetlerimiz</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>

    <url>
        <loc><?= SITE_URL ?>/hakkimizda</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc><?= SITE_URL ?>/is-ortaklarimiz</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc><?= SITE_URL ?>/blog</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?= SITE_URL ?>/iletisim</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
	<loc><?= SITE_URL ?>/kvkk</loc>
	<changefreq>yearly</changefreq>
	<priority>0.4</priority>
    </url>
    <url>
	<loc><?= SITE_URL ?>/gizlilik-politikasi</loc>
	<changefreq>yearly</changefreq>
	<priority>0.4</priority>
    </url>
    <url>
	<loc><?= SITE_URL ?>/cerez-politikasi</loc>
	<changefreq>yearly</changefreq>
	<priority>0.4</priority>
    </url>

<?php
try {
    $posts = db()->query("SELECT slug, title, excerpt, cover_image, updated_at, created_at FROM posts WHERE status='published' ORDER BY updated_at DESC NULLS LAST, created_at DESC")->fetchAll();
    foreach ($posts as $p):
        $image = $p['cover_image'] ? (str_starts_with($p['cover_image'], 'http') ? $p['cover_image'] : SITE_URL . $p['cover_image']) : '';
        $lastmod = date('Y-m-d', strtotime($p['updated_at'] ?? $p['created_at']));
?>
    <url>
        <loc><?= SITE_URL ?>/blog/<?= e($p['slug']) ?></loc>
        <lastmod><?= $lastmod ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
<?php if ($image): ?>
        <image:image>
            <image:loc><?= e($image) ?></image:loc>
            <image:title><?= e($p['title']) ?></image:title>
            <?php if ($p['excerpt']): ?><image:caption><?= e($p['excerpt']) ?></image:caption><?php endif; ?>
        </image:image>
<?php endif; ?>
    </url>
<?php endforeach; } catch(Exception $e) { error_log('Sitemap error: ' . $e->getMessage()); } ?>
</urlset>
