<?php

use yii\db\Migration;

class m160304_065108_product extends Migration
{
    public function up()
    {
        $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('{{%category_name}}', [
            'category_name_id' => $this->primaryKey(),
            'category_id' => $this->integer()->notNull(),
            'value' => $this->string(250),
        ], $tableOptions);

        $this->createTable('{{%category}}', [
            'category_id' => $this->primaryKey(),
            'parent_id' => $this->integer()->notNull()->defaultValue(0),
            'path' => 'INT[]',
            'depth' => $this->integer()->notNull()->defaultValue(0),
            'alias' => $this->string(250),
            'image' => $this->string(255),
            'meta_title' => $this->string(255),
            'meta_desc' => $this->text(),
            'meta_robots' => $this->string(50),
            'seo_text' => $this->text(),
            'category_name_id' => $this->integer(),
            'product_unit_id' => $this->integer()
        ], $tableOptions);

        $this->addForeignKey('category_name_fkey', 'category', 'category_name_id', 'category_name', 'category_name_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('category_name_category_fkey', 'category_name', 'category_id', 'category', 'category_id', 'NO ACTION', 'NO ACTION');

        $this->createTable('{{%product_category}}', [
            'product_id' => $this->integer()->notNull(),
            'category_id' => $this->integer()->notNull(),
        ], $tableOptions);

        $this->createTable('{{%brand_name}}', [
            'brand_name_id' => $this->primaryKey(),
            'brand_id' => $this->integer()->notNull(),
            'value' => $this->string(250),
        ], $tableOptions);

        $this->createTable('{{%brand}}', [
            'brand_id' => $this->primaryKey(),
            'brand_name_id' => $this->integer(),
            'alias' => $this->string(250),
            'image' => $this->string(255),
            'meta_title' => $this->string(255),
            'meta_desc' => $this->text(),
            'meta_robots' => $this->string(50),
            'seo_text' => $this->text(),
        ], $tableOptions);

        $this->addForeignKey('brand_name_fkey', 'brand', 'brand_name_id', 'brand_name', 'brand_name_id', 'CASCADE', 'CASCADE');
        $this->addForeignKey('brand_name_brand_fkey', 'brand_name', 'brand_id', 'brand', 'brand_id', 'NO ACTION', 'NO ACTION');

        $this->createTable('{{%product}}', [
            'product_id' => $this->primaryKey(),
            'name' => $this->string(255)->notNull(),
            'alias' => $this->string(255),
            'brand_id' => $this->integer(),
            'description' => $this->text(),
            'video' => $this->text(),
        ], $tableOptions);

        $this->addForeignKey('fki_product_id', 'product_category', 'product_id', 'product', 'product_id', 'NO ACTION', 'NO ACTION');
        $this->addForeignKey('fki_category_id', 'product_category', 'category_id', 'category', 'category_id', 'NO ACTION', 'NO ACTION');
        $this->addForeignKey('fki_brand_id', 'product', 'brand_id', 'brand', 'brand_id', 'NO ACTION', 'CASCADE');

        $this->createTable('{{%product_variant}}', [
            'product_variant_id' => $this->primaryKey(),
            'product_id' => $this->integer()->notNull(),
            'name' => $this->string(255)->notNull(),
            'sku' => $this->string(255)->notNull(),
            'price' => $this->float(),
            'price_old' => $this->float(),
            'stock' => $this->float(),
            'product_unit_id' => $this->integer()->notNull(),
        ], $tableOptions);

        $this->createTable('{{%product_unit}}', [
            'product_unit_id' => $this->primaryKey(),
            'name' => $this->string(255)->notNull(),
            'code' => $this->string(50)->notNull(),
            'is_default' => $this->boolean()
        ], $tableOptions);
//
        $this->addForeignKey('product_variant_product_unit_fkey', 'product_variant', 'product_unit_id', 'product_unit', 'product_unit_id', 'CASCADE', 'NO ACTION');
        $this->addForeignKey('category_product_unit_fkey', 'category', 'product_unit_id', 'product_unit', 'product_unit_id', 'NO ACTION', 'NO ACTION');
    }

    public function down()
    {
        $this->dropForeignKey('category_name_fkey', 'category');
        $this->dropForeignKey('category_name_category_fkey', 'category_name');
        $this->dropForeignKey('brand_name_fkey', 'brand');
        $this->dropForeignKey('brand_name_brand_fkey', 'brand_name');
        $this->dropForeignKey('fki_product_id', 'product_category');
        $this->dropForeignKey('fki_category_id', 'product_category');
        $this->dropForeignKey('fki_brand_id', 'product');
        $this->dropForeignKey('product_variant_product_unit_fkey', 'product_variant');
        $this->dropForeignKey('category_product_unit_fkey', 'category');
        $this->dropTable('{{%category}}');
        $this->dropTable('{{%category_name}}');
        $this->dropTable('{{%product_category}}');
        $this->dropTable('{{%product}}');
        $this->dropTable('{{%product_variant}}');
        $this->dropTable('{{%product_unit}}');
        $this->dropTable('{{%brand_name}}');
        $this->dropTable('{{%brand}}');
    }

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

    public function safeDown()
    {
    }
    */
}
