<?php
/**
 * Dynamic Sitemap Generator
 * File: sitemap.php
 * Run: https://toletkhulna.com.bd/sitemap.php
 */

header('Content-Type: application/xml; charset=utf-8');

require_once __DIR__ . '/core/database.php';
$db = getDB();

// Get properties
$properties = $db->getRows("SELECT id, updated_at FROM properties WHERE status = 'approved' ORDER BY id ASC");

// Get blog posts
$blogs = $db->getRows("SELECT slug, published_at FROM blog_posts WHERE status = 'published' ORDER BY published_at DESC");

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    
    <!-- Static Pages -->
    <url><loc>https://toletkhulna.com.bd/</loc><changefreq>daily</changefreq><priority>1.0</priority></url>
    <url><loc>https://toletkhulna.com.bd/properties/list.php</loc><changefreq>daily</changefreq><priority>0.9</priority></url>
    <url><loc>https://toletkhulna.com.bd/about.php</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
    <url><loc>https://toletkhulna.com.bd/contact.php</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <url><loc>https://toletkhulna.com.bd/blog.php</loc><changefreq>weekly</changefreq><priority>0.9</priority></url>
    <url><loc>https://toletkhulna.com.bd/faq.php</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <url><loc>https://toletkhulna.com.bd/affiliate.php</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <url><loc>https://toletkhulna.com.bd/login.php</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
    <url><loc>https://toletkhulna.com.bd/register.php</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
    
    <!-- Dynamic Properties -->
    <?php foreach ($properties as $p): ?>
    <url>
        <loc>https://toletkhulna.com.bd/properties/details.php?id=<?php echo $p['id']; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($p['updated_at'] ?? 'now')); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Dynamic Blog Posts -->
    <?php foreach ($blogs as $b): ?>
    <url>
        <loc>https://toletkhulna.com.bd/blog-details.php?slug=<?php echo $b['slug']; ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($b['published_at'] ?? 'now')); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>
    
</urlset>