<?php

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

class CreateOfferTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('offer', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('property_id');
            $table->string('title',200);
            $table->mediumText('description')->nullable();
            $table->date('expire_date');
            $table->string('language',2);
            $table->string('currency',3);
            $table->double('total',10,2)->nullable();
            $table->integer('payment_type_id')->nullable(); // TODO İlişki Kurulmadı
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

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



        Schema::create('offer_accommodation_mapping', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('offer_id');
            $table->unsignedInteger('property_accommodation_id');
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->dateTime('created_at');
            $table->dateTime('updated_at');
        });

        Schema::table('offer_accommodation_mapping', function (Blueprint $table) {
            $table->foreign('offer_id')->references('id')->on('offer');
            $table->foreign('property_accommodation_id')->references('id')->on('property_fact');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
        });

        Schema::create('offer_cancellation_policy', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('offer_id');
            $table->date('start_date');
            $table->date('end_date');
            $table->integer('days_before');
            $table->string('type',3);
            $table->double('value',10,2);
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

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

        Schema::create('offer_contact_mapping', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('offer_id');
            $table->unsignedInteger('property_executive_id');
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::table('offer_contact_mapping', function (Blueprint $table) {
            $table->foreign('offer_id')->references('id')->on('offer');
            $table->foreign('property_executive_id')->references('id')->on('property_executive');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
        });

        Schema::create('offer_fact_mapping', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('offer_id');
            $table->unsignedInteger('category_id');
            $table->unsignedInteger('property_fact_parent_id');
            $table->unsignedInteger('property_fact_id');
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::table('offer_fact_mapping', function (Blueprint $table) {
            $table->foreign('offer_id')->references('id')->on('offer');
            $table->foreign('category_id')->references('id')->on('property_fact');
            $table->foreign('property_fact_parent_id')->references('parent_id')->on('property_fact');
            $table->foreign('property_fact_id')->references('id')->on('property_fact');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
        });

        Schema::create('offer_important_notes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('offer_id');
            $table->mediumText('note')->nullable();
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

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

        Schema::create('offer_payment_type', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name',200);
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

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

        Schema::create('offer_photo_mapping', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('offer_id');
            $table->unsignedInteger('property_photo_id');
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::table('offer_photo_mapping', function (Blueprint $table) {
            $table->foreign('offer_id')->references('id')->on('offer');
            $table->foreign('property_photo_id')->references('id')->on('property_photo');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
        });

        Schema::create('offer_price', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('offer_id');
            $table->date('date');
            $table->unsignedBigInteger('property_room_id');
            $table->unsignedInteger('property_accommodation_id');
            $table->unsignedInteger('number_of_rooms');
            $table->string('currency',3);
            $table->double('amount',10,2);
            $table->double('total_amount',10,2);
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::table('offer_price', function (Blueprint $table) {
            $table->foreign('offer_id')->references('id')->on('offer');
            $table->foreign('property_room_id')->references('id')->on('property_room');
            $table->foreign('property_accommodation_id')->references('id')->on('property_fact');
            $table->foreign('created_by')->references('id')->on('user');
            $table->foreign('updated_by')->references('id')->on('user');
        });

        Schema::create('offer_room_mapping', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('offer_id');
            $table->unsignedBigInteger('property_room_id');
            $table->tinyInteger('status')->default(1);
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');
        });

        Schema::table('offer_room_mapping', function (Blueprint $table) {
            $table->foreign('offer_id')->references('id')->on('offer');
            $table->foreign('property_room_id')->references('id')->on('property_room');
            $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('offer_room_mapping');
        Schema::dropIfExists('offer_price');
        Schema::dropIfExists('offer_photo_mapping');
        Schema::dropIfExists('offer_payment_type');
        Schema::dropIfExists('offer_important_notes');
        Schema::dropIfExists('offer_fact_mapping');
        Schema::dropIfExists('offer_contact_mapping');
        Schema::dropIfExists('offer_cancellation_policy');
        Schema::dropIfExists('offer_accommodation_mapping');
        Schema::dropIfExists('offer');

    }
}
