<?php

declare(strict_types=1);

namespace Seka\Terminy\Http\Controllers\Backend\Udalosti;

use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Seka\Terminy\Models\Udalost\Udalost;
use Seka\Terminy\Http\Controllers\Controller;
use Seka\Terminy\Actions\RegistrationsCsvFactory;

/**
 * Class RegistracieUdalostiController
 *
 * @package Seka\Terminy\Http\Controllers\Backend\Udalosti
 */
final class RegistracieUdalostiController extends Controller
{
    /**
     * @param int $udalostId
     *
     * @return \Illuminate\Contracts\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function index(int $udalostId): View
    {
        /** @var \Seka\Terminy\Models\Udalost\Udalost $udalost */
        $udalost = Udalost::query()
            ->withTrashed()
            ->findOrFail($udalostId);

        $registracieQuery = $udalost->registracie()
            ->with('pracovisko', 'pouzivatel', 'vytvoril', 'udalost')
            ->orderByDesc('updated_at');

        /** @var \Illuminate\Database\Eloquent\Collection $registracieAktivne */
        $registracieAktivne = (clone $registracieQuery)->withoutTrashed()->get();
        /** @var \Illuminate\Database\Eloquent\Collection $registracieZrusene */
        $registracieZrusene = (clone $registracieQuery)->onlyTrashed()->get();

        return view('Terminy::pages.udalosti.registracie.backend.index')
            ->withUdalost($udalost)
            ->withRegistracieAktivne($registracieAktivne)
            ->withRegistracieZrusene($registracieZrusene);
    }

    /**
     * @param int $udalostId
     * @param int $registraciaId
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function delete(int $udalostId, int $registraciaId): RedirectResponse
    {
        /** @var \Seka\Terminy\Models\Udalost\Udalost $udalost */
        $udalost = Udalost::query()
            ->withTrashed()
            ->with('registracie', 'registracie.pracovisko', 'registracie.pouzivatel', 'registracie.vytvoril')
            ->findOrFail($udalostId);

        $registracia = $udalost->registracie()->findOrFail($registraciaId);

        $registracia->delete();

        flash()->overlay('Registrácia bola úspešne zrušená')->success();

        return redirect()->back();
    }

    /**
     * @param \Seka\Terminy\Actions\RegistrationsCsvFactory $csvFactory
     * @param int                                           $udalostId
     *
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function export(RegistrationsCsvFactory $csvFactory, int $udalostId)
    {
        /** @var Udalost $udalost */
        $udalost = Udalost::query()
            ->with('registracie.pouzivatel')
            ->findOrFail($udalostId);

        $filename = mb_strtoupper(
            sprintf(
                '%s_%s_registracie.csv',
                $udalost->druh->value,
                $udalost->datum_od->format('YmdHi')
            )
        );

        $headers = array(
            "Content-type" => "text/csv",
            "Content-Disposition" => "attachment; filename=$filename",
            "Pragma" => "no-cache",
            "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
            "Expires" => "0"
        );

        $callback = $csvFactory::make($udalost->registracie);

        return response()->stream($callback, 200, $headers);
    }
}
