<?php
/**
 * Bhavya Realty - Dynamic XML Sitemap
 */
define('ROOT_PATH', __DIR__ . '/');
require_once ROOT_PATH . 'config/config.php';
require_once ROOT_PATH . 'includes/functions.php';

header('Content-Type: application/xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';

$db  = db();
$now = date('Y-m-d');
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

  <!-- Static Pages -->
<?php
$static = [
  [BASE_URL,                           '1.0',  'daily'],
  [BASE_URL.'about.php',               '0.8',  'monthly'],
  [BASE_URL.'properties.php',          '0.9',  'daily'],
  [BASE_URL.'builders.php',            '0.8',  'weekly'],
  [BASE_URL.'services.php',            '0.7',  'monthly'],
  [BASE_URL.'gallery/',                '0.6',  'weekly'],
  [BASE_URL.'testimonials.php',        '0.6',  'monthly'],
  [BASE_URL.'faq.php',                 '0.7',  'monthly'],
  [BASE_URL.'blog/',                   '0.8',  'daily'],
  [BASE_URL.'contact/',                '0.8',  'monthly'],
  [BASE_URL.'calculators/emi.php',     '0.6',  'yearly'],
  [BASE_URL.'calculators/home-loan.php','0.6', 'yearly'],
  [BASE_URL.'calculators/stamp-duty.php','0.6','yearly'],
  [BASE_URL.'calculators/registration.php','0.5','yearly'],
  [BASE_URL.'privacy-policy.php',      '0.3',  'yearly'],
  [BASE_URL.'terms.php',               '0.3',  'yearly'],
  [BASE_URL.'disclaimer.php',          '0.3',  'yearly'],
  [BASE_URL.'cookie-policy.php',       '0.3',  'yearly'],
];
foreach ($static as [$url, $pri, $freq]):
?>
  <url>
    <loc><?= htmlspecialchars($url) ?></loc>
    <lastmod><?= $now ?></lastmod>
    <changefreq><?= $freq ?></changefreq>
    <priority><?= $pri ?></priority>
  </url>
<?php endforeach; ?>

  <!-- Properties -->
<?php
$props = $db->query("SELECT slug, thumbnail, updated_at FROM properties WHERE status=1 ORDER BY updated_at DESC")->fetchAll();
foreach ($props as $p):
  $img = $p['thumbnail'] ? UPLOADS_URL.'properties/'.htmlspecialchars($p['thumbnail']) : '';
?>
  <url>
    <loc><?= htmlspecialchars(BASE_URL.'property/index.php?slug='.$p['slug']) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($p['updated_at'])) ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.85</priority>
    <?php if ($img): ?>
    <image:image><image:loc><?= $img ?></image:loc></image:image>
    <?php endif; ?>
  </url>
<?php endforeach; ?>

  <!-- Blog Posts -->
<?php
$posts = $db->query("SELECT slug, thumbnail, updated_at FROM blogs WHERE status=1 ORDER BY published_at DESC")->fetchAll();
foreach ($posts as $b):
?>
  <url>
    <loc><?= htmlspecialchars(BASE_URL.'blog/post.php?slug='.$b['slug']) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($b['updated_at'])) ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.65</priority>
  </url>
<?php endforeach; ?>

</urlset>
