<?php

// Configuration
$jsonUrl = "https://indieblog.page/export";
$rssFile = "/app/data/public/indieblogv2_feed.xml";
$cacheFile = "/app/data/cache/indieblogv2_cache.txt";
$cutoffDate = strtotime('-10 days'); // Seulement les 10 derniers jours

@mkdir(dirname($rssFile), 0777, true);
@mkdir(dirname($cacheFile), 0777, true);

$jsonData = @file_get_contents($jsonUrl);
if (!$jsonData) die("Error: Unable to fetch JSON from $jsonUrl\n");

$data = json_decode($jsonData, true);
if (!$data) die("Error: Invalid JSON data from $jsonUrl\n");

$includedIds = file_exists($cacheFile) ? file($cacheFile, FILE_IGNORE_NEW_LINES) : [];

// Filtrer les éléments récents et non déjà inclus
$newItems = array_filter($data, function ($item) use ($includedIds, $cutoffDate) {
    return isset($item['published'], $item['itemid']) &&
        !in_array($item['itemid'], $includedIds) &&
        $item['published'] >= $cutoffDate; // Publie dans les 10 derniers jours
});

usort($newItems, fn($a, $b) => $b['published'] <=> $a['published']);

$groupedByDay = [];
foreach ($newItems as $item) {
    $day = isset($item['published']) && is_numeric($item['published'])
        ? date('Y-m-d', $item['published'])
        : date('Y-m-d');
    $groupedByDay[$day][] = $item;
}

$rss = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"></rss>');
$channel = $rss->addChild('channel');
$channel->addChild('title', 'IndieBlog Feed (Last 10 Days)');
$channel->addChild('link', 'https://indieblog.page/');
$channel->addChild('description', 'RSS feed of articles from the last 10 days');
$channel->addChild('language', 'en');

$newEntries = [];
foreach ($groupedByDay as $day => $items) {
    $rssItem = $channel->addChild('item');
    $rssItem->addChild('title', "Links for $day");
    $rssItem->addChild('link', "https://indieblog.page/$day#" . md5(json_encode($items)));
    $rssItem->addChild('guid', "https://indieblog.page/$day#" . md5(json_encode($items)));
    $rssItem->addChild('pubDate', date(DATE_RSS, strtotime($day)));

    $description = "<ul>";
    foreach ($items as $item) {
        $postTitle = htmlspecialchars($item['itemtitle'] ?? 'No title', ENT_XML1);
        $postUrl = htmlspecialchars($item['itemurl'] ?? '#', ENT_XML1);
        $blogTitle = htmlspecialchars($item['feedtitle'] ?? 'Unknown Blog', ENT_XML1);
        $blogUrl = htmlspecialchars($item['feedurl'] ?? '#', ENT_XML1);

        $description .= "<li><a href=\"$postUrl\">$postTitle</a> (<a href=\"$blogUrl\">$blogTitle</a>)</li>";
        $newEntries[] = $item['itemid'];
    }
    $description .= "</ul>";

    addCData($rssItem->addChild('description'), $description);
}

if (!empty($newEntries)) {
    file_put_contents($cacheFile, implode("\n", array_merge($includedIds, $newEntries)));
}

$rss->asXML($rssFile);

function addCData(SimpleXMLElement $node, $content)
{
    $domNode = dom_import_simplexml($node);
    $domOwner = $domNode->ownerDocument;
    $domNode->appendChild($domOwner->createCDATASection($content));
}