<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'config/database.php';

$baseUrl = 'https://hraindia.org';
$currentDate = date('Y-m-d');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

    <!-- Homepage -->
    <url>
        <loc><?php echo $baseUrl; ?>/</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>

    <!-- Static Pages -->
    <url>
        <loc><?php echo $baseUrl; ?>/about</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/leadership</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/history</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.7</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/our-work</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/get-involved</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>

    <!-- Gallery Main Page -->
    <url>
        <loc><?php echo $baseUrl; ?>/gallery</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>

    <!-- Upcoming Events -->
    <url>
        <loc><?php echo $baseUrl; ?>/events</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>

    <!-- Insights Pages -->
    <url>
        <loc><?php echo $baseUrl; ?>/news</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/insights/perspectives.php</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/insights/news.php</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/insights/resources.php</loc>
        <lastmod><?php echo $currentDate; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>

    <?php
    try {
        // Dynamic Gallery Albums
        $galleryStmt = $pdo->query("
            SELECT id, slug, updated_at, event_date
            FROM gallery_albums 
            WHERE status = 'published'
            ORDER BY updated_at DESC
        ");
        
        while ($album = $galleryStmt->fetch(PDO::FETCH_ASSOC)) {
            $lastmod = $album['updated_at'] ? date('Y-m-d', strtotime($album['updated_at'])) : $currentDate;
            echo "    <url>\n";
            echo "        <loc>{$baseUrl}/gallery/{$album['id']}</loc>\n";
            echo "        <lastmod>{$lastmod}</lastmod>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "        <priority>0.7</priority>\n";
            echo "    </url>\n\n";
        }

        // Dynamic News Articles
        $newsStmt = $pdo->query("
            SELECT id, slug, updated_at, published_date
            FROM news_articles 
            WHERE status = 'published'
            ORDER BY published_date DESC
            LIMIT 100
        ");
        
        while ($article = $newsStmt->fetch(PDO::FETCH_ASSOC)) {
            $lastmod = $article['updated_at'] ? date('Y-m-d', strtotime($article['updated_at'])) : $currentDate;
            echo "    <url>\n";
            echo "        <loc>{$baseUrl}/news/{$article['id']}</loc>\n";
            echo "        <lastmod>{$lastmod}</lastmod>\n";
            echo "        <changefreq>yearly</changefreq>\n";
            echo "        <priority>0.6</priority>\n";
            echo "    </url>\n\n";
        }

    } catch (Exception $e) {
        // If database connection fails, skip dynamic content
        error_log("Sitemap generation error: " . $e->getMessage());
    }
    ?>

</urlset>