<?php

declare(strict_types=1);

namespace App\Http\Controllers\Backend\Pouzivatelia;

use App\Http\Controllers\Controller;
use App\Models\Pouzivatel\Pouzivatel;
use App\Http\Requests\PouzivatelUpdateRequest;

/**
 * Class PouzivateliaController
 *
 * @package App\Http\Controllers\Backend\Pouzivatelia
 */
final class PouzivateliaController extends Controller
{
    /**
     * @return mixed
     */
    public function index()
    {
        $pouzivatelia = Pouzivatel::query()->paginate();

        return view('pages.sprava.pouzivatelia.index')
            ->withPouzivatelia($pouzivatelia);
    }

    /**
     * @param int $pouzivatelId
     *
     * @return mixed
     */
    public function edit(int $pouzivatelId)
    {
        $pouzivatel = Pouzivatel::query()->with('pracovisko')->findOrFail($pouzivatelId);

        return view('pages.sprava.pouzivatelia.edit')
            ->withPouzivatel($pouzivatel);
    }

    /**
     * @param \App\Http\Requests\PouzivatelUpdateRequest $request
     * @param int                                        $pouzivatelId
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(PouzivatelUpdateRequest $request, int $pouzivatelId)
    {
        $pouzivatel = Pouzivatel::query()->findOrFail($pouzivatelId);

        $pouzivatel->update(
            collect($request->validated())
                ->only('stav')
                ->toArray()
        );

        flash()->success(sprintf('Nastavenia účtu používateľa %s boli úspešbe upravené.', $pouzivatel->meno));

        return response()->redirectToRoute('portal.sprava.pouzivatelia.edit', $pouzivatel);
    }
}
