<?php

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

class CreatePropertyPhotoTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('property_photo', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('property_id');
            $table->unsignedInteger('property_photo_category_id')->nullable();
            $table->string('photo_path');
            $table->string('photo_name',200);
            $table->double('photo_rank')->nullable();
            $table->string('file_size')->nullable();
            $table->string('file_ext')->nullable();
            $table->string('photo_resolution')->nullable();
            $table->integer('photo_order')->nullable();
            $table->tinyInteger('is_default')->default(0);
            $table->tinyInteger('is_temp')->default(1);
            $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_photo_category', function (Blueprint $table) {
            $table->increments('id');
            $table->string('category_name',50)->unique();
            $table->tinyInteger('status',false,true)->default(0);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::create('property_photo_category_locale', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('property_photo_category_id');
            $table->string('category_name');
            $table->string('locale',2);
            $table->tinyInteger('status',false,true)->default(0);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::create('photo_google_label', function (Blueprint $table) {
            $table->increments('id');
            $table->string('label', 50)->unique();
            $table->tinyInteger('status',false,true)->default(0);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::create('property_photo_category_label_mapping', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('property_photo_category_id');
            $table->unsignedInteger('photo_google_label_id');
            $table->tinyInteger('status',false,true)->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });


        /**
         * Table Relations
         */
        Schema::table('property_photo',function (Blueprint $table) {
            $table->foreign('property_id')->references('id')->on('property');
            $table->foreign('property_photo_category_id','pp_property_photo_category_id')->references('id')->on('property_photo_category');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');

        });

        /**
         * Table Relations
         */
        Schema::table('property_photo_category_label_mapping',function (Blueprint $table) {
            $table->foreign('property_photo_category_id','ppclm_property_photo_category_id')->references('id')->on('property_photo_category');
            $table->foreign('photo_google_label_id','ppclm_photo_google_label_id')->references('id')->on('photo_google_label');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
        });

        /**
         * Table Relations
         */
        Schema::table('property_photo_category',function (Blueprint $table) {
            $table->unique('category_name','pr_ph_ctg_name');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
        });

        /**
         * Table Relations
         */
        Schema::table('property_photo_category_locale',function (Blueprint $table) {
            $table->foreign('property_photo_category_id','ppcl_property_photo_category_id')->references('id')->on('property_photo_category');
            $table->unique(['property_photo_category_id', 'locale'],'ppcl_category_id_locale');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('property_photo');
        Schema::dropIfExists('property_photo_category_label_mapping');
        Schema::dropIfExists('property_photo_category_locale');
        Schema::dropIfExists('property_photo_category');
        Schema::dropIfExists('photo_google_label');
    }
}
