<?php

declare(strict_types=1);

namespace App\Http\Controllers\Backend\Pouzivatelia;

use App\Http\Controllers\Controller;
use App\Http\Requests\PouzivatelUpdateRequest;
use App\Repositories\PouzivatelRepositoryContract;

/**
 * Class PouzivateliaController
 *
 * @package App\Http\Controllers\Backend\Pouzivatelia
 */
final class PouzivateliaController extends Controller
{
    /**
     * @var \App\Repositories\PouzivatelRepositoryContract
     */
    private PouzivatelRepositoryContract $pouzivatelRepository;

    /**
     * PouzivateliaController constructor.
     *
     * @param \App\Repositories\PouzivatelRepositoryContract $pouzivatelRepository
     */
    public function __construct(PouzivatelRepositoryContract $pouzivatelRepository)
    {
        $this->pouzivatelRepository = $pouzivatelRepository;
    }

    /**
     * @return mixed
     */
    public function index()
    {
        $pouzivatelia = $this->pouzivatelRepository->paginate();

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

    /**
     * @param int $pouzivatelId
     *
     * @return mixed
     */
    public function edit(int $pouzivatelId)
    {
        $pouzivatel = $this->pouzivatelRepository->with('pracovisko')->find($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 = $this->pouzivatelRepository->find($pouzivatelId);
        $this->pouzivatelRepository->update(
            collect($request->validated())->only('stav')->toArray(),
            $pouzivatel->id
        );

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

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