<?php
define('GHOST_NET_ACCESS', true);
require_once 'config.php';
require_once 'functions.php';

// Set XML content type
header('Content-Type: application/xml; charset=utf-8');

// Get all published posts
global $pdo;
$stmt = $pdo->query("
    SELECT id, title, category, updated_at, created_at 
    FROM posts 
    WHERE status = 'published' 
    ORDER BY updated_at DESC
");
$posts = $stmt->fetchAll();

// Get categories with posts
$stmt = $pdo->query("
    SELECT DISTINCT category 
    FROM posts 
    WHERE status = 'published' 
    ORDER BY category
");
$categories = $stmt->fetchAll(PDO::FETCH_COLUMN);

// Current date for lastmod
$current_date = date('Y-m-d\TH:i:s+00:00');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    
    <!-- Homepage -->
    <url>
        <loc>https://ghostnet.blog/</loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Category Pages -->
    <?php foreach ($categories as $category): ?>
    <url>
        <loc>https://ghostnet.blog/<?php echo htmlspecialchars($category); ?></loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Static Pages -->
    <url>
        <loc>https://ghostnet.blog/about</loc>
        <lastmod><?php echo $current_date; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <!-- Individual Blog Posts -->
    <?php foreach ($posts as $post): ?>
    <url>
        <loc>https://ghostnet.blog/post/<?php echo $post['id']; ?></loc>
        <lastmod><?php echo date('Y-m-d\TH:i:s+00:00', strtotime($post['updated_at'])); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    
</urlset>