<?php

/**
 * Created by PhpStorm.
 * User: ondra
 * Date: 22.11.17
 * Time: 13:49.
 */

namespace KupShop\QuantityDiscountBundle\Resources\upgrade;

use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Contexts;
use Query\Operator;

class QuantityDiscountUpgrade extends \UpgradeNew
{
    public function check_quantityDiscounts()
    {
        return $this->checkTableExists('products_quantity_discounts');
    }

    /** Create table products_quantity_discounts */
    public function upgrade_quantityDiscounts()
    {
        sqlQuery('CREATE TABLE IF NOT EXISTS products_quantity_discounts (
              id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
              id_product INT(11) NOT NULL,
              pieces INT(11) NOT NULL,
              discount NUMERIC(12, 8) NOT NULL,
              CONSTRAINT FK_id_product FOREIGN KEY (id_product) REFERENCES products (id) ON DELETE CASCADE
              ) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;');

        $this->upgradeOK();
    }

    public function check_quantityDiscountGroupId(): bool
    {
        return $this->checkColumnExists('products_quantity_discounts', 'id_group');
    }

    /** products_quantity_discounts: add column 'id_group' */
    public function upgrade_quantityDiscountGroupId(): void
    {
        sqlQuery('ALTER TABLE products_quantity_discounts ADD COLUMN id_group INT(11) DEFAULT NULL AFTER id');
        sqlQuery('ALTER TABLE products_quantity_discounts ADD INDEX id_group_index (`id_group`)');

        $this->upgradeOK();
    }

    public function check_IdProductOnUpdateCascade(): bool
    {
        return $this->checkConstraintRule('products_quantity_discounts', 'FK_id_product', null, 'CASCADE');
    }

    /** products_quantity_discounts: id_product - add missing on update cascade */
    public function upgrade_IdProductOnUpdateCascade(): void
    {
        sqlQuery('alter table products_quantity_discounts drop foreign key FK_id_product;');

        sqlQuery('alter table products_quantity_discounts
            add constraint FK_id_product
                foreign key (id_product) references products (id)
                    on update cascade on delete cascade;');

        $this->upgradeOK();
    }

    public function check_quantityDiscountVariationId(): bool
    {
        return $this->checkColumnExists('products_quantity_discounts', 'id_variation');
    }

    /** products_quantity_discounts: add column 'id_variation' */
    public function upgrade_quantityDiscountVariationId(): void
    {
        sqlQuery('ALTER TABLE products_quantity_discounts ADD COLUMN id_variation INT(11) DEFAULT NULL AFTER id_product');
        sqlQuery('ALTER TABLE products_quantity_discounts ADD CONSTRAINT quantity_discounts_id_variation FOREIGN KEY (id_variation) REFERENCES products_variations (id) ON DELETE CASCADE ON UPDATE CASCADE');

        $this->upgradeOK();
    }

    public function check_quantityDiscountDiscountType(): bool
    {
        return $this->checkColumnExists('products_quantity_discounts', 'discount_type');
    }

    /** products_quantity_discounts: add column 'discount_type' */
    public function upgrade_quantityDiscountDiscountType(): void
    {
        sqlQuery("ALTER TABLE products_quantity_discounts ADD COLUMN discount_type VARCHAR(10) NOT NULL DEFAULT 'perc'");

        $this->upgradeOK();
    }

    public function check_QuantityDiscountsDiscountTypeChange(): bool
    {
        return sqlQueryBuilder()
            ->select('discount_type')
            ->from('products_quantity_discounts')
            ->where(Operator::equals(['discount_type' => 'amount']))
            ->execute()->rowCount() > 0;
    }

    /** Quantity discounts: use currency as discount_type instead of `amount` keyword */
    public function upgrade_QuantityDiscountsDiscountTypeChange(): void
    {
        sqlQueryBuilder()
            ->update('products_quantity_discounts')
            ->directValues(['discount_type' => Contexts::get(CurrencyContext::class)->getDefaultId()])
            ->where(Operator::equals(['discount_type' => 'amount']))
            ->execute();

        $this->upgradeOK();
    }
}
