<?php

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

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

        DB::statement("SET FOREIGN_KEY_CHECKS = 0;");
        DB::statement("DROP TABLE IF EXISTS `property_place_category_field_value`;");
        DB::statement("DROP TABLE IF EXISTS `place_category_field_option_field_mapping`;");
        DB::statement("DROP TABLE IF EXISTS `place_category_field_option`;");
        DB::statement("DROP TABLE IF EXISTS `place_category_field_mapping`;");
        DB::statement("DROP TABLE IF EXISTS `place_category_field`;");

        DB::statement("
            CREATE TABLE `place_category_field`  (
               `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
               `name` varchar(255)  NOT NULL,
               `label` varchar(255)  NOT NULL,
               `language_key` varchar(255)  NOT NULL,
               `element` varchar(128)  NOT NULL,
               `type` varchar(128)  NULL DEFAULT NULL,
               `field_attribute` tinytext  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,
               CONSTRAINT `place_category_field_ibfk_2` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
               CONSTRAINT `place_category_field_ibfk_3` FOREIGN KEY (`updated_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
            ) ;

        ");
        DB::statement("
            CREATE TABLE `place_category_field_mapping`  (
              `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              `property_place_category_id` int(20) UNSIGNED NOT NULL,
              `property_place_category_field_id` bigint(20) UNSIGNED NOT NULL,
              `order_number` int(11) 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 `property_place_category_id`(`property_place_category_id`) USING BTREE,
              INDEX `property_place_field_id`(`property_place_category_field_id`) USING BTREE,
              UNIQUE INDEX `field_category_unique`(`property_place_category_id`, `property_place_category_field_id`) USING BTREE,
              INDEX `created_by`(`created_by`) USING BTREE,
              INDEX `updated_by`(`updated_by`) USING BTREE,
              CONSTRAINT `property_place_category_field_mapping_ibfk_1` FOREIGN KEY (`property_place_category_id`) REFERENCES `property_place_category` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_mapping_ibfk_2` FOREIGN KEY (`property_place_category_field_id`) REFERENCES `place_category_field` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_mapping_ibfk_3` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_mapping_ibfk_4` FOREIGN KEY (`updated_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
            );

        ");
        DB::statement("
            CREATE TABLE `place_category_field_option`  (
              `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              `name` varchar(255)  NULL DEFAULT NULL,
              `language_key` varchar(255)  NULL DEFAULT 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,
              CONSTRAINT `place_category_field_option_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `place_category_field_option_ibfk_2` FOREIGN KEY (`updated_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
            ) ;
        ");
        DB::statement("
        CREATE TABLE `place_category_field_option_field_mapping`  (
              `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              `field_id` bigint(20) UNSIGNED NOT NULL,
              `field_option_id` bigint(20) UNSIGNED NOT NULL,
              `order_number` int(11) 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 `field_id_2`(`field_id`, `field_option_id`) USING BTREE,
              INDEX `field_id`(`field_id`) USING BTREE,
              INDEX `field_option_id`(`field_option_id`) USING BTREE,
              INDEX `created_by`(`created_by`) USING BTREE,
              INDEX `updated_by`(`updated_by`) USING BTREE,
              CONSTRAINT `place_category_field_option_field_mapping_ibfk_1` FOREIGN KEY (`field_id`) REFERENCES `place_category_field` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `place_category_field_option_field_mapping_ibfk_2` FOREIGN KEY (`field_option_id`) REFERENCES `place_category_field_option` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `place_category_field_option_field_mapping_ibfk_3` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `place_category_field_option_field_mapping_ibfk_4` FOREIGN KEY (`updated_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
        ) ;

        ");
        DB::statement("
            CREATE TABLE `property_place_category_field_value`  (
              `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
              `property_id` int(10) UNSIGNED NOT NULL,
              `place_id` bigint(20) UNSIGNED NOT NULL,
              `property_place_category_id` int(10) UNSIGNED NOT NULL,
              `place_category_field_id` bigint(20) UNSIGNED NOT NULL,
              `input_value` varchar(255) NULL DEFAULT NULL,
              `option_value` bigint(20) UNSIGNED NULL DEFAULT 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 `property_id`(`property_id`) USING BTREE,
              INDEX `property_place_category_id`(`property_place_category_id`) USING BTREE,
              INDEX `place_category_field_id`(`place_category_field_id`) USING BTREE,
              INDEX `option_value`(`option_value`) USING BTREE,
              INDEX `created_by`(`created_by`) USING BTREE,
              INDEX `updated_by`(`updated_by`) USING BTREE,
              INDEX `place_id`(`place_id`) USING BTREE,
              CONSTRAINT `property_place_category_field_value_ibfk_1` FOREIGN KEY (`property_id`) REFERENCES `property` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_value_ibfk_2` FOREIGN KEY (`property_place_category_id`) REFERENCES `property_place_category` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_value_ibfk_3` FOREIGN KEY (`place_category_field_id`) REFERENCES `place_category_field` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_value_ibfk_4` FOREIGN KEY (`option_value`) REFERENCES `place_category_field_option` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_value_ibfk_5` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_value_ibfk_6` FOREIGN KEY (`updated_by`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
              CONSTRAINT `property_place_category_field_value_ibfk_7` FOREIGN KEY (`place_id`) REFERENCES `property_place` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
            ) ;
        ");
        DB::statement("SET FOREIGN_KEY_CHECKS = 1;");


    }


}
