<?php

namespace KupShop\AutomationConfiguratorBundle\Resources\upgrade;

class ContextUpgrade extends \UpgradeNew
{
    public function check_OrderConfigurationTable()
    {
        return $this->checkTableExists('automation_configurator');
    }

    /** Create table automation_configurator */
    public function upgrade_OrderConfigurationTable()
    {
        sqlQuery("
        create table automation_configurator
(
    id        int auto_increment
        primary key,
    name      varchar(100)                null,
    context      varchar(100)                null,
    descr     text                        null,
    detailLog enum ('Y', 'N') default 'N' null,
    active    enum ('Y', 'N') default 'N' null,
    appData   text                        null
);      
        ");

        $this->upgradeOK();
    }

    public function check_AutomationConfiguratorHistoryTable()
    {
        return $this->checkTableExists('automation_configurator_history');
    }

    /** Create table automation_configurator_history */
    public function upgrade_AutomationConfiguratorHistoryTable()
    {
        sqlQuery('
create table automation_configurator_history
(
    id              int auto_increment
        primary key,
    id_configurator int                                  not null,
    id_order        int unsigned                         null,
    date            datetime default current_timestamp() null,
    constraint automation_configurator_history_automation_configurator_id_fk
        foreign key (id_configurator) references automation_configurator (id),
    constraint automation_configurator_history_orders_id_fk
        foreign key (id_order) references orders (id)
);   
        ');

        $this->upgradeOK();
    }

    public function check_AutomationConfiguratorApplyOnlyOnce()
    {
        return $this->checkColumnExists('automation_configurator', 'applyOnlyOnce');
    }

    /** table automation_configurator add field applyOnlyOnce */
    public function upgrade_AutomationConfiguratorApplyOnlyOnce()
    {
        sqlQuery("
alter table automation_configurator
	add applyOnlyOnce enum('Y', 'N') default 'Y' null after active;
        ");

        $this->upgradeOK();
    }

    public function check_AutomationConfigurationHistoryForeignKeysFix(): bool
    {
        return $this->checkConstraintRule('automation_configurator_history', 'automation_configurator_history_automation_configurator_id_fk', 'CASCADE', 'CASCADE');
    }

    /** automation_configurator_history: fix foreign keys */
    public function upgrade_AutomationConfigurationHistoryForeignKeysFix(): void
    {
        sqlQuery('ALTER TABLE automation_configurator_history DROP FOREIGN KEY automation_configurator_history_automation_configurator_id_fk;');
        sqlQuery('ALTER TABLE automation_configurator_history DROP FOREIGN KEY automation_configurator_history_orders_id_fk;');

        sqlQuery('ALTER TABLE automation_configurator_history ADD CONSTRAINT automation_configurator_history_automation_configurator_id_fk FOREIGN KEY (id_configurator) REFERENCES automation_configurator (id) ON UPDATE CASCADE ON DELETE CASCADE;');
        sqlQuery('ALTER TABLE automation_configurator_history ADD CONSTRAINT automation_configurator_history_orders_id_fk FOREIGN KEY (id_order) REFERENCES orders (id) ON UPDATE CASCADE ON DELETE CASCADE;');

        $this->upgradeOK();
    }

    public function check_AutomationConfigurationHistorySales(): bool
    {
        return findModule(\Modules::SALES) && $this->checkColumnExists('automation_configurator_history', 'id_sale');
    }

    /** Add `automation_configurator_history.id_sale` column */
    public function upgrade_AutomationConfigurationHistorySales(): void
    {
        sqlQuery('ALTER TABLE automation_configurator_history ADD COLUMN id_sale INT(11) UNSIGNED NULL AFTER id_order');
        sqlQuery('ALTER TABLE automation_configurator_history ADD CONSTRAINT FK_automation_configurator_history_id_sale FOREIGN KEY (id_sale) REFERENCES sales (id) ON UPDATE CASCADE ON DELETE CASCADE');

        $this->upgradeOK();
    }

    public function check_AutomationConfigurationHistoryUsers(): bool
    {
        return $this->checkColumnExists('automation_configurator_history', 'id_user');
    }

    /** Add `automation_configurator_history.id_user` column */
    public function upgrade_AutomationConfigurationHistoryUsers(): void
    {
        sqlQuery('ALTER TABLE automation_configurator_history ADD COLUMN id_user INT(11) UNSIGNED NULL AFTER id_order');
        sqlQuery('ALTER TABLE automation_configurator_history ADD CONSTRAINT FK_automation_configurator_history_id_user FOREIGN KEY (id_user) REFERENCES users (id) ON UPDATE CASCADE ON DELETE CASCADE');

        $this->upgradeOK();
    }
}
