<?php

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

class CreatePermissionTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('permission', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',128);
            $table->string('code',128)->unique();
            $table->unsignedInteger('parent_id')->nullable();
            $table->tinyInteger('is_menu')->nullable()->default(0);
            $table->string('menu_detail', 255)->nullable();
            $table->tinyInteger('menu_order')->nullable()->default(0);
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

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

        });

        Schema::create('permission_group', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',128);
            $table->tinyInteger('is_admin')->nullable()->default(0);
            $table->string('parameter_rules', 255)->nullable();
            $table->tinyInteger('is_private_permission')->nullable();
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::table('permission_group',function (Blueprint $table) {

            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');

        });

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

        Schema::table('permission_group_mapping',function (Blueprint $table) {
            $table->foreign('permission_group_id')->references('id')->on('permission_group');
            $table->foreign('permission_id')->references('id')->on('permission');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
            $table->unique(['permission_group_id', 'permission_id'], 'pgm_unique');
        });

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

        Schema::table('permission_group_user_mapping',function (Blueprint $table) {
            $table->foreign('permission_group_id')->references('id')->on('permission_group');
            $table->foreign('property_id')->references('id')->on('property');
            $table->foreign('user_id')->references('id')->on('user');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
            $table->unique(['permission_group_id', 'property_id', 'user_id'], 'pgu_unique');

        });

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

        Schema::table('permission_locale', function (Blueprint $table) {
            $table->foreign('permission_id')->references('id')->on('permission');
            $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(['permission_id', 'locale'],'language_unique');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('permission_group_user_mapping');
        Schema::dropIfExists('permission_group_mapping');
        Schema::dropIfExists('permission_group');
        Schema::dropIfExists('permission_locale');
        Schema::dropIfExists('permission');
    }
}
