<?php

namespace KupShop\ElninoBundle\Resources\script;

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

class RemoveInvalidProductsInSections extends Script
{
    protected static $name = 'Odstranit vazby na chybně přiřazené sekce dle pohlaví';

    protected static $defaultParameters = [
        'dryRun' => true,
        'invalidRootSectionsIDsPerSex' => [
            'W' => ['2'],
            'M' => ['1'],
            'K' => [],
            'B' => [],
        ],
    ];

    protected function run(array $arguments)
    {
        $this->log('Starting...');

        $rows = sqlQuery("
            SELECT aser.id_abra AS serie_id, aser.sex AS sex, p.id AS product_id, s.id AS section_id, s.name AS section_name,
                s_top.id AS top_section_id, s_top.name AS top_section_name
            FROM abra_series aser
            INNER JOIN abra_models m ON m.id_serie=aser.id_abra
            INNER JOIN products p ON p.id_model=m.id_abra
            INNER JOIN abra_secondary_assortment_group asag ON asag.id_product=p.id AND asag.id_abra_section != '0'
            INNER JOIN abra_sections asec ON asec.id_abra=asag.id_abra_section
            INNER JOIN products_in_sections pis ON pis.id_product=p.id AND pis.id_section=asec.id_section
                AND pis.generated = 0 AND pis.deletable <= 0
            INNER JOIN sections s ON s.id=pis.id_section
            LEFT JOIN sections_relation sr1 ON sr1.id_section=s.id
            LEFT JOIN sections_relation sr2 ON sr2.id_section=sr1.id_topsection
            LEFT JOIN sections_relation sr3 ON sr3.id_section=sr2.id_topsection
            LEFT JOIN sections_relation sr4 ON sr4.id_section=sr3.id_topsection
            LEFT JOIN sections_relation sr5 ON sr5.id_section=sr4.id_topsection
            LEFT JOIN sections_relation sr6 ON sr6.id_section=sr5.id_topsection
            LEFT JOIN sections_relation sr7 ON sr7.id_section=sr6.id_topsection
            LEFT JOIN sections_relation sr8 ON sr8.id_section=sr7.id_topsection
            LEFT JOIN sections_relation sr9 ON sr9.id_section=sr8.id_topsection
            LEFT JOIN sections_relation sr10 ON sr10.id_section=sr9.id_topsection
            LEFT JOIN sections_relation sr11 ON sr11.id_section=sr10.id_topsection
            INNER JOIN sections s_top ON s_top.id=COALESCE(sr11.id_topsection, sr10.id_topsection, sr9.id_topsection,
                sr8.id_topsection, sr7.id_topsection, sr6.id_topsection, sr5.id_topsection, sr4.id_topsection,
                sr3.id_topsection, sr2.id_topsection, sr1.id_topsection, s.id)
        ")->fetchAll();
        $count = 0;
        foreach ($rows as $row) {
            if (!empty($arguments['invalidRootSectionsIDsPerSex'][$row['sex']])
                && is_array($arguments['invalidRootSectionsIDsPerSex'][$row['sex']])
                && in_array($row['top_section_id'], $arguments['invalidRootSectionsIDsPerSex'][$row['sex']])
            ) {
                $count++; // invalid relation found
                echo '<br/><br/>'.PHP_EOL.PHP_EOL;
                print_r($row);
                if ($arguments['dryRun']) {
                    continue;
                }
                sqlQueryBuilder()->delete('products_in_sections')->where(Operator::equals([
                    'id_product' => $row['product_id'],
                    'id_section' => $row['section_id'],
                    'generated' => 0,
                ]))->andWhere('deletable <= 0')->execute();
                echo ' ... removed';
            }
        }

        echo '<br/><br/>'.PHP_EOL.PHP_EOL;
        if ($arguments['dryRun']) {
            $this->log("Celkem nalezeno {$count} záznamů.");
        } else {
            $this->log("Hotovo. Odstraněno {$count} záznamů.");
            echo '<br/><br/>'.PHP_EOL.PHP_EOL;
            echo 'Ještě to chce spustit: <a href="launch.php?s=list.php&type=sections&acn=generateVirtualSections">Přegenerovat virtuální sekce</a>';
            echo '<br/><br/>'.PHP_EOL.PHP_EOL;
        }
    }
}

return RemoveInvalidProductsInSections::class;
