<?php declare(strict_types=1);

namespace App\Engine\Home\Hero;

final readonly class HomeHeroSectionProvider
{

    public const CACHE_TTL = 86400; // 1 den
    public const CACHE_KEY = "HomeHeroSection";

    public function __construct(
        private \App\Engine\Settings\Sections\Content\HomeHeroSettingsSection $homeHeroSettingsSection,
        private \Symfony\Contracts\Cache\CacheInterface                       $fileAdapterCache,
    ) {
    }

    public function provideCached(): \App\Engine\Inertia\Frontend\ViewModels\HomeHeroSectionViewModel
    {
        return $this->fileAdapterCache->get(self::CACHE_KEY, function (\Symfony\Component\Cache\CacheItem $item) {
            $item->expiresAfter(self::CACHE_TTL);
            return $this->provide();
        });
    }

    public function provide(): \App\Engine\Inertia\Frontend\ViewModels\HomeHeroSectionViewModel
    {
        $hero = $this->homeHeroSettingsSection->get();
        return new \App\Engine\Inertia\Frontend\ViewModels\HomeHeroSectionViewModel(
            desktopImage: \App\Engine\Thumbnail\Thumbnail::create()
                ->fromRelativePath($hero->desktopImage)
                ->height(1383)
                ->width(2880)
                ->sizeOperation(\App\Engine\Thumbnail\SizeOperation::COVER)
                ->getPictureHtmlTag(),
            mobileImage: \App\Engine\Thumbnail\Thumbnail::create()
                ->fromRelativePath($hero->mobileImage)
                ->height(1350)
                ->width(1350)
                ->sizeOperation(\App\Engine\Thumbnail\SizeOperation::COVER)
                ->getPictureHtmlTag()
        );
    }

}