<?php
require_once 'includes/functions.php';
$db = getDB();

header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Homepage -->
    <url>
        <loc><?php echo 'https://dovecreations.org/'; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Blog listing page -->
    <url>
        <loc><?php echo 'https://dovecreations.org/blog.php'; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <!-- Contact page -->
    <url>
        <loc><?php echo 'https://dovecreations.org/#contact'; ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <?php if ($db): ?>
        <!-- Blog posts -->
        <?php
        $stmt = $db->prepare("SELECT slug, updated_at FROM blog_posts WHERE active = 1 ORDER BY updated_at DESC");
        $stmt->execute();
        $blogPosts = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($blogPosts as $post):
            $lastmod = $post['updated_at'] ? date('Y-m-d', strtotime($post['updated_at'])) : date('Y-m-d');
        ?>
        <url>
            <loc><?php echo 'https://dovecreations.org/blog-post.php?slug=' . htmlspecialchars($post['slug']); ?></loc>
            <lastmod><?php echo $lastmod; ?></lastmod>
            <changefreq>monthly</changefreq>
            <priority>0.6</priority>
        </url>
        <?php endforeach; ?>
        
        <!-- Projects -->
        <?php
        $stmt = $db->prepare("SELECT id, updated_at FROM projects WHERE active = 1 ORDER BY updated_at DESC");
        $stmt->execute();
        $projects = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($projects as $project):
            $lastmod = $project['updated_at'] ? date('Y-m-d', strtotime($project['updated_at'])) : date('Y-m-d');
        ?>
        <url>
            <loc><?php echo 'https://dovecreations.org/project-details.php?id=' . $project['id']; ?></loc>
            <lastmod><?php echo $lastmod; ?></lastmod>
            <changefreq>monthly</changefreq>
            <priority>0.6</priority>
        </url>
        <?php endforeach; ?>
    <?php endif; ?>
</urlset>
