<?php

namespace KupShop\ElninoBundle\Resources\script;

use KupShop\AdminBundle\Util\Script\Script;
use KupShop\KupShopBundle\Config;

class RemovePicturesInPicturetypeRange extends Script
{
    // bug #9886

    protected static $name = 'Odstranit obrázky z rozsahu Picturetype (např Loreal 31-40 z neautorizovaného shopu)';

    protected static $defaultParameters = [
        'dryRun' => true,
        'picturetypeRange' => [31, 40], // Loreal
        'dryRunDisplayNumberOfRows' => 100,
    ];

    /** @var \AbraElnino */
    protected $abra;

    public function __construct()
    {
        $cfg = Config::get();
        $this->abra = new \AbraElnino($cfg['Modules']['abra']);
    }

    protected function run(array $arguments)
    {
        ini_set('max_execution_time', 600 * 60); // 10h
        ini_set('memory_limit', '2048M');

        $this->log('Starting...');

        if (empty($arguments['picturetypeRange'][0])
            || empty($arguments['picturetypeRange'][1])
            || $arguments['picturetypeRange'][0] > $arguments['picturetypeRange'][1]
        ) {
            $this->log('Invalid argument picturetypeRange! It should be array with two values, where first value is lower or equal to the second value.');

            return;
        }
        $low = $arguments['picturetypeRange'][0];
        $high = $arguments['picturetypeRange'][1];

        $qb = sqlQueryBuilder()->select('ppr.id_photo, ppr.id_product, api.id_abra AS abra_picture_id, api.type, api.id_abra_product')
            ->from('photos_products_relation', 'ppr')
            ->innerJoin('ppr', 'photos', 'ph', 'ph.id=ppr.id_photo')
            ->innerJoin('ppr', 'abra_products', 'apr', 'apr.id_product=ppr.id_product')
            ->innerJoin('ph', 'abra_pictures', 'api', 'api.id_picture=ph.id AND api.id_abra_product=apr.id_abra
                AND api.type >= :picturetypeLow AND api.type <= :picturetypeHigh')
            ->setParameter('picturetypeLow', $low)
            ->setParameter('picturetypeHigh', $high);

        $counter = 0;
        foreach ($qb->execute() as $row) {
            $counter++;
            if ($arguments['dryRun']) {
                if ($counter < $arguments['dryRunDisplayNumberOfRows']) {
                    $this->log('Found: PhotoID='.$row['id_photo'].', abraPictureID='.$row['abra_picture_id'].', picturetype='.$row['type'].', abraProductID='.$row['id_abra_product']);
                }
            } else {
                $this->log('Removing: PhotoID='.$row['id_photo'].', abraPictureID='.$row['abra_picture_id'].', picturetype='.$row['type'].', abraProductID='.$row['id_abra_product']);
                sqlGetConnection()->transactional(function () use (&$row) {
                    sqlQueryBuilder()->delete('photos')->where('id=:photoID')->setParameter('photoID', $row['id_photo'])
                        ->execute();
                    $this->abra->updatePhotosProductsRelation($row['id_abra_product'], $row['id_product']);
                });
            }
        }

        if ($arguments['dryRun']) {
            $this->log("Celkem nalezeno {$counter} obrázků v picturetype rozsahu {$low} až {$high}.");
        } else {
            $this->log("Hotovo. Smazáno {$counter} obrázků v picturetype rozsahu {$low} až {$high}.");
        }
    }
}

return RemovePicturesInPicturetypeRange::class;
