<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;


class CreateTaggableTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('taggable_tags', function (Blueprint $table) {
            $table->increments('tag_id');
            $table->string('name');
            $table->string('slug')->nullable();
            $table->string('normalized');
            $table->timestamps();

            $table->index('normalized');
        });

        Schema::create('taggable_taggables', function (Blueprint $table) {
            $table->unsignedInteger('tag_id');
            $table->unsignedInteger('taggable_id');
            $table->string('taggable_type');
            $table->timestamps();

            $table->unique(['tag_id', 'taggable_id', 'taggable_type']);

            $table->index(['tag_id', 'taggable_id'], 'i_taggable_fwd');
            $table->index(['taggable_id', 'tag_id'], 'i_taggable_rev');
            $table->index('taggable_type', 'i_taggable_type');
        });

        Schema::create('tag_descriptions', function (Blueprint $table) {
            $table->increments('id');
            $table->char('lang', 10)->index();
            $table->integer('tag_id');
            $table->string('name');
            $table->text('description')->nullable();

            $table->index(['lang', 'tag_id'], 'idx_lang_tag');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('taggable_tags');
        Schema::drop('taggable_taggables');
        Schema::drop('tag_descriptions');
    }
}
