<?php
namespace Incoviba\Controller;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Service;

class Search
{
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view, ?string $query = '', ?string $tipo = '*'): ResponseInterface
    {
        $post = $request->getParsedBody() ?? '';
        return $view->render($response, 'search', compact('post', 'query', 'tipo'));
    }
    public function query(ServerRequestInterface $request, ResponseInterface $response, Service\Search $service): ResponseInterface
    {
        $data = $request->getParsedBody();
        $results = $service->query($data['query'], $data['tipo']);
        $response->getBody()->write(json_encode(compact('results')));
        return $response->withHeader('Content-Type', 'application/json');
    }
}
