<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Backend\Testovanie;

use Illuminate\Support\Str;
use Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Controller;
use Seka\Portal\Modul\Vzdelavanie\Http\Requests\Sprava\Testovanie\Otazka\OtazkaStoreRequest;
use Seka\Portal\Modul\Vzdelavanie\Http\Requests\Sprava\Testovanie\Otazka\OtazkaUpdateRequest;
use Seka\Portal\Modul\Vzdelavanie\Models\Testovanie\Otazka;
use Seka\Portal\Modul\Vzdelavanie\Models\Testovanie\Test;

/**
 * Class TestOtazkyController
 *
 * @package Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Backend\Testovanie
 */
final class TestOtazkyController extends Controller
{
    /**
     * @param int $vzdTstTstId
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function index(int $vzdTstTstId)
    {
        $test = Test::query()->with('otazky')->findOrFail($vzdTstTstId);

        return view('Vzdelavanie::pages.sprava.testovanie.otazky.index', compact('test'));
    }

    /**
     * @param int $vzdTstTstId
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function create(int $vzdTstTstId)
    {
        $test = Test::query()->findOrFail($vzdTstTstId);
        $actionRoute = route('vzdelavanie.sprava.testovanie.testy.otazky.store', $test);

        return view('Vzdelavanie::pages.sprava.testovanie.otazky.nova', compact('test', 'actionRoute'));
    }

    /**
     * @param \Seka\Portal\Modul\Vzdelavanie\Http\Requests\Sprava\Testovanie\Otazka\OtazkaStoreRequest $request
     * @param int                                                                                      $vzdTstTstId
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(OtazkaStoreRequest $request, int $vzdTstTstId)
    {
        $test = Test::query()->findOrFail($vzdTstTstId);
        $otazka = new Otazka();
        $otazka->fill($request->validated());
        $otazka->test()->associate($test);
        $otazka->save();
        $otazka = $otazka->refresh();

        flash()->success(
            sprintf('Otázka "<strong>%s</strong>" testu bola úspešne vytvorená.', Str::limit($otazka->znenie, 25)),
        );

        return response()->redirectToRoute(
            'vzdelavanie.sprava.testovanie.testy.otazky.edit',
            [
                $test,
                $otazka,
            ],
        );
    }

    /**
     * @param int $vzdTstTstId
     * @param int $vzdTstOtzkId
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function edit(int $vzdTstTstId, int $vzdTstOtzkId)
    {
        $test = Test::query()->findOrFail($vzdTstTstId);
        $otazka = Otazka::query()->with('odpovede')->findOrFail($vzdTstOtzkId);
        $actionRoute = route(
            'vzdelavanie.sprava.testovanie.testy.otazky.update',
            [
                $test,
                $otazka,
            ],
        );

        return view('Vzdelavanie::pages.sprava.testovanie.otazky.upravit', compact('test', 'otazka', 'actionRoute'));
    }

    /**
     * @param \Seka\Portal\Modul\Vzdelavanie\Http\Requests\Sprava\Testovanie\Otazka\OtazkaUpdateRequest $request
     * @param int                                                                                       $vzdTstTstId
     * @param int                                                                                       $vzdTstOtzkId
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(OtazkaUpdateRequest $request, int $vzdTstTstId, int $vzdTstOtzkId)
    {
        $test = Test::query()->findOrFail($vzdTstTstId);
        $otazka = Otazka::query()->findOrFail($vzdTstOtzkId);
        $otazka->fill($request->validated());
        $otazka->save();

        flash()->success(
            sprintf('Otázka "<strong>%s</strong>" testu bola úspešne upravená.', Str::limit($otazka->znenie, 25)),
        );

        return response()->redirectToRoute(
            'vzdelavanie.sprava.testovanie.testy.otazky.edit',
            [
                $test,
                $otazka,
            ],
        );
    }

    /**
     * @param int $vzdTstTstId
     * @param int $vzdTstOtzkId
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Exception
     */
    public function destroy(int $vzdTstTstId, int $vzdTstOtzkId)
    {
        $otazka = Otazka::query()->with('test')->findOrFail($vzdTstOtzkId);
        $otazka->delete();

        flash()->success(
            sprintf('Otázka "<strong>%s</strong>" testu bola úspešne odstránená.', Str::limit($otazka->znenie, 25)),
        );

        return response()->redirectToRoute('vzdelavanie.sprava.testovanie.testy.otazky.index', $otazka);
    }
}
