<?php

declare(strict_types=1);

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

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

/**
 * Class TestOtazkyController
 *
 * @package Seka\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')
            ->withTest($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')
            ->withTest($test)
            ->withActionRoute($actionRoute);
    }

    /**
     * @param \Seka\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')
            ->withTest($test)
            ->withOtazka($otazka)
            ->withActionRoute($actionRoute);
    }

    /**
     * @param \Seka\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);
    }
}
