<?php

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

class CreatePropertyRoomPhotoMappingTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        DB::connection()->statement("ALTER TABLE `offer_photo_mapping`DROP FOREIGN KEY `offer_photo_mapping_property_photo_id_foreign`;");
        DB::connection()->statement("ALTER TABLE `property_photo` MODIFY COLUMN `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT FIRST");
        DB::connection()->statement("ALTER TABLE `offer_photo_mapping` MODIFY COLUMN `property_photo_id` bigint(20) UNSIGNED NOT NULL");
        DB::connection()->statement("ALTER TABLE `offer_photo_mapping` ADD CONSTRAINT `offer_photo_mapping_property_photo_id_foreign` FOREIGN KEY (`property_photo_id`) REFERENCES `property_photo` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT");

        Schema::create('property_room_photo_mapping', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('property_id')->unsigned();
            $table->bigInteger('room_id')->unsigned();
            $table->bigInteger('photo_id')->unsigned();
            $table->unsignedInteger('created_by');
            $table->unsignedInteger('updated_by');
            $table->integer('created_at');
            $table->integer('updated_at');

            $table->foreign('property_id')->references('id')->on('property');
            $table->foreign('room_id')->references('id')->on('property_room');
            $table->foreign('photo_id')->references('id')->on('property_photo');
            $table->unique(['property_id', 'photo_id', 'room_id'],'ppr_unique');
        });
    }

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