<?php

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

class CreateLanguageTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('language', function (Blueprint $table) {
            $table->increments('id');
            $table->string('code', 2);
            $table->string('name', 45);
            $table->tinyInteger('status')->default(1)->index();
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
       });

        Schema::table('language', function (Blueprint $table) {
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
            $table->unique('code');
        });


        Schema::create('language_locale', function (Blueprint $table) {
            $table->increments('id');
            $table->string('language_code',2);
            $table->string('name', 45);
            $table->string('locale',2);
            $table->tinyInteger('status')->default(1)->index();
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');

        });

        Schema::table('language_locale', function (Blueprint $table) {
            $table->foreign('language_code')->references('code')->on('language');
            $table->foreign('locale')->references('code')->on('language');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
            $table->unique(['language_code','locale']);
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('language_locale');
        Schema::dropIfExists('language');
    }
}
