<?php
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/functions.php';
require_once dirname(__DIR__) . '/includes/blog_functions.php';

// Set content type to XML
header('Content-Type: application/rss+xml; charset=UTF-8');

// Get recent blog posts
$posts = getBlogPosts(1, 20); // Get latest 20 posts

// Create RSS feed
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title><?php echo SITE_NAME; ?> Blog</title>
        <link><?php echo SITE_URL; ?>/blog/</link>
        <description>Stay updated with the latest news, tutorials, and insights from <?php echo SITE_NAME; ?></description>
        <language>en-US</language>
        <lastBuildDate><?php echo date(DATE_RSS); ?></lastBuildDate>
        <atom:link href="<?php echo SITE_URL; ?>/blog/rss.xml" rel="self" type="application/rss+xml" />
        
        <?php foreach ($posts as $post): ?>
            <item>
                <title><?php echo htmlspecialchars($post['title']); ?></title>
                <link><?php echo SITE_URL; ?>/blog/post/<?php echo $post['slug']; ?></link>
                <guid><?php echo SITE_URL; ?>/blog/post/<?php echo $post['slug']; ?></guid>
                <pubDate><?php echo date(DATE_RSS, strtotime($post['published_at'])); ?></pubDate>
                <description><![CDATA[
                    <?php if ($post['featured_image']): ?>
                        <img src="<?php echo htmlspecialchars($post['featured_image']); ?>" alt="<?php echo htmlspecialchars($post['title']); ?>" style="max-width: 100%; height: auto; margin-bottom: 1rem;">
                    <?php endif; ?>
                    <?php echo $post['excerpt'] ?: substr(strip_tags($post['content']), 0, 300) . '...'; ?>
                    <p><strong>Author:</strong> <?php echo htmlspecialchars($post['author_name']); ?></p>
                    <?php if (!empty($post['category_name'])): ?>
                        <p><strong>Category:</strong> <?php echo htmlspecialchars($post['category_name']); ?></p>
                    <?php endif; ?>
                    <?php if ($post['reading_time']): ?>
                        <p><strong>Reading time:</strong> <?php echo $post['reading_time']; ?> minutes</p>
                    <?php endif; ?>
                ]]></description>
                <?php if (!empty($post['category_name'])): ?>
                    <category><?php echo htmlspecialchars($post['category_name']); ?></category>
                <?php endif; ?>
                <?php if (!empty($post['tags'])): ?>
                    <?php foreach ($post['tags'] as $tag): ?>
                        <category><?php echo htmlspecialchars($tag['name']); ?></category>
                    <?php endforeach; ?>
                <?php endif; ?>
            </item>
        <?php endforeach; ?>
    </channel>
</rss> 