<?php

declare(strict_types=1);

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

use Seka\Portal\Modul\Vzdelavanie\Enums\ModelStatusEnum;
use Seka\Portal\Modul\Vzdelavanie\Events\Testovanie\Backend\TestCreated;
use Seka\Portal\Modul\Vzdelavanie\Events\Testovanie\Backend\TestDeleted;
use Seka\Portal\Modul\Vzdelavanie\Events\Testovanie\Backend\TestUpdated;
use Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Controller;
use Seka\Portal\Modul\Vzdelavanie\Http\Requests\Sprava\Testovanie\Test\TestStoreRequest;
use Seka\Portal\Modul\Vzdelavanie\Http\Requests\Sprava\Testovanie\Test\TestUpdateRequest;
use Seka\Portal\Modul\Vzdelavanie\Models\Testovanie\Test;

/**
 * Class TestController
 *
 * @package Seka\Portal\Modul\Vzdelavanie\Http\Controllers\Backend\Testovanie
 */
final class TestController extends Controller
{
    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function index()
    {
        $testy = Test::query()->orderByDesc('stav')->orderBy('nazov')->paginate(30);

        return view('Vzdelavanie::pages.sprava.testovanie.testy.index')
            ->withTesty($testy);
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function create()
    {
        $test = new Test();
        $test->setAttribute('stav', ModelStatusEnum::PUBLIKOVANE);
        $actionRoute = route('vzdelavanie.sprava.testovanie.testy.store');

        return view('Vzdelavanie::pages.sprava.testovanie.testy.novy')
            ->withTest($test)
            ->withActionRoute($actionRoute);
    }

    /**
     * @param \Seka\Portal\Modul\Vzdelavanie\Http\Requests\Sprava\Testovanie\Test\TestStoreRequest $request
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException|\Illuminate\Validation\ValidationException
     */
    public function store(TestStoreRequest $request)
    {
        $test = new Test();
        $test->fill($request->validated());
        $test->save();

        $test = $test->refresh();

        flash()->success(sprintf('Test "<strong>%s</strong>" bol úspešne vytvorený.', $test->nazov));

        event(new TestCreated($test));

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

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

        return view('Vzdelavanie::pages.sprava.testovanie.testy.upravit')
            ->withTest($test)
            ->withActionRoute($actionRoute);
    }

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

        flash()->success(sprintf('Test "<strong>%s</strong>" bol úspešbe upravený.', $test->nazov));

        event(new TestUpdated($test));

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

    /**
     * @param int $vzdTstTstId
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Exception
     */
    public function destroy(int $vzdTstTstId)
    {
        /** @var \Seka\Portal\Modul\Vzdelavanie\Models\Testovanie\Test $test */
        $test = Test::query()->findOrFail($vzdTstTstId);
        $test->delete();

        flash()->success(sprintf('Test "<strong>%s</strong>" bol úspešbe odstránený...', $test->nazov));

        event(new TestDeleted($test));

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