<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'includes/config.php';
require_once 'includes/db.php';

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Homepage -->
    <url>
        <loc><?php echo SITE_URL; ?>/</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Static Pages -->
    <url>
        <loc><?php echo SITE_URL; ?>/about.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/news.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/prophecy.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/devotional.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/gallery.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/livestream.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/contact.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/register.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/conference-register.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/prayer-school.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/board-of-trustees.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <url>
        <loc><?php echo SITE_URL; ?>/history.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <?php
    // Dynamic News Articles
    $news_query = "SELECT id, title, created_at, updated_at FROM news ORDER BY created_at DESC";
    $news_result = $conn->query($news_query);
    
    if ($news_result && $news_result->num_rows > 0) {
        while ($news = $news_result->fetch_assoc()) {
            $lastmod = !empty($news['updated_at']) ? $news['updated_at'] : $news['created_at'];
            echo "    <url>\n";
            echo "        <loc>" . SITE_URL . "/news.php?id=" . $news['id'] . "</loc>\n";
            echo "        <lastmod>" . date('c', strtotime($lastmod)) . "</lastmod>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "        <priority>0.7</priority>\n";
            echo "    </url>\n";
        }
    }
    
    // Dynamic Prophecy Articles
    $prophecy_query = "SELECT id, title, created_at, updated_at FROM prophecies WHERE type = 'prophecy' ORDER BY created_at DESC";
    $prophecy_result = $conn->query($prophecy_query);
    
    if ($prophecy_result && $prophecy_result->num_rows > 0) {
        while ($prophecy = $prophecy_result->fetch_assoc()) {
            $lastmod = !empty($prophecy['updated_at']) ? $prophecy['updated_at'] : $prophecy['created_at'];
            echo "    <url>\n";
            echo "        <loc>" . SITE_URL . "/prophecy.php?id=" . $prophecy['id'] . "</loc>\n";
            echo "        <lastmod>" . date('c', strtotime($lastmod)) . "</lastmod>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "        <priority>0.7</priority>\n";
            echo "    </url>\n";
        }
    }
    
    // Dynamic Devotional Articles
    $devotional_query = "SELECT id, title, created_at, updated_at FROM prophecies WHERE type = 'devotional' ORDER BY created_at DESC";
    $devotional_result = $conn->query($devotional_query);
    
    if ($devotional_result && $devotional_result->num_rows > 0) {
        while ($devotional = $devotional_result->fetch_assoc()) {
            $lastmod = !empty($devotional['updated_at']) ? $devotional['updated_at'] : $devotional['created_at'];
            echo "    <url>\n";
            echo "        <loc>" . SITE_URL . "/devotional.php?id=" . $devotional['id'] . "</loc>\n";
            echo "        <lastmod>" . date('c', strtotime($lastmod)) . "</lastmod>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "        <priority>0.7</priority>\n";
            echo "    </url>\n";
        }
    }
    ?>
</urlset>