<?php
require_once __DIR__ . '/../includes/db_helpers.php';
require_once __DIR__ . '/../includes/site_config.php';

header('Content-Type: application/xml; charset=utf-8');

$base = rtrim(SITE_URL, '/');
$today = date('Y-m-d');

$urls = [];

// Statik sayfalar
$urls[] = ['loc' => $base . '/',                'lastmod' => $today, 'priority' => '1.0', 'changefreq' => 'weekly'];
$urls[] = ['loc' => $base . '/hakkimizda.php',  'lastmod' => $today, 'priority' => '0.8', 'changefreq' => 'monthly'];
$urls[] = ['loc' => $base . '/projeler.php',    'lastmod' => $today, 'priority' => '0.9', 'changefreq' => 'weekly'];
$urls[] = ['loc' => $base . '/iletisim.php',    'lastmod' => $today, 'priority' => '0.7', 'changefreq' => 'monthly'];
$urls[] = ['loc' => $base . '/kvkk.php',        'lastmod' => $today, 'priority' => '0.3', 'changefreq' => 'yearly'];

// Sektor sayfalari
foreach ($SECTORS as $slug => $sector) {
    $urls[] = [
        'loc' => $base . '/hizmetler/' . $slug . '.php',
        'lastmod' => $today,
        'priority' => '0.9',
        'changefreq' => 'monthly',
    ];
}

// Projeler — slug bazli
$projects = getAllProjects($pdo);
foreach ($projects as $p) {
    $slug = slugify($p['proje_adi']);
    if ($slug === '') continue;
    $lastmod = !empty($p['guncelleme_tarihi'])
        ? date('Y-m-d', strtotime($p['guncelleme_tarihi']))
        : (!empty($p['olusturma_tarihi']) ? date('Y-m-d', strtotime($p['olusturma_tarihi'])) : $today);
    $urls[] = [
        'loc' => $base . '/proje.php?slug=' . $slug,
        'lastmod' => $lastmod,
        'priority' => '0.7',
        'changefreq' => 'monthly',
    ];
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ($urls as $u) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($u['loc'], ENT_XML1) . "</loc>\n";
    echo "    <lastmod>" . $u['lastmod'] . "</lastmod>\n";
    echo "    <changefreq>" . $u['changefreq'] . "</changefreq>\n";
    echo "    <priority>" . $u['priority'] . "</priority>\n";
    echo "  </url>\n";
}
echo '</urlset>' . "\n";
