<?php

declare(strict_types=1);

namespace App\Application\Actions;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Application\Services\BlogProvider;

class BlogApiListAction
{
    private BlogProvider $blogProvider;

    public function __construct(BlogProvider $blogProvider)
    {
        $this->blogProvider = $blogProvider;
    }

    public function __invoke(Request $request, Response $response, array $args): Response
    {
        $shortArticles = $this->blogProvider->getRandomArticles(6);

        $payload = json_encode($shortArticles);
        $response->getBody()->write($payload);
        return $response->withHeader('Content-Type', 'application/json');
    }
}
