<?php
/**
 * tesisyoneticisi.com - Dynamic XML Sitemap Generator
 */

require_once __DIR__ . '/../config/helpers.php';

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

$siteUrl = getSetting('site_url', 'https://tesisyoneticisi.com');
$pdo = getDbConnection();

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    
    <!-- Static Pages -->
    <url>
        <loc><?= $siteUrl ?>/</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?= $siteUrl ?>/hizmetler</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= $siteUrl ?>/teklif-al</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= $siteUrl ?>/blog</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?= $siteUrl ?>/iletisim</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <!-- Services Dynamic URLs -->
    <?php
    $stmtSrv = $pdo->query("SELECT slug, created_at FROM " . DB_PREFIX . "services WHERE status = 'active'");
    while ($row = $stmtSrv->fetch()):
    ?>
    <url>
        <loc><?= $siteUrl ?>/hizmet/<?= $row['slug'] ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($row['created_at'])) ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.85</priority>
    </url>
    <?php endwhile; ?>

    <!-- Locations Directory and pSEO URLs -->
    <url>
        <loc><?= $siteUrl ?>/lokasyonlar</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    <?php
    $stmtLoc = $pdo->query("SELECT slug, created_at FROM " . DB_PREFIX . "locations WHERE status = 'active'");
    while ($row = $stmtLoc->fetch()):
    ?>
    <url>
        <loc><?= $siteUrl ?>/lokasyon/<?= $row['slug'] ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($row['created_at'])) ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.85</priority>
    </url>
    <?php endwhile; ?>

    <!-- Blogs Dynamic URLs -->
    <?php
    $stmtBlog = $pdo->query("SELECT slug, created_at FROM " . DB_PREFIX . "blogs WHERE status = 'active'");
    while ($row = $stmtBlog->fetch()):
    ?>
    <url>
        <loc><?= $siteUrl ?>/blog/<?= $row['slug'] ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($row['created_at'])) ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.75</priority>
    </url>
    <?php endwhile; ?>

</urlset>
