<?php

namespace App\Controller;

use App\Api\TTRssClient;
use App\Core\Controller\Site\PageController;
use App\Markdown\Parser\Post as PostParser;
use Symfony\Component\HttpFoundation\Response;

class LinkController extends PageController
{
    public function links(TTRssClient $client, int $page = 1): Response
    {
        return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
            'pager' => $client->getPager($page),
        ]);
    }

    public function rss(PostParser $parser, TTRssClient $client): Response
    {
        $entities = $client->getPager(1);
        $items = [];

        foreach ($entities['items'] as $entity) {
            $items[] = [
                'title' => $entity['title'],
                'guid' => md5($entity['link']),
                'date' => $entity['timestamp'],
                'link' => $entity['link'],
                'description' => null,
                'categories' => [],
            ];
        }

        $response = new Response();
        $response->headers->set('Content-Type', 'text/xml');

        return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
            'items' => $items,
        ], $response);
    }
}
