<?php

namespace External\VarioBundle\Resources\upgrade;

class MappingTablesUpgrade extends \UpgradeNew
{
    protected $priority = 1200;

    private $tables = [
        'vario_products' => [
            'id_column' => 'id_product',
            'references' => 'products(id)',
            'unsigned' => false,
        ],
        'vario_sections' => [
            'id_column' => 'id_section',
            'references' => 'sections(id)',
            'unsigned' => false,
        ],
        'vario_variations' => [
            'id_column' => 'id_variation',
            'references' => 'products_variations(id)',
            'unsigned' => false,
        ],
        'vario_stores' => [
            'id_column' => 'id_store',
            'references' => 'stores(id)',
            'unsigned' => false,
        ],
        'vario_pricelists' => [
            'id_column' => 'id_pricelist',
            'references' => 'pricelists(id)',
            'unsigned' => false,
        ],
        'vario_orders' => [
            'id_column' => 'id_order',
            'references' => 'orders(id)',
            'unsigned' => true,
        ],
    ];

    public function check_mappingTablesExists()
    {
        return $this->checkTableExists('vario_products');
    }

    /** Vario: create mapping tables */
    public function upgrade_mappingTablesExists()
    {
        foreach ($this->tables as $table => $options) {
            $unsigned = '';
            if ($options['unsigned']) {
                $unsigned = 'UNSIGNED';
            }

            sqlQuery("CREATE TABLE {$table} (
                  id_vario VARCHAR(100) PRIMARY KEY,
                  {$options['id_column']} INT(11) {$unsigned} NOT NULL,
                  FOREIGN KEY ({$options['id_column']}) REFERENCES {$options['references']}
                  ON DELETE CASCADE ON UPDATE CASCADE
                );");
        }

        $this->upgradeOK();
    }

    public function check_PaidSentColumn()
    {
        return $this->checkColumnExists('vario_orders', 'paid_sent');
    }

    /** Add column vario_orders.paid_sent */
    public function upgrade_PaidSentColumn()
    {
        sqlQuery('ALTER TABLE vario_orders ADD COLUMN paid_sent TINYINT DEFAULT NULL');

        $this->upgradeOK();
    }

    public function check_VarioOrdersDataColumn()
    {
        return $this->checkColumnExists('vario_orders', 'data');
    }

    /** Add column vario_orders.data */
    public function upgrade_VarioOrdersDataColumn()
    {
        sqlQuery('ALTER TABLE vario_orders ADD COLUMN data MEDIUMTEXT DEFAULT NULL');

        $this->upgradeOK();
    }

    public function check_PrimaryKeyInVarioVariations()
    {
        return !$this->checkIndexNameExists('vario_variations', 'PRIMARY');
    }

    /** Drop primary key in vario_variations */
    public function upgrade_PrimaryKeyInVarioVariations()
    {
        sqlQuery('ALTER TABLE vario_variations DROP PRIMARY KEY ');

        $this->upgradeOK();
    }

    public function check_VarioDocumentsTable()
    {
        return $this->checkTableExists('vario_documents');
    }

    /** Add vario_documents table */
    public function upgrade_VarioDocumentsTable()
    {
        sqlQuery('CREATE TABLE vario_documents (
                  id_vario VARCHAR(100) PRIMARY KEY,
                  status VARCHAR(100) DEFAULT NULL
                );');

        $this->upgradeOK();
    }

    public function check_VarioProductsForeignKey()
    {
        return $this->checkForeignKeyExists('vario_products', 'id_product');
    }

    /** Add foreign key to 'vario_products' */
    public function upgrade_VarioProductsForeignKey()
    {
        sqlQuery('ALTER TABLE vario_products ADD CONSTRAINT vario_products_ibfk1 FOREIGN KEY (id_product) REFERENCES products(id) ON DELETE CASCADE ON UPDATE CASCADE');

        $this->upgradeOK();
    }

    public function check_VarioSectionsParent()
    {
        return $this->checkColumnExists('vario_sections', 'parent');
    }

    /** Add 'parent' column into 'vario_sections' */
    public function upgrade_VarioSectionsParent()
    {
        sqlQuery('ALTER TABLE vario_sections ADD COLUMN parent VARCHAR(100) DEFAULT NULL');

        $this->upgradeOK();
    }

    public function check_VarioSectionsOrder()
    {
        return $this->checkColumnExists('vario_sections', 'position');
    }

    /** Add 'position' column into 'vario_sections' */
    public function upgrade_VarioSectionsOrder()
    {
        sqlQuery('ALTER TABLE vario_sections ADD COLUMN position INT(11) DEFAULT 0');

        $this->upgradeOK();
    }

    public function check_VarioStoresDisabledColumn()
    {
        return $this->checkColumnExists('vario_stores', 'disabled');
    }

    /** add 'disabled' column into 'vario_stores' table */
    public function upgrade_VarioStoresDisabledColumn()
    {
        sqlQuery('ALTER TABLE vario_stores ADD COLUMN disabled TINYINT DEFAULT 0');

        $this->upgradeOK();
    }

    public function check_VarioDelayedUpdate(): bool
    {
        return $this->checkTableExists('vario_delayed_update');
    }

    /** Create table vario_delayed_update */
    public function upgrade_VarioDelayedUpdate(): void
    {
        sqlQuery(
            'CREATE TABLE vario_delayed_update (
                  id_vario VARCHAR(100),
                  type VARCHAR(25) NOT NULL
                );'
        );

        $this->upgradeOK();
    }
}
