<?php

declare(strict_types=1);

namespace KupShop\OrderingBundle\Resources\upgrade;

use KupShop\OrderingBundle\Resources\script\UpdatePaymentsScript;
use Query\Operator;

class OrderPaymentsFixUpgrade extends \UpgradeNew
{
    public function check_FixPayments(): bool
    {
        return $this->checkDataMigration(35);
    }

    /** Run `UpdatePaymentsScript` to fix payments bug after auto query-hint removed */
    public function upgrade_FixPayments(): void
    {
        // najdu si prvni nasazeni shopu od mergnuti prusvihu
        $deployDate = sqlQueryBuilder()
            ->select('date')
            ->from('report_activities')
            ->where('date > :date_from')
            ->andWhere(Operator::equals(['report' => 'Proběhlo nasazení nove verze e-shopu.']))
            ->setParameter('date_from', '2024-10-03')
            ->orderBy('id', 'ASC')
            ->execute()->fetchOne();

        // spustim scriptik
        if ($deployDate) {
            $script = new UpdatePaymentsScript();
            $script->run(['date_updated_from' => $deployDate]);
        }

        $this->commitDataMigration(35);
    }

    public function check_FixPayments2(): bool
    {
        return $this->checkDataMigration(36);
    }

    /** Run `UpdatePaymentsScript` to fix payments bug after auto query-hint removed - take 2 */
    public function upgrade_FixPayments2(): void
    {
        $this->upgrade_FixPayments();

        $this->commitDataMigration(36);
    }
}
