<?php

namespace KupShop\ElninoBundle\Resources\script;

use KupShop\AdminBundle\Util\Script\Script;
use Query\Operator;

class RemoveProducerBannerScript extends Script
{
    protected static $name = 'Odstranit banery výrobců';

    protected static $defaultParameters = [
        'dryRun' => true,
        'ignoreSliderIDs' => [5],
    ];

    protected function run(array $arguments)
    {
        $this->log('Starting... '.($arguments['dryRun'] ? 'DRY_RUN' : 'PROD_RUN'));

        $qb = sqlQueryBuilder()->select('pr.id, pr.name, pr.data')->from('producers', 'pr')
            ->where("JSON_VALUE(pr.data, '$.id_slider') > 0");
        $sliderIDs = [];
        $producerIDs = [];
        foreach ($qb->execute() as $producer) {
            $data = json_decode($producer['data'], true);

            if (in_array((int) $data['id_slider'], $arguments['ignoreSliderIDs'])) {
                $this->log(" - Producer [{$producer['id']}], sliderID [{$data['id_slider']}]: {$producer['name']} - IGNORED");
                continue;
            }

            $producerIDs[$producer['id']] = $producer['id'];
            $sliderIDs[$data['id_slider']] = $data['id_slider'];
            $this->log(" - Producer [{$producer['id']}], sliderID [{$data['id_slider']}]: {$producer['name']}");
            unset($data['id_slider']);
            if (!$arguments['dryRun']) {
                sqlQueryBuilder()->update('producers')
                    ->set('data', ':data')->setParameter('data', json_encode($data))
                    ->where(Operator::equals(['id' => $producer['id']]))
                    ->execute();
            }
        }

        $this->log('----------');
        $this->log('----------');
        $this->log('Producer IDs: '.join(', ', $producerIDs));
        $this->log('Slider IDs: '.join(', ', $sliderIDs));

        $this->log('----------');
        $this->log('Processing photos:');
        $photosQb = sqlQueryBuilder()->select('GROUP_CONCAT(sl.name) AS slider_names, GROUP_CONCAT(sl.id) AS slider_ids, ph.id AS photo_id')->from('photos', 'ph')
            ->innerJoin('ph', 'sliders_images', 'sli', 'sli.id_photo=ph.id')
            ->innerJoin('sli', 'sliders', 'sl', 'sl.id=sli.id_slider')
            ->where(Operator::inIntArray($sliderIDs, 'sl.id'))
            ->groupBy('ph.id')
            ->orderBy('sl.id')->addOrderBy('ph.id');
        foreach ($photosQb->execute() as $photo) {
            $this->log(" - photo [{$photo['photo_id']}]: slider [{$photo['slider_ids']}] {$photo['slider_names']}");
            if (!$arguments['dryRun']) {
                $img = new \Photos('none');
                $img->newImage($photo['photo_id']);
                $img->deletePhoto();
            }
        }

        // delete sliders
        if (!$arguments['dryRun']) {
            sqlQueryBuilder()->delete('sliders')->where(Operator::inIntArray($sliderIDs, 'id'))->execute();
        }

        $this->log('==========');
        $this->log('==========');
        $this->log('Updated producer IDs: '.join(', ', $producerIDs));
        $this->log('Removed slider IDs: '.join(', ', $sliderIDs));
        $this->log('----------');

        if (!$arguments['dryRun']) {
            \MenuSectionTree::invalidateCache();
            clearCache('sliders', true);
        }

        $this->log('Hotovo. '.($arguments['dryRun'] ? 'DRY_RUN' : 'PROD_RUN'));
    }
}

return RemoveProducerBannerScript::class;
