<?php
/**
 * Mirasvit
 *
 * This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
 * Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
 * If you wish to customize this module for your needs.
 * Please refer to http://www.magentocommerce.com for more information.
 *
 * @category  Mirasvit
 * @package   mirasvit/module-cache-warmer
 * @version   1.11.13
 * @copyright Copyright (C) 2026 Mirasvit (https://mirasvit.com/)
 */



namespace Mirasvit\CacheWarmer\Controller\Track;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Http\Context as HttpContext;
use Mirasvit\CacheWarmer\Api\Data\PageInterface;
use Mirasvit\CacheWarmer\Api\Repository\PageRepositoryInterface;
use Mirasvit\CacheWarmer\Api\Service\LogServiceInterface;
use Mirasvit\CacheWarmer\Api\Service\PageServiceInterface;
use Mirasvit\CacheWarmer\Service\WarmRuleService;
use Mirasvit\Core\Service\SerializeService;

/**
 * Purpose: track popularity & reports data (hit/miss, time)
 */
class Index extends Action
{
    private $pageRepository;

    private $pageService;

    private $logService;

    private $warmRuleService;

    private $request;

    private $httpContext;

    public function __construct(
        PageRepositoryInterface $pageRepository,
        PageServiceInterface $pageService,
        LogServiceInterface $logService,
        WarmRuleService $warmRuleService,
        HttpContext $httpContext,
        Context $context
    ) {
        parent::__construct($context);

        $this->pageRepository  = $pageRepository;
        $this->pageService     = $pageService;
        $this->logService      = $logService;
        $this->warmRuleService = $warmRuleService;
        $this->request         = $context->getRequest();
        $this->httpContext     = $httpContext;
    }

    /**
     * {@inheritdoc}
     */
    public function execute()
    {
        $uri   = $this->request->getParam('uri');
        $ttfb  = (float)$this->request->getParam('ttfb');
        $isHit = $this->request->getParam('isHit') ? true : false;
        
        if (!$uri) {
            /** @var \Magento\Framework\App\Response\Http\Interceptor $response */
            $response = $this->getResponse();
            $response->representJson(SerializeService::encode(['success' => false]));
        } else {
            $result = $this->increasePopularity($uri);

            $this->logService->log($uri, $ttfb, $isHit);

            /** @var \Magento\Framework\App\Response\Http\Interceptor $response */
            $response = $this->getResponse();
            $response->representJson(SerializeService::encode(['success' => $result]));
        }
    }

    /**
     * @param string $uri
     * @return bool
     */
    private function increasePopularity($uri)
    {
        $varyDataHash = $this->pageService->getVaryDataHash($this->request);
        /** @var PageInterface $page */
        $page = $this->pageRepository->getByURI($uri, $varyDataHash);

        if ($page && $page->getId()) {
            $page->setPopularity($page->getPopularity() + 1);

            if ($page->getPopularity() % 10 == 0) {
                $this->warmRuleService->refreshRulesForPage($page);
            }

            $this->pageRepository->save($page);

            return true;
        }

        return false;
    }
}
