<?php

namespace KupShop\AdminBundle\Resources\script;

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

class CleanWatchdogScript extends Script
{
    protected static $name = '[WATCHDOG] - REMOVE DUPLICITY';

    protected static $defaultParameters = [
        'duplicity' => false,
        'product' => [
            'HELP' => 'Set RUN true, fill id_product and null to id_variation to remove all products without variations, fill id_product and id_variation too to remove just one variation',
            'run' => false,
            'id_product' => false,
            'id_variation' => false,
        ],
    ];

    public function run(array $arguments)
    {
        $this->log('RUN SCRIPT - [WATCHDOG]');
        if (($arguments['duplicity'] ?? false) === true) {
            $this->removeDuplicity();
        }
        if (($arguments['product']['run'] ?? false) === true && ($arguments['product']['id_product'] ?? false)) {
            $this->removeProduct($arguments['product']);
        }
        $this->log('OK');

        return true;
    }

    protected function removeProduct($product)
    {
        var_dump($product);
        if ($product['id_variation'] === false) {
            $this->log('YOU MUST SPECIFY NULL OR ID TO id_variation');

            return false;
        }
        if ($product['id_variation'] === null) {
            $this->log('REMOVING PRODUCT: '.$product['id_product'].' WITH ALL VARIATIONS');
        } else {
            $this->log('REMOVING PRODUCT: '.$product['id_product'].' WITH VARIATION: '.$product['id_variation']);
        }

        $deletions = sqlQueryBuilder()
            ->delete('products_watchdog')
            ->where(Operator::equalsNullable([
                'id_product' => $product['id_product'],
                'id_variation' => $product['id_variation'],
            ]))
            ->execute();
        $this->log('WAS REMOVED '.$deletions.' WATCHDOGS');
    }

    protected function removeDuplicity()
    {
        $count = 0;
        $this->log('REMOVING DUPLICITY...');
        $duplicity = sqlQueryBuilder()
            ->select('pw.*, count(*) as cnt')
            ->from('products_watchdog', 'pw')
            ->groupBy('pw.id_user, pw.id_product, pw.id_variation')
            ->having('cnt > 1')
            ->execute();
        foreach ($duplicity as $d) {
            $count = $count + $d['cnt'];
            sqlQueryBuilder() // vymažeme všechny výskyty
                ->delete('products_watchdog')
                ->where(Operator::equalsNullable([
                    'id_user' => $d['id_user'],
                    'id_product' => $d['id_product'],
                    'id_variation' => $d['id_variation'],
                ]))
                ->execute();
            unset($d['cnt']);
            sqlQueryBuilder() // vrátíme jako jeden záznam
                ->insert('products_watchdog')
                ->directValues($d)
                ->execute();
        }
        $this->log($count.' DUPLICITY WAS REMOVED FROM WATCHDOG');
    }
}

return CleanWatchdogScript::class;
