<?php

use yii\db\Schema;
use yii\db\Migration;

class m160126_071717_rubrication extends Migration
{
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // Only for MySQL
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';

            // @todo https://habrahabr.ru/post/138947/
        } elseif ($this->db->driverName === 'pgsql') {
            // Only for PostgreSQL
            // @todo use intarray field for tax_options
        }
        $this->createTable('{{%tax_group}}', [
            'tax_group_id' => $this->primaryKey(),
            'alias' => $this->string(50)->notNull(),
            'name' => $this->string(255)->notNull(),
            'description' => $this->text(),
            'module' => $this->string(50)->notNull(),
            'hierarchical' => $this->boolean()->notNull()->defaultValue(false),
            'settings' => $this->text()
        ], $tableOptions);

        $this->createTable('{{%tax_option}}', [
            'tax_option_id' => $this->bigPrimaryKey(),
            'tax_group_id' => $this->integer()->notNull(),
            'parent_id' => $this->integer()->notNull()->defaultValue(0),
            'path'  => $this->string(),
            'depth' => $this->integer(),
            'sort' => $this->integer()->notNull()->defaultValue(0),
            'default_value' => $this->integer(),
            'alias' => $this->string(50)->notNull()->defaultValue(''),
        ], $tableOptions);
        $this->addForeignKey('fki_tax_option_tax_group_id', 'tax_option', 'tax_group_id', 'tax_group', 'tax_group_id', 'CASCADE', 'CASCADE');

        $this->createTable('{{%tax_group_to_group}}', [
            'tax_group1_id' => $this->integer()->notNull(),
            'tax_group2_id' => $this->integer()->notNull(),
            'alias' => $this->string(50)->notNull(),
            'sort' => $this->integer(),
        ], $tableOptions);
        $this->addForeignKey('fki_tax_group_to_group1', 'tax_group_to_group', 'tax_group1_id', 'tax_group', 'tax_group_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('fki_tax_group_to_group2', 'tax_group_to_group', 'tax_group2_id', 'tax_group', 'tax_group_id', 'CASCADE', 'CASCADE');
        $this->addPrimaryKey('pki_tax_group_to_group', 'tax_group_to_group', ['tax_group1_id', 'tax_group2_id', 'alias']);

        $this->createTable('{{%tax_option_to_group}}', [
            'tax_option_id' => $this->integer()->notNull(),
            'tax_group_id' => $this->integer()->notNull(),
            'alias' => $this->string(50)->notNull(),
            'sort' => $this->integer(),
        ], $tableOptions);
        $this->addForeignKey('tax_option_to_group_option_id', 'tax_option_to_group', 'tax_option_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('tax_option_to_group_group_id', 'tax_option_to_group', 'tax_group_id', 'tax_group', 'tax_group_id', 'CASCADE', 'CASCADE');
        $this->addPrimaryKey('pki_tax_option_to_group', 'tax_option_to_group', ['tax_option_id', 'tax_group_id', 'alias']);

        $this->createTable('{{%tax_option_to_option}}', [
            'tax_option1_id' => $this->integer()->notNull(),
            'tax_option2_id' => $this->integer()->notNull(),
            'alias' => $this->string(50)->notNull(),
            'sort' => $this->integer(),
        ], $tableOptions);
        $this->addForeignKey('fki_tax_option_to_option1', 'tax_option_to_option', 'tax_option1_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('fki_tax_option_to_option2', 'tax_option_to_option', 'tax_option2_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE');
        $this->addPrimaryKey('pki_tax_option_to_option', 'tax_option_to_option', ['tax_option1_id', 'tax_option2_id', 'alias']);

        /*$this->createTable('{{%tax_entity_relation}}', [
            'tax_option_id' => $this->integer()->notNull(),
            'entity_id' => $this->integer()->notNull(),
            'entity_table_name' => $this->string(50)->notNull(),
            'entity_key_name' => $this->string(50)->notNull(),
            'alias' => $this->string(50)->notNull(),
        ], $tableOptions);
        $this->addForeignKey('fki_tax_entity_relation_option_id', 'tax_entity_relation', 'tax_option_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE');
        $this->addPrimaryKey('pki_tax_entity_relation', 'tax_entity_relation', ['tax_option_id', 'entity_id', 'entity_table_name', 'entity_key_name', 'alias']);*/

        $this->createTable('{{%tax_value_int}}', [
            'tax_value_id' => $this->primaryKey(),
            'tax_option_id' => $this->integer()->notNull(),
            'value' => $this->integer(),
        ], $tableOptions);
        $this->addForeignKey('fki_tax_value_int_option_id', 'tax_value_int', 'tax_option_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE');

        $this->createTable('{{%tax_value_string}}', [
            'tax_value_id' => $this->primaryKey(),
            'tax_option_id' => $this->integer()->notNull(),
            'value' => $this->string(),
        ], $tableOptions);
        $this->addForeignKey('fki_tax_value_string_option_id', 'tax_value_string', 'tax_option_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE');

        $this->createTable('{{%tax_value_text}}', [
            'tax_value_id' => $this->primaryKey(),
            'tax_option_id' => $this->integer()->notNull(),
            'value' => $this->text(),
        ], $tableOptions);
        $this->addForeignKey('fki_tax_value_text_option_id', 'tax_value_text', 'tax_option_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE');

        $this->createTable('{{%tax_value_float}}', [
            'tax_value_id' => $this->bigPrimaryKey(),
            'tax_option_id' => $this->integer()->notNull(),
            'value' => $this->float(),
        ], $tableOptions);
        $this->addForeignKey('fki_tax_value_float_option_id', 'tax_value_float', 'tax_option_id', 'tax_option', 'tax_option_id', 'CASCADE', 'CASCADE');
    }

    public function safeDown()
    {
        $this->dropTable('{{%tax_value_int}}');
        $this->dropTable('{{%tax_value_string}}');
        $this->dropTable('{{%tax_value_text}}');
        $this->dropTable('{{%tax_value_float}}');
//        $this->dropTable('{{%tax_entity_relation}}');
        $this->dropTable('{{%tax_group_to_group}}');
        $this->dropTable('{{%tax_option_to_group}}');
        $this->dropTable('{{%tax_option_to_option}}');
        $this->dropTable('{{%tax_option}}');
        $this->dropTable('{{%tax_group}}');
    }

    /*
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}
