<?php
ob_start();

include __DIR__ . '/includes/config.php';

ob_end_clean();

header('Content-Type: application/xml; charset=UTF-8');

if (isset($conn)) {
    mysqli_set_charset($conn, 'utf8mb4');
}

/*
|--------------------------------------------------------------------------
| XML Escape
|--------------------------------------------------------------------------
*/
function xmlEscape($value)
{
    return htmlspecialchars(
        (string)$value,
        ENT_XML1 | ENT_QUOTES,
        'UTF-8'
    );
}

/*
|--------------------------------------------------------------------------
| Date Format
|--------------------------------------------------------------------------
*/
function sitemapDate($date)
{
    if (empty($date)) {
        return null;
    }

    $timestamp = strtotime($date);

    if ($timestamp === false) {
        return null;
    }

    return date('c', $timestamp);
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo "\n";
?>

<?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">
<?php

/* =========================================================
   HOMEPAGE
========================================================= */

$homeQ = mysqli_query(
    $conn,
    "SELECT MAX(updated_at) AS last_update
     FROM posts
     WHERE status = 'published'"
);

if ($homeQ) {

    $home = mysqli_fetch_assoc($homeQ);

    $homeLastMod = sitemapDate($home['last_update'] ?? null);

?>

<url>
    <loc>https://primeassure.news/</loc>
<?php if ($homeLastMod): ?>
    <lastmod><?= xmlEscape($homeLastMod) ?></lastmod>
<?php endif; ?>
</url>

<?php
}

/* =========================================================
   CATEGORIES
========================================================= */

$catQ = mysqli_query(
    $conn,
    "SELECT
        c.id,
        c.slug,
        MAX(p.updated_at) AS last_update
     FROM categories c
     INNER JOIN posts p
        ON p.category_id = c.id
        AND p.status = 'published'
     GROUP BY c.id, c.slug
     ORDER BY c.id ASC"
);

if ($catQ) {

    while ($cat = mysqli_fetch_assoc($catQ)) {

        if (empty($cat['slug'])) {
            continue;
        }

        $lastmod = sitemapDate($cat['last_update'] ?? null);

        $categoryUrl =
            'https://primeassure.news/categories/' .
            rawurlencode($cat['slug']);
?>

<url>
    <loc><?= xmlEscape($categoryUrl) ?></loc>
<?php if ($lastmod): ?>
    <lastmod><?= xmlEscape($lastmod) ?></lastmod>
<?php endif; ?>
</url>

<?php
    }
}

/* =========================================================
   POSTS
========================================================= */

$postQ = mysqli_query(
    $conn,
    "SELECT slug, updated_at
     FROM posts
     WHERE status = 'published'
       AND slug IS NOT NULL
       AND slug != ''
     ORDER BY updated_at DESC"
);

if ($postQ) {

    while ($post = mysqli_fetch_assoc($postQ)) {

        $postLastMod = sitemapDate($post['updated_at'] ?? null);

        $postUrl =
            'https://primeassure.news/post/' .
            rawurlencode($post['slug']);
?>

<url>
    <loc><?= xmlEscape($postUrl) ?></loc>
<?php if ($postLastMod): ?>
    <lastmod><?= xmlEscape($postLastMod) ?></lastmod>
<?php endif; ?>
</url>

<?php
    }
}

?>

</urlset>