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

$properties = $db->fetchAll("SELECT slug, updated_at FROM properties WHERE is_active = 1 ORDER BY id");
$posts = $db->fetchAll("SELECT slug, updated_at FROM blog_posts WHERE is_published = 1 ORDER BY id");
?>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
        <loc><?= SITE_URL ?></loc>
        <priority>1.0</priority>
        <changefreq>daily</changefreq>
        <lastmod><?= date('Y-m-d') ?></lastmod>
    </url>
    <url>
        <loc><?= SITE_URL ?>properties.php</loc>
        <priority>0.9</priority>
        <changefreq>daily</changefreq>
    </url>
    <url>
        <loc><?= SITE_URL ?>services.php</loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?= SITE_URL ?>about.php</loc>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?= SITE_URL ?>contact.php</loc>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?= SITE_URL ?>blog.php</loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc><?= SITE_URL ?>faq.php</loc>
        <priority>0.5</priority>
        <changefreq>monthly</changefreq>
    </url>
    <?php foreach ($properties as $p): ?>
    <url>
        <loc><?= SITE_URL ?>property/<?= $p['slug'] ?></loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
        <lastmod><?= date('Y-m-d', strtotime($p['updated_at'])) ?></lastmod>
    </url>
    <?php endforeach; ?>
    <?php foreach ($posts as $post): ?>
    <url>
        <loc><?= SITE_URL ?>blog/<?= $post['slug'] ?></loc>
        <priority>0.6</priority>
        <changefreq>monthly</changefreq>
        <lastmod><?= date('Y-m-d', strtotime($post['updated_at'])) ?></lastmod>
    </url>
    <?php endforeach; ?>
</urlset>
