<?php declare(strict_types=1);

namespace App\Controller;

class UpcomingEventController extends \App\Engine\Inertia\Frontend\BaseInertiaFrontendController
{

    public function detail(
        \App\Engine\UpcomingEvent\UpcomingEventRepository $upcomingEventRepository,
        \Symfony\Component\HttpFoundation\Request         $request,
        \App\Engine\PageMeta\MetadataHandler              $metadataHandler,
    ): \Symfony\Component\HttpFoundation\Response {
        if (($urlPart = $request->attributes->get("urlPart")) === null) {
            throw $this->createNotFoundException();
        }

        $urlPart = \App\Engine\Utills\StringHelpers::sanitizeUrlAsAbsoulutePath($urlPart);
        $upcomingEventEntity = $upcomingEventRepository->findOneBy(["urlPart" => $urlPart, "enabled" => true]);
        if ($upcomingEventEntity === null) {
            throw $this->createNotFoundException();
        }

        $metadataHandler->handleUpcomingEvent($upcomingEventEntity);

        $props = new \App\Engine\Inertia\Frontend\Props\UpcomingEventDetailProps();
        $props->content = new \App\Engine\Inertia\Frontend\ViewModels\UpcomingEventDetailViewModel(
            title: $upcomingEventEntity->getTitle(),
            mainImagePictureTag: \App\Engine\Thumbnail\Thumbnail::create()
                ->fromRelativePath($upcomingEventEntity->getMainOrFallbackImage())
                ->alt($upcomingEventEntity->getTitle())
                ->height(500)
                ->width(500)
                ->getPictureHtmlTag(),
            htmlContent: $upcomingEventEntity->getText(),
        );

        return $this->inertia->render(
            component: "{$this->getVueComponent()}/Detail",
            props: $this->getInertiaProps($props),
        );
    }

    public function getId(): string
    {
        return "UpcomingEvent";
    }

    public function list(
        \App\Engine\UpcomingEvent\UpcomingEventProvider $upcomingEventProvider,
    ): \Symfony\Component\HttpFoundation\Response {
        $props = new \App\Engine\Inertia\Frontend\Props\UpcomingEventListProps();
        $props->imagePictureTag = ''; // Aktuálně nepoužíváme.
        $props->list = $upcomingEventProvider->provideForEventsListCached();
        $props->archive = $upcomingEventProvider->provideForEventsArchiveCached();

        return $this->inertia->render(
            component: "{$this->getVueComponent()}/List",
            props: $this->getInertiaProps($props),
        );
    }

    public function trainingList(
        \App\Engine\UpcomingEvent\UpcomingEventProvider $upcomingEventProvider,
        \App\Engine\Page\PageRepository                 $pageRepository,
        \App\Engine\PageMeta\MetadataHandler            $metadataHandler,
    ): \Symfony\Component\HttpFoundation\Response {
        $page = $pageRepository->findOneBy(["internalIdentifier" => \App\Engine\Page\PageDefinitions::TRAININGS_INTERNAL_IDENTIFIER]);
        if (!$page) {
            throw $this->createNotFoundException();
        }
        $metadataHandler->handlePage($page);

        $props = new \App\Engine\Inertia\Frontend\Props\UpcomingEventTrainingListProps();
        $props->imagePictureTag = \App\Engine\Thumbnail\Thumbnail::create()
            ->fromRelativePath("assets/images/frontend/subpage/header/training-list.png")
            ->height(500)
            ->width(500)
            ->alt("Dětská misie - školení")
            ->getPictureHtmlTag();
        $props->list = $upcomingEventProvider->provideForEventsTrainingListCached();
        $props->content = $page->getText();

        return $this->inertia->render(
            component: "{$this->getVueComponent()}/TrainingList",
            props: $this->getInertiaProps($props),
        );
    }

}
