<?php
/**
 * NOFFER.IN - Dynamic Sitemap
 * Generates XML sitemap for SEO
 */

require_once __DIR__ . '/functions.php';

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

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Homepage -->
    <url>
        <loc><?php echo SITE_URL; ?>/</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Static Pages -->
    <url>
        <loc><?php echo SITE_URL; ?>/recommend.php</loc>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?php echo SITE_URL; ?>/submit-company.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <url>
        <loc><?php echo SITE_URL; ?>/claim-company.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <!-- Hosting Types -->
    <?php foreach (getHostingTypes() as $key => $label): ?>
    <url>
        <loc><?php echo SITE_URL; ?>/index.php?type=<?php echo $key; ?></loc>
        <changefreq>daily</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Companies -->
    <?php
    $allCompanies = dbFetchAll("SELECT slug, updated_at FROM companies WHERE company_status = 1 ORDER BY updated_at DESC");
    foreach ($allCompanies as $c):
    ?>
    <url>
        <loc><?php echo SITE_URL; ?>/view.php?slug=<?php echo sanitize($c['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($c['updated_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Blog Posts (if any) -->
    <?php
    $posts = dbFetchAll("SELECT slug, updated_at FROM posts WHERE post_status = 'published' ORDER BY updated_at DESC");
    foreach ($posts as $post):
    ?>
    <url>
        <loc><?php echo SITE_URL; ?>/blog/<?php echo sanitize($post['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($post['updated_at'])); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
</urlset>