<?php
header("Content-Type: application/xml; charset=utf-8");

require __DIR__ . '/data/posts.php';

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

<?php foreach ($posts as $post):

    if (empty($post['slug']) || empty($post['date'])) continue;

    // ✅ SAFE SLUG (PREVENT XML BREAK)
    $slug = rawurlencode($post['slug']);
    $title = htmlspecialchars(strip_tags($post['title']), ENT_XML1, 'UTF-8');

    // IMAGE FALLBACK
    $image = !empty($post['image'])
        ? 'https://www.smartstudyweb.com' . htmlspecialchars($post['image'], ENT_XML1)
        : 'https://www.smartstudyweb.com/assets/default-story.jpg';
?>

  <url>
    <loc>https://www.smartstudyweb.com/webstory.php?slug=<?= $slug ?></loc>
    <lastmod><?= date('c', strtotime($post['date'])) ?></lastmod>

    <news:news>
      <news:publication>
        <news:name>SmartStudyWeb</news:name>
        <news:language>en</news:language>
      </news:publication>
      <news:publication_date><?= date('c', strtotime($post['date'])) ?></news:publication_date>
      <news:title><?= $title ?></news:title>
    </news:news>
    
    <image:image>
  <image:loc>https://www.smartstudyweb.com/assets/default-story.jpg</image:loc>
</image:image>


    <image:image>
      <image:loc><?= $image ?></image:loc>
    </image:image>
  </url>

<?php endforeach; ?>

</urlset>
