<?php
ini_set('display_errors', 0);
error_reporting(0);

header("Content-Type: application/xml; charset=UTF-8");

require_once __DIR__ . '/includes/db.php';

$baseUrl = "https://www.royaloptics.in";

$staticUrls = [
  "/" => ["freq" => "daily", "priority" => "1.0"],
  "/about.php" => ["freq" => "monthly", "priority" => "0.7"],
  "/contact.php" => ["freq" => "monthly", "priority" => "0.6"],
  "/blogs.php" => ["freq" => "daily", "priority" => "0.8"],
];

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// Static URLs
foreach ($staticUrls as $path => $meta) {
  $loc = $baseUrl . $path;

  echo "  <url>\n";
  echo "    <loc>" . htmlspecialchars($loc, ENT_XML1, 'UTF-8') . "</loc>\n";
  echo "    <lastmod>" . date('Y-m-d') . "</lastmod>\n";
  echo "    <changefreq>{$meta['freq']}</changefreq>\n";
  echo "    <priority>{$meta['priority']}</priority>\n";
  echo "  </url>\n";
}

// Blog URLs
$sql = "SELECT slug, id FROM blogs ORDER BY id DESC";
$res = $conn->query($sql);

if ($res) {
  while ($row = $res->fetch_assoc()) {
    $slug = $row['slug'];
    $blogUrl = "{$baseUrl}/blog.php?slug={$slug}";

    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($blogUrl, ENT_XML1, 'UTF-8') . "</loc>\n";
    echo "    <lastmod>" . date('Y-m-d') . "</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.7</priority>\n";
    echo "  </url>\n";
  }
}

echo "</urlset>";
