<?php

namespace Bastion\Handlers;

use Openguru\OpenCartFramework\Cache\CacheInterface;
use Openguru\OpenCartFramework\TeleCartPulse\TeleCartEvent;
use Symfony\Component\HttpFoundation\JsonResponse;

class TeleCartPulseStatsHandler
{
    private TeleCartEvent $eventModel;
    private CacheInterface $cache;
    private const CACHE_KEY = 'telecart_pulse_stats';
    private const CACHE_TTL = 3600; // 1 час

    public function __construct(TeleCartEvent $eventModel, CacheInterface $cache)
    {
        $this->eventModel = $eventModel;
        $this->cache = $cache;
    }

    public function getStats(): JsonResponse
    {
        $stats = $this->cache->get(self::CACHE_KEY);

        if ($stats === null) {
            $stats = $this->eventModel->getStats();
            $this->cache->set(self::CACHE_KEY, $stats, self::CACHE_TTL);
        }

        return new JsonResponse(['data' => $stats]);
    }
}
