<?php

namespace KupShop\CatalogBundle\Resources\script;

use KupShop\AdminBundle\Util\Script\Script;
use KupShop\CatalogBundle\Util\FilterUtil;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class orderableSectionFiltersEnable extends Script
{
    protected static $defaultParameters = [
        'set_to' => 'Y',
        'filters' => ['in_store'],
    ];

    protected static $name = 'Zapnout nebo vypnout (\'Y\' nebo \'N\') custom filtr ve všech sekcích (price_range / in_store / labels / search)';

    private FilterUtil $filterUtil;

    public function __construct()
    {
        $this->filterUtil = ServiceContainer::getService(FilterUtil::class);
    }

    protected function run(array $arguments)
    {
        foreach ($arguments['filters'] as $filterType) {
            if ($arguments['set_to'] === 'N') {
                $ids = sqlQueryBuilder()->select('sf.id')
                    ->from('sections_filters', 'sf')
                    ->andWhere('sf.type = :type')
                    ->andWhere('sf.id_source_section IS NOT NULL OR sf.id_source_producer IS NOT NULL')
                    ->setParameter('type', $filterType)
                    ->execute()->fetchFirstColumn();

                $filterItems = [];
                foreach ($ids as $id) {
                    $filterItems[] = [
                        'id' => $id,
                        'enabled' => 'N',
                        'type' => $filterType,
                    ];
                }

                if (empty($filterItems)) {
                    continue;
                }

                $this->filterUtil->updateSectionFilters($filterItems);

                continue;
            }

            $sectionIds = sqlQueryBuilder()->select('s.id')
                ->from('sections', 's')
                ->andWhere('s.id NOT IN (SELECT id_source_section FROM sections_filters WHERE type = :type AND id_source_section IS NOT NULL)')
                ->setParameter('type', $filterType)
                ->execute()->fetchFirstColumn();

            $filterItems = [];
            foreach ($sectionIds as $sectionId) {
                $filterItems[] = [
                    'id_source_section' => $sectionId,
                    'type' => $filterType,
                    'position' => FilterUtil::POSITION_LAST,
                    'enabled' => $arguments['set_to'],
                ];
            }

            $producerIds = sqlQueryBuilder()->select('pr.id')
                ->from('producers', 'pr')
                ->andWhere('pr.id NOT IN (SELECT id_source_producer FROM sections_filters WHERE type = :type AND id_source_producer IS NOT NULL)')
                ->setParameter('type', $filterType)
                ->execute()->fetchFirstColumn();

            foreach ($producerIds as $producerId) {
                $filterItems[] = [
                    'id_source_producer' => $producerId,
                    'type' => $filterType,
                    'position' => FilterUtil::POSITION_LAST,
                    'enabled' => $arguments['set_to'],
                ];
            }

            if (empty($filterItems)) {
                continue;
            }

            $this->filterUtil->updateSectionFilters($filterItems);
        }
    }
}

return orderableSectionFiltersEnable::class;
