<?php

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

class CreatePropertyFactAttributeTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('property_fact_attribute', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name','250');
            $table->integer('unit_id',false,true)->default(0);
            $table->tinyInteger('type')->default(0);
            $table->tinyInteger('order_number')->default(0);
            $table->tinyInteger('status',false,true)->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });


        Schema::create('property_fact_attribute_locale', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('attribute_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 Relations
         */

        Schema::table('property_fact_attribute',function (Blueprint $table) {
            $table->foreign('unit_id')->references('id')->on('property_unit');
        });

        Schema::table('property_fact_attribute_locale',function (Blueprint $table) {
            $table->foreign('attribute_id')->references('id')->on('property_fact_attribute');
            $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(['attribute_id', 'locale'],'pfal_language_unique');
        });
    }

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