<?php declare(strict_types=1);

namespace App\Engine\Home\Statistics;

final readonly class HomeStatisticsSectionProvider
{

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

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

    public function provideCached(): \App\Engine\Inertia\Frontend\ViewModels\HomeStatisticsSectionViewModel
    {
        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\HomeStatisticsSectionViewModel
    {
        $homeStatistics = $this->homeStatisticsSettingsSection->get();
        return new \App\Engine\Inertia\Frontend\ViewModels\HomeStatisticsSectionViewModel(
            value1: $homeStatistics->value1,
            value2: $homeStatistics->value2,
            value3: $homeStatistics->value3,
            value4: $homeStatistics->value4,
        );
    }

}