<?php

declare(strict_types=1);

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

use Illuminate\Support\Facades\Auth;
use Seka\Vzdelavanie\Enums\ModelStatusEnum;
use Seka\Vzdelavanie\Models\Testovanie\Test;
use Seka\Vzdelavanie\Http\Controllers\Controller;
use Seka\Vzdelavanie\Events\Testovanie\Backend\TestCreated;
use Seka\Vzdelavanie\Events\Testovanie\Backend\TestUpdated;
use Seka\Vzdelavanie\Events\Testovanie\Backend\TestDeleted;
use Seka\Vzdelavanie\Http\Requests\Sprava\Testovanie\Test\TestStoreRequest;
use Seka\Vzdelavanie\Http\Requests\Sprava\Testovanie\Test\TestUpdateRequest;

/**
 * Class TestController
 *
 * @package Seka\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\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, Auth::user()));

        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\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\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, Auth::user()));

        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\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, Auth::user()));

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