<?php
/**
 * Plattr Blog — dynamic sitemap.
 *
 * Lists every published post + the blog index, with last-modified
 * timestamps and image entries (Google Images cares).
 *
 * Referenced from /robots.txt and /sitemap.xml so search engines pick it up.
 */
declare(strict_types=1);

$ccRoot = realpath(__DIR__ . '/../..');
require_once $ccRoot . '/includes/blog.php';

header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=3600');

$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host   = $_SERVER['HTTP_HOST'] ?? 'plattr.nz';
$base   = $scheme . '://' . $host;

$posts = blog_all_posts(true);

// Newest post drives the index's <lastmod>
$indexLastMod = !empty($posts)
    ? gmdate('Y-m-d\TH:i:s\Z', strtotime($posts[0]['updated_at'] ?? $posts[0]['published_at'] ?? 'now'))
    : gmdate('Y-m-d\TH:i:s\Z');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n";
echo '        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"' . "\n";
echo '        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">' . "\n";

// Blog index
echo "  <url>\n";
echo "    <loc>" . htmlspecialchars("{$base}/blog/", ENT_XML1) . "</loc>\n";
echo "    <lastmod>{$indexLastMod}</lastmod>\n";
echo "    <changefreq>daily</changefreq>\n";
echo "    <priority>0.9</priority>\n";
echo "  </url>\n";

// Each post
foreach ($posts as $p) {
    $loc      = htmlspecialchars(blog_url($p['slug']), ENT_XML1);
    $lastMod  = gmdate('Y-m-d\TH:i:s\Z', strtotime($p['updated_at'] ?? $p['published_at'] ?? 'now'));
    $pubDate  = gmdate('Y-m-d\TH:i:s\Z', strtotime($p['published_at'] ?? 'now'));
    $img      = $p['featured_image'] ?? '';
    $imgAbs   = $img ? (str_starts_with($img, 'http') ? $img : $base . $img) : '';
    $imgAlt   = htmlspecialchars($p['featured_image_alt'] ?: $p['title'], ENT_XML1);
    $title    = htmlspecialchars($p['title'], ENT_XML1);

    echo "  <url>\n";
    echo "    <loc>{$loc}</loc>\n";
    echo "    <lastmod>{$lastMod}</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.8</priority>\n";
    if ($imgAbs !== '') {
        echo "    <image:image>\n";
        echo "      <image:loc>" . htmlspecialchars($imgAbs, ENT_XML1) . "</image:loc>\n";
        echo "      <image:title>{$title}</image:title>\n";
        echo "      <image:caption>{$imgAlt}</image:caption>\n";
        echo "    </image:image>\n";
    }
    // News tag — only for posts < 2 days old (Google News rules)
    if ((time() - strtotime($p['published_at'] ?? 'now')) < 2 * 86400) {
        echo "    <news:news>\n";
        echo "      <news:publication>\n";
        echo "        <news:name>Plattr</news:name>\n";
        echo "        <news:language>en</news:language>\n";
        echo "      </news:publication>\n";
        echo "      <news:publication_date>{$pubDate}</news:publication_date>\n";
        echo "      <news:title>{$title}</news:title>\n";
        echo "    </news:news>\n";
    }
    echo "  </url>\n";
}

echo "</urlset>\n";
