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

include 'includes/db.php';
?>

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

    <!-- Homepage -->
    <url>
        <loc>https://www.interiorsolutions.co.ke/</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>

    <!-- Main Pages -->
    <url>
        <loc>https://www.interiorsolutions.co.ke/services.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.9</priority>
    </url>

    <url>
        <loc>https://www.interiorsolutions.co.ke/portfolio.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>

    <url>
        <loc>https://www.interiorsolutions.co.ke/blog.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc>https://www.interiorsolutions.co.ke/about.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc>https://www.interiorsolutions.co.ke/contact.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <!-- Dynamic Blog Posts -->
    <?php
    $posts = $pdo->query("SELECT slug, date_published FROM blog_posts ORDER BY date_published DESC")->fetchAll();
    foreach ($posts as $post):
    ?>
    <url>
        <loc>https://www.interiorsolutions.co.ke/blog-single.php?slug=<?= urlencode($post['slug']) ?></loc>
        <lastmod><?= $post['date_published'] ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>

    <!-- Dynamic Services (if you have slugs) -->
    <?php
    $services = $pdo->query("SELECT slug FROM services ORDER BY sort_order ASC")->fetchAll();
    foreach ($services as $service):
    ?>
    <url>
        <loc>https://www.interiorsolutions.co.ke/services.php#<?= htmlspecialchars($service['slug']) ?></loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>

    <!-- Portfolio Items -->
    <?php
    $portfolio = $pdo->query("SELECT id FROM portfolio ORDER BY sort_order ASC")->fetchAll();
    foreach ($portfolio as $item):
    ?>
    <url>
        <loc>https://www.interiorsolutions.co.ke/portfolio.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>

</urlset>