<?php

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

class CreatePlanningPricesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('planning_prices')) {
            Schema::create('planning_prices', function (Blueprint $table) {
                $table->increments('id');

                $table->string('number_of_apartments')->nullable();

                $table->double('min_space', 7, 2)->nullable();
                $table->double('max_space', 7, 2)->nullable();

                $table->double('price_per_squera_meter', 7, 2)->nullable();
                $table->double('price_per_squera_meter_max', 7, 2)->nullable();

                $table->decimal('min_price', 11, 2)->nullable();
                $table->decimal('max_price', 11, 2)->nullable();

                if (!Schema::hasColumn('planning_prices', 'residential_complex_id')) {
                    $table->integer('residential_complex_id')->unsigned()->nullable();
                    $table->foreign('residential_complex_id')
                        ->references('id')->on('residential_complexes')->onDelete('cascade');
                }
                if (!Schema::hasColumn('planning_prices', 'section_id')) {
                    $table->integer('section_id')->unsigned()->nullable();
                    $table->foreign('section_id')
                        ->references('id')->on('sections')->onDelete('cascade');
                }
                if (!Schema::hasColumn('planning_prices', 'room_count_id')) {
                    $table->integer('room_count_id')->unsigned()->nullable();
                    $table->foreign('room_count_id')
                        ->references('id')->on('room_counts')->onDelete('cascade');
                }
                if (!Schema::hasColumn('planning_prices', 'room_type_id')) {
                    $table->integer('room_type_id')->unsigned()->nullable();
                    $table->foreign('room_type_id')
                        ->references('id')->on('room_types')->onDelete('cascade');
                }

                $table->timestamps();
                $table->softDeletes();
                $table->index(['deleted_at']);
            });
        }
    }

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