<?php

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

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

        DB::statement("DROP TABLE IF EXISTS `property_promotion_mapping`;");
        DB::statement("DROP TABLE IF EXISTS `property_promotion`;");
        DB::statement("DROP TABLE IF EXISTS `promotion_type`;");


        DB::statement("
                CREATE TABLE `promotion_type`  (
                  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                  `name` varchar(100) NOT NULL,
                  `language_key` varchar(100) NULL DEFAULT NULL,
                  `parent_id` int(10) UNSIGNED NULL DEFAULT NULL,
                  `type_code` varchar(10) NOT NULL,
                  `order_number` tinyint(4) NOT NULL DEFAULT 0,
                  `status` tinyint(3) UNSIGNED NOT NULL DEFAULT 1,
                  `created_by` int(10) UNSIGNED NOT NULL,
                  `updated_by` int(10) UNSIGNED NOT NULL,
                  `created_at` int(11) NOT NULL,
                  `updated_at` int(11) NOT NULL,
                  PRIMARY KEY (`id`) USING BTREE,
                  INDEX `created_by`(`created_by`) USING BTREE,
                  INDEX `updated_by`(`updated_by`) USING BTREE,
                  CONSTRAINT `promotion_type_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
                  CONSTRAINT `promotion_type_ibfk_2` FOREIGN KEY (`updated_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
                ) ;

        ") ;


        DB::statement("

                CREATE TABLE `property_promotion`  (
              `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
              `property_id` int(10) UNSIGNED NOT NULL,
              `promotion_type_id` int(10) UNSIGNED NOT NULL,
              `start_date` date NULL DEFAULT NULL,
              `end_date` date NULL DEFAULT NULL,
              `reservation_start_date` date NULL DEFAULT NULL,
              `reservation_end_date` date NULL DEFAULT NULL,
              `day_before` int(10) NULL DEFAULT NULL,
              `amount` double(10, 2) NULL DEFAULT NULL,
              `min_stay` int(10) NULL DEFAULT NULL,
              `days` varchar(100) NULL DEFAULT NULL,
              `params` mediumtext NULL,
              `detail` mediumtext NULL,
              `status` tinyint(3) UNSIGNED NOT NULL DEFAULT 1,
              `created_by` int(10) UNSIGNED NOT NULL,
              `updated_by` int(10) UNSIGNED NOT NULL,
              `created_at` int(11) NOT NULL,
              `updated_at` int(11) NOT NULL,
              PRIMARY KEY (`id`) USING BTREE,
              INDEX `created_by`(`created_by`) USING BTREE,
              INDEX `updated_by`(`updated_by`) USING BTREE,
              INDEX `property_id`(`property_id`) USING BTREE,
              INDEX `promotion_type_id`(`promotion_type_id`) USING BTREE,
              CONSTRAINT `property_promotion_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_promotion_ibfk_2` FOREIGN KEY (`updated_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_promotion_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `property` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_promotion_ibfk_4` FOREIGN KEY (`promotion_type_id`) REFERENCES `promotion_type` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
            );

        ");

                    DB::statement("
                    CREATE TABLE `property_promotion_mapping`  (
                      `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                      `property_id` int(10) UNSIGNED NOT NULL,
                      `room_rate_channel_mapping_id` bigint(20) UNSIGNED NOT NULL,
                      `property_promotion_id` int(10) UNSIGNED NOT NULL,
                      `status` tinyint(3) UNSIGNED NOT NULL DEFAULT 1,
                      `created_by` int(10) UNSIGNED NOT NULL,
                      `updated_by` int(10) UNSIGNED NOT NULL,
                      `created_at` int(11) NOT NULL,
                      `updated_at` int(11) NOT NULL,
                      PRIMARY KEY (`id`) USING BTREE,
                      UNIQUE INDEX `property_channel_room_rate_promotion_unique`(`property_id`, `property_promotion_id`, `room_rate_channel_mapping_id`) USING BTREE,
                      INDEX `created_by`(`created_by`) USING BTREE,
                      INDEX `updated_by`(`updated_by`) USING BTREE,
                      INDEX `property_id`(`property_id`) USING BTREE,
                      INDEX `property_promotion_id`(`property_promotion_id`) USING BTREE,
                      INDEX `room_rate_channel_mapping_id`(`room_rate_channel_mapping_id`) USING BTREE,
                      CONSTRAINT `property_promotion_mapping_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
                      CONSTRAINT `property_promotion_mapping_ibfk_2` FOREIGN KEY (`updated_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
                      CONSTRAINT `property_promotion_mapping_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `property` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
                      CONSTRAINT `property_promotion_mapping_ibfk_4` FOREIGN KEY (`property_promotion_id`) REFERENCES `property_promotion` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
                      CONSTRAINT `property_promotion_mapping_ibfk_5` FOREIGN KEY (`room_rate_channel_mapping_id`) REFERENCES `property_room_rate_channel_mapping` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
                    ) ;
        ") ;
                    DB::statement("INSERT INTO `promotion_type` VALUES (1, 'Early Booking', NULL, NULL, 'ERB', 0, 1, 1, 1, 1, 1),(2, 'Period', NULL, 1, 'PRD', 0, 1, 1, 1, 1, 1),(3, 'Before Day', NULL, 1, 'BFD', 0, 1, 1, 1, 1, 1);") ;

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}
