<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Terminy\Http\Controllers\Frontend;

use Acaronlex\LaravelCalendar\Calendar;
use Eluceo\iCal\Presentation\Component;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;
use Seka\Portal\Modul\Terminy\Actions\iCalendarFactory;
use Seka\Portal\Modul\Terminy\Entities\Udalost\Udalost;
use Seka\Portal\Modul\Terminy\Http\Controllers\Controller;

/**
 * Class UdalostiController
 *
 * @package Seka\Portal\Modul\Terminy\Http\Controllers\Frontend
 */
final class UdalostiController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function index(): View
    {
        $udalosti = Udalost::query()
            ->whereDate('datum_od', '>', now()->startOfMonth()->subMonths(2))
            ->zverejnena()
            ->get();

        $calendar = new Calendar();
        $calendar
            ->addEvents($udalosti)
            ->setOptions([
                'locale' => 'sk',
                'timeZone' => config('app.timezone'),
                'firstDay' => 1,
                'initialView' => 'dayGridMonth',
                'themeSystem' => 'bootstrap',
                'weekNumbers' => true,
                'weekText' => '',
                'showNonCurrentDates' => false,
                'validRange' => [
                    'start' => now()->startOfMonth()->subMonths(2)->format('Y-m-d'),
                ],
                'headerToolbar' => [
                    'left' => 'prev,next today',
                    'center' => 'title',
                    'right' => 'dayGridMonth,listMonth',
                ],
                'eventTimeFormat' => [
                    'hour' => 'numeric',
                    'minute' => '2-digit',
                    'meridiem' => false,
                ],
            ]);

        return view('Terminy::pages.frontend.udalosti.index', compact('calendar'));
    }

    /**
     * @param int $id
     *
     * @return \Illuminate\Contracts\View\View
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function show(int $id): View
    {
        /** @var Udalost $udalost */
        $udalost = Udalost::query()
            ->with('miestoKonania')
            ->findOrFail($id);

        $pouzivatelJeUzRegistrovany = $udalost
            ->registracie()
            ->where('pouzivatel_id', auth()->user()->getAuthIdentifier())
            ->exists();

        return view(
            'Terminy::pages.frontend.udalosti.show',
            compact('udalost', 'pouzivatelJeUzRegistrovany'),
        );
    }

    /**
     * @param int $id
     *
     * @return \Eluceo\iCal\Presentation\Component
     * @throws \Exception
     */
    public function eventCalendarFile(int $id): Component
    {
        /** @var Udalost $udalost */
        $udalost = Udalost::query()
            ->with([
                'miestoKonania' => function ($query) {
                    $query->withTrashed();
                },
            ])
            ->findOrFail($id);

        $response = iCalendarFactory::make($udalost);

        $filename = sprintf(
            'seka-%s-%s-%s',
            Str::lower($udalost->druh->value),
            $udalost->datum_od->format('YmdHi'),
            $udalost->datum_do->format('YmdHi'),
        );

        header('Content-Type: text/calendar; charset=utf-8');
        header(sprintf('Content-Disposition: attachment; filename="%s.ics"', $filename));

        return $response;
    }
}
