<?php
/**
 * Dynamic XML Sitemap Generator
 *
 * Generates a sitemap.xml that includes:
 * - Static pages (home, projects, tech blog, travel blog)
 * - Dynamic blog posts from the database
 *
 * Configure your web server (Hostinger .htaccess) to serve this as /sitemap.xml
 */

header('Content-Type: application/xml; charset=utf-8');

$site_url = 'https://vimleshpandey.com';

// Start XML output
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- ========== Static Pages ========== -->
    <url>
        <loc><?php echo $site_url; ?>/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?php echo $site_url; ?>/#projects</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?php echo $site_url; ?>/#tech-blog</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?php echo $site_url; ?>/#travel-blog</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?php echo $site_url; ?>/assets/resume/vimlesh-pandey-resume.html</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>

<?php
// ========== Dynamic Blog Posts from Database ==========
try {
    require_once __DIR__ . '/config/database.php';
    $conn = getDBConnection();

    // Fetch tech blog posts
    $result = $conn->query("SELECT id, title, updated_at, created_at FROM tech_blogs WHERE published = 1 ORDER BY created_at DESC");
    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $date = date('Y-m-d', strtotime($row['updated_at'] ?? $row['created_at']));
            $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $row['title']), '-'));
            echo "    <url>\n";
            echo "        <loc>{$site_url}/#tech-blog/post/{$row['id']}/{$slug}</loc>\n";
            echo "        <lastmod>{$date}</lastmod>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "        <priority>0.7</priority>\n";
            echo "    </url>\n";
        }
    }

    // Fetch travel blog posts
    $result = $conn->query("SELECT id, title, updated_at, created_at FROM travel_blogs WHERE published = 1 ORDER BY created_at DESC");
    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $date = date('Y-m-d', strtotime($row['updated_at'] ?? $row['created_at']));
            $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $row['title']), '-'));
            echo "    <url>\n";
            echo "        <loc>{$site_url}/#travel-blog/post/{$row['id']}/{$slug}</loc>\n";
            echo "        <lastmod>{$date}</lastmod>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "        <priority>0.7</priority>\n";
            echo "    </url>\n";
        }
    }

    $conn->close();
} catch (Exception $e) {
    // If database connection fails, sitemap still works with static pages
    error_log('Sitemap: Database connection failed - ' . $e->getMessage());
}
?>
</urlset>

