<?php
/**
 * Created by PhpStorm.
 * User: radko
 * Date: 2019-02-11
 * Time: 15:16
 */

namespace Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Api\Frontend\Kniznica;

use Illuminate\Http\JsonResponse;
use Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Api\Controller;
use Seka\Portal\Modul\Vzdelavanie\Http\Requests\VyhladavanieIndexRequest;
use Seka\Portal\Modul\Vzdelavanie\Http\Resources\SekciaResource;
use Seka\Portal\Modul\Vzdelavanie\Http\Resources\UcebnicaResource;
use Seka\Portal\Modul\Vzdelavanie\Services\SekciaServiceInterface;
use Seka\Portal\Modul\Vzdelavanie\Services\UcebnicaServiceInterface;

/**
 * Class VyhladavanieController
 *
 * @package Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Api\Frontend\Kniznica
 */
class VyhladavanieController extends Controller
{
    /**
     * @var \Seka\Portal\Modul\Vzdelavanie\Services\UcebnicaServiceInterface
     */
    private $ucebnicaService;

    /**
     * @var \Seka\Portal\Modul\Vzdelavanie\Services\SekciaServiceInterface
     */
    private $sekciaService;

    /**
     * VyhladavanieController constructor.
     *
     * @param \Seka\Portal\Modul\Vzdelavanie\Services\UcebnicaServiceInterface $ucebnicaService
     * @param \Seka\Portal\Modul\Vzdelavanie\Services\SekciaServiceInterface   $sekciaService
     */
    public function __construct(UcebnicaServiceInterface $ucebnicaService, SekciaServiceInterface $sekciaService)
    {
        $this->ucebnicaService = $ucebnicaService;
        $this->sekciaService = $sekciaService;
    }

    /**
     * @param \Seka\Portal\Modul\Vzdelavanie\Http\Requests\VyhladavanieIndexRequest $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function index(VyhladavanieIndexRequest $request): JsonResponse
    {
        $text = request('text');

        return response()->json([
            'ucebne_materialy' => UcebnicaResource::collection($this->ucebnicaService->vyhladaj($text)),
            'sekcie' => SekciaResource::collection($this->sekciaService->vyhladaj($text))->hide(['obsah']),
        ]);
    }
}
