<?php declare(strict_types=1);

namespace App\Engine\PageMeta\GeneralMetadata;

final readonly class GeneralMetadataSectionProvider
{

    public const CACHE_TTL = 3600;
    public const CACHE_KEY = "GeneralMetadataSection";

    public function __construct(
        private \App\Engine\Settings\Sections\SEO\GeneralMetadataSettingsSection $generalMetadataSettingsSection,
        private \Symfony\Contracts\Cache\CacheInterface                          $fileAdapterCache,
    ) {
    }

    public function provideCached(): \App\Engine\Inertia\Frontend\ViewModels\GeneralMetadataSectionViewModel
    {
        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\GeneralMetadataSectionViewModel
    {
        $generalMetadata = $this->generalMetadataSettingsSection->get();
        return new \App\Engine\Inertia\Frontend\ViewModels\GeneralMetadataSectionViewModel(
            title: $generalMetadata->title,
            description: $generalMetadata->description,
            ogImage: \App\Engine\Thumbnail\Thumbnail::create()
                ->fromRelativePath($generalMetadata->ogImage)
                ->height(630)
                ->width(1200)
                ->sizeOperation(\App\Engine\Thumbnail\SizeOperation::COVER)
                ->getThumbnailPath(),
        );
    }

}