<?php

declare(strict_types=1);

namespace App\Http\Controllers\Backend\Pouzivatelia;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\PouzivatelUpdateRequest;
use App\Repositories\PouzivatelRepositoryContract;
use App\Repositories\Eloquent\Criteria\OrderDescCriteria;
use App\Notifications\Push\MAAISEKUserSendPushNotification;

/**
 * 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()
    {
        $this->pouzivatelRepository->pushCriteria(OrderDescCriteria::class);
        $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);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @param int                      $pouzivatelId
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function send(Request $request, int $pouzivatelId)
    {
        $message = $request->get('message') ?? 'Testovacia notifikácia...';
        $pouzivatel = $this->pouzivatelRepository->find($pouzivatelId);

        $pouzivatel->notify(new MAAISEKUserSendPushNotification(Str::limit($message, 50)));

        flash()->success(sprintf('Notifikácia pre používateľa %s bola zaradená na odoslanie.', $pouzivatel->meno))->overlay();

        return back();
    }
}
