<?php

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

class CreatePropertyModuleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('property_module', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',200);
            $table->tinyInteger('point')->nullable();
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

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


        Schema::create('property_module_mapping', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('property_module_id');
            $table->unsignedInteger('property_id');
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::table('property_module_mapping',function (Blueprint $table) {
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
            $table->foreign('property_id')->references('id')->on('property');
            $table->foreign('property_module_id')->references('id')->on('property_module');

            $table->unique(['property_module_id','property_id'], 'mapping_unique');
        });
    }

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