<?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\Adminhtml\Page;

use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Module\ModuleListInterface;
use Magento\Ui\Component\MassAction\Filter;
use Mirasvit\CacheWarmer\Api\Repository\PageRepositoryInterface;
use Mirasvit\CacheWarmer\Api\Service\WarmerServiceInterface;
use Mirasvit\CacheWarmer\Controller\Adminhtml\AbstractPage;
use Mirasvit\CacheWarmer\Model\Config;
use Mirasvit\CacheWarmer\Repository\SourceRepository;

class Index extends AbstractPage
{
    private $sourceRepository;

    public function __construct(
        SourceRepository $sourceRepository,
        PageRepositoryInterface $pageRepository,
        WarmerServiceInterface $warmerService,
        ModuleListInterface $moduleList,
        Config $config,
        Filter $filter,
        Context $context
    ) {
        $this->sourceRepository = $sourceRepository;

        parent::__construct($pageRepository, $warmerService, $moduleList, $config, $filter, $context);
    }

    /**
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);

        if (!$this->sourceRepository->getDefaultSource()) {
            $this->messageManager->addErrorMessage(
                (string)__("Required default pages' source (Visitors' actions) is missing. Please create the required source.")
            );
        }

        if ($this->config->getDataVersion() < Config::DATA_VERSION) {
            $this->messageManager->addWarningMessage(
                (string)__(
                    "No pages will be collected until at least one warming cron job is executed. Wait until the warming cron job is executed or run it manually using the command bin/magento mirasvit:cache-warmer --warm"
                )
            );
        }

        $this->initPage($resultPage);

        return $resultPage;
    }
}
