<?php

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

class CreatePropertyUnitTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('property_unit', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',100);
            $table->string('short_name',45);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::create('property_unit_locale', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('unit_id');
            $table->string('name');
            $table->string('locale',2);
            $table->tinyInteger('status');
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        /**
         * Table Relation
         */
        Schema::table('property_unit_locale',function (Blueprint $table) {
            $table->foreign('unit_id')->references('id')->on('property_unit');
            $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(['unit_id', 'locale'],'pul_language_unique');
        });
    }

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