<?php
/**
 * XML Sitemap Generator
 * Note: Content-Type header is set by the router (public/index.php) before including this file.
 */
require_once dirname(__DIR__) . '/includes/config.php';
require_once dirname(__DIR__) . '/includes/db.php';

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Static Pages -->
    <url>
        <loc><?= SITE_URL ?>/</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/services</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/portfolio</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/blog</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/appointments</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/contact</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <url>
        <loc><?= SITE_URL ?>/testimonials</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>

    <!-- Portfolio Items -->
    <?php
    try {
        $portfolio = dbFetchAll("SELECT id, slug, updated_at FROM portfolio WHERE is_active = 1 ORDER BY created_at DESC");
        foreach ($portfolio as $item):
    ?>
    <url>
        <loc><?= SITE_URL ?>/portfolio?slug=<?= urlencode($item['slug']) ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($item['updated_at'])) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php
        endforeach;
    } catch (Exception $e) {}
    ?>

    <!-- Blog Posts -->
    <?php
    try {
        $posts = dbFetchAll("SELECT id, slug, published_at FROM blog_posts WHERE status = 'published' ORDER BY published_at DESC LIMIT 100");
        foreach ($posts as $post):
    ?>
    <url>
        <loc><?= SITE_URL ?>/blog?slug=<?= urlencode($post['slug']) ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($post['published_at'])) ?></lastmod>
        <changefreq>never</changefreq>
        <priority>0.7</priority>
    </url>
    <?php
        endforeach;
    } catch (Exception $e) {}
    ?>

    <!-- Blog Category Pages -->
    <?php
    try {
        $categories = dbFetchAll("SELECT slug FROM blog_categories");
        foreach ($categories as $cat):
    ?>
    <url>
        <loc><?= SITE_URL ?>/blog?category=<?= urlencode($cat['slug']) ?></loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php
        endforeach;
    } catch (Exception $e) {}
    ?>
</urlset>
