<?php
declare(strict_types=1);

namespace Autogedal\BlackFriday\Model\Config\Backend;

use Autogedal\BlackFriday\Model\CacheCleaner;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Value;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;

/** Settings that change what the landing page shows: the page and its product data are dropped from the cache. */
class CacheCleaning extends Value
{
    public function __construct(
        Context $context,
        Registry $registry,
        ScopeConfigInterface $config,
        TypeListInterface $cacheTypeList,
        protected readonly CacheCleaner $cacheCleaner,
        ?AbstractResource $resource = null,
        ?AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
    }

    public function afterSave()
    {
        if ($this->isValueChanged()) {
            $this->cacheCleaner->clean();
        }
        return parent::afterSave();
    }
}
