<?php

namespace KupShop\ContentBundle\Resources\upgrade;

class ParametersUpgrade extends \UpgradeNew
{
    public function check_trigger_parameters()
    {
        return $this->checkIfTriggerExists('trigger_parameters');
    }

    /** Add trigger (incrementing position) to parameters  */
    public function upgrade_trigger_parameters()
    {
        sqlQuery('CREATE TRIGGER trigger_parameters BEFORE INSERT ON parameters
                    FOR EACH ROW
                    BEGIN
                    IF NEW.position IS NULL THEN
                        SET NEW.position = (SELECT COALESCE(MAX(position) + 1, 0) FROM parameters);
                    END IF;
                 END;');
        $this->upgradeOK();
    }

    public function check_trigger_parametersList()
    {
        return $this->checkIfTriggerExists('trigger_parameters_list');
    }

    /** Add trigger (incrementing position) to parameters_list  */
    public function upgrade_trigger_parametersList()
    {
        sqlQuery('CREATE TRIGGER trigger_parameters_list BEFORE INSERT ON parameters_list
                    FOR EACH ROW
                    BEGIN
                    IF NEW.position IS NULL THEN
                        SET NEW.position = (SELECT COALESCE(MAX(position) + 1, 0) FROM parameters_list);
                    END IF;
                 END;');
        $this->upgradeOK();
    }

    public function check_ValueMeaningColumn()
    {
        return $this->checkColumnExists('parameters', 'value_meaning');
    }

    /** Add column value_meaning */
    public function upgrade_ValueMeaningColumn()
    {
        sqlQuery('ALTER TABLE parameters ADD COLUMN value_meaning ENUM("text", "color", "image") DEFAULT "text" AFTER value_type');

        $this->upgradeOK();
    }

    public function check_ValueMeaningValues()
    {
        return $this->checkEnumOptions('parameters', 'value_meaning', ['text', 'color', 'image', 'progress']);
    }

    /** Update Parameter.value_meaning enum options */
    public function upgrade_ValueMeaningValues()
    {
        $this->updateEnumOptions('parameters', 'value_meaning', ['text', 'color', 'image', 'progress']);

        $this->upgradeOK();
    }

    public function check_ParametersFigureColumn()
    {
        return $this->checkColumnExists('parameters', 'figure');
    }

    /** Add 'figure' column into parameters table */
    public function upgrade_ParametersFigureColumn()
    {
        sqlQuery('ALTER TABLE parameters ADD COLUMN figure ENUM("Y", "N") DEFAULT "Y"');

        $this->upgradeOK();
    }

    public function check_ParametersCatalogFigureColumn()
    {
        return findModule(\Modules::COMPONENTS) && $this->checkColumnExists('parameters', 'catalog_figure');
    }

    /** Add 'catalog_figure' column into parameters table */
    public function upgrade_ParametersCatalogFigureColumn()
    {
        sqlQuery('ALTER TABLE parameters ADD COLUMN catalog_figure ENUM("Y", "N") DEFAULT "N"');

        $this->upgradeOK();
    }

    public function check_switch_parameters_list_description_value()
    {
        return $this->checkDataMigration(14);
    }

    /** Switch value and description for meaning photo and type list */
    public function upgrade_switch_parameters_list_description_value()
    {
        sqlQuery('update parameters_list set description=value
                            where description="" and id_parameter in 
                                               (select id from parameters where value_meaning="image" and value_type = "list");');

        sqlQuery('update parameters_list set value=description
                            where value="" and id_parameter in 
                                               (select id from parameters where value_meaning="image" and value_type = "list");');

        sqlQuery('update parameters_list set description=value, value=@temp
                            where (@temp:=description) is not null 
                              and id_parameter in 
                                  (select id from parameters where value_meaning="image" and value_type = "list");');

        $this->commitDataMigration(14);
        $this->upgradeOK();
    }

    public function check_ParametersSectionsRequiredColumn(): bool
    {
        return $this->checkColumnExists('parameters_sections', 'required');
    }

    /** Add parameters_sections.enabled and parameters_sections.required columns */
    public function upgrade_ParametersSectionsRequiredColumn(): void
    {
        sqlQuery('ALTER TABLE parameters_sections ADD COLUMN filter ENUM("Y", "N") DEFAULT "Y" AFTER id_section');
        sqlQuery('ALTER TABLE parameters_sections ADD COLUMN required ENUM("Y", "N") DEFAULT "Y" AFTER filter');

        $this->upgradeOK();
    }

    public function check_ParametersProductsAddIndex_id_product(): bool
    {
        return $this->checkIndexNameExists('parameters_products', 'id_product');
    }

    /** Add parameters_products index for id_product when missing */
    public function upgrade_ParametersProductsAddIndex_id_product(): void
    {
        sqlQuery('create index id_product on parameters_products (id_product);');
        $this->upgradeOK();
    }

    public function check_ParametersProductsRemoveCompoundIndex_id_product_id_parameter(): bool
    {
        return !$this->checkIndexNameExists('parameters_products', 'parameters_products_id_product_id_parameter_index');
    }

    /** Remove parameters_products old compound index (id_product, id_parameter), replace it with (id_parameter, id_product)  */
    public function upgrade_ParametersProductsRemoveCompoundIndex_id_product_id_parameter(): void
    {
        sqlQuery('drop index parameters_products_id_product_id_parameter_index on parameters_products;');
        $this->upgradeOK();
    }

    public function check_ParametersProductsCompoundIndex_id_parameter_id_product(): bool
    {
        return $this->checkIndexNameExists('parameters_products', 'id_parameter_id_product_index');
    }

    /** Add parameters_products compound index (id_parameter, id_product)  */
    public function upgrade_ParametersProductsCompoundIndex_id_parameter_id_product(): void
    {
        sqlQuery('create index id_parameter_id_product_index on parameters_products (id_parameter, id_product);');

        $this->upgradeOK();
    }

    public function check_ParametersProductsCompoundIndex_value_list_id_product(): bool
    {
        return $this->checkIndexNameExists('parameters_products', 'value_list_id_product_index');
    }

    /** Add parameters_products compound index (id_parameter, id_product)  */
    public function upgrade_ParametersProductsCompoundIndex_value_list_id_product(): void
    {
        sqlQuery('create index value_list_id_product_index on parameters_products (value_list, id_product);');

        $this->upgradeOK();
    }

    public function check_ParametersAdminNameColumnDelete(): bool
    {
        return !$this->checkColumnExists('parameters', 'name_admin');
    }

    public function upgrade_ParametersAdminNameColumnDelete(): void
    {
        sqlQuery('ALTER TABLE parameters DROP COLUMN name_admin;');

        $this->upgradeOK();
    }

    public function check_ParametersNameFrontendColumn(): bool
    {
        return $this->checkColumnExists('parameters', 'name_frontend');
    }

    public function upgrade_ParametersNameFrontendColumn(): void
    {
        sqlQuery('ALTER TABLE parameters ADD COLUMN name_frontend VARCHAR(255) AFTER name;');

        $this->upgradeOK();
    }

    public function check_ParametersNameFrontendTranslationColumn(): bool
    {
        return findModule(\Modules::TRANSLATIONS) && $this->checkColumnExists('parameters_translations', 'name_frontend');
    }

    public function upgrade_ParametersNameFrontendTranslationColumn(): void
    {
        sqlQuery('ALTER TABLE parameters_translations ADD COLUMN name_frontend VARCHAR(255) AFTER name;');

        $this->upgradeOK();
    }
}
