CREATE TABLE complex_house_plans (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    house_id BIGINT UNSIGNED NOT NULL,
    light_angle INT NULL,
    has_light_angle TINYINT DEFAULT 0 NULL,
    is_3d TINYINT DEFAULT 0 NULL,
    code varchar(255) COLLATE utf8_general_ci NULL,
    `3d_url` varchar(255) COLLATE utf8_general_ci NULL,
    CONSTRAINT complex_house_plans_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

CREATE TABLE `complex_entrances` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `position` int(11) NOT NULL,
    `complex_house_id` bigint(20) unsigned NOT NULL,
    PRIMARY KEY (`id`),
    KEY `complex_entrances_FK` (`complex_house_id`),
    CONSTRAINT `complex_entrances_FK` FOREIGN KEY (`complex_house_id`) REFERENCES `complex_houses` (`id`) ON DELETE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 50 DEFAULT CHARSET = utf8;

CREATE TABLE complex_house_plans_images (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    plan_id BIGINT UNSIGNED NOT NULL,
    is_main TINYINT DEFAULT 0 NULL,
    `position` INT DEFAULT 1 NULL,
    url VARCHAR(255) COLLATE utf8_general_ci NULL,
    name varchar(255) COLLATE utf8_general_ci NULL,
    CONSTRAINT complex_house_plans_images_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

CREATE TABLE `complex_rooms` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `position` int(11) NOT NULL,
    `type` enum(
        'apartment',
        'apartments',
        'parking_space',
        'office',
        'storage_room',
        'warehouse',
        'retail_space',
        'commercial_space',
        'townhouse',
        'villa',
        'penthouse',
        'duplex',
        'cottage'
    ) NOT NULL DEFAULT 'apartment',
    `floor_id` bigint(20) unsigned NOT NULL,
    `number` varchar(100) NOT NULL,
    `rooms_count` int(11) NOT NULL,
    `area` int(11) NOT NULL,
    `status` enum(
        'not_for_sale',
        'sold',
        'reservation',
        'available'
    ) NOT NULL,
    `total_amount` bigint(20) NOT NULL,
    `layout_code` varchar(100) DEFAULT NULL,
    `payment_types` varchar(255) DEFAULT '[]',
    `layout_feature` enum(
        'none_of_the_above',
        'studio',
        'european_layout',
        'available'
    ) DEFAULT NULL,
    `on_site_number` int(11) DEFAULT NULL,
    `description` text,
    `windows_placement_id` int(11) DEFAULT NULL,
    `decoration_id` bigint(20) unsigned DEFAULT NULL,
    `estimated_area` decimal(8, 1) DEFAULT NULL,
    `living_area` decimal(8, 1) DEFAULT NULL,
    `kitchen_area` decimal(8, 1) DEFAULT NULL,
    `loggias_count` int(11) DEFAULT NULL,
    `balconies_count` int(11) DEFAULT NULL,
    `combined_bathrooms_count` int(11) DEFAULT NULL,
    `seperated_bathrooms_count` int(11) DEFAULT NULL,
    `bti_room_number` varchar(100) DEFAULT NULL,
    `bti_total_area` int(11) DEFAULT NULL,
    `area_without_balcony` int(11) DEFAULT NULL,
    `hallway_area` int(11) DEFAULT NULL,
    `ceiling_height` decimal(4, 2) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `complex_rooms_FK` (`floor_id`),
    CONSTRAINT `complex_rooms_FK` FOREIGN KEY (`floor_id`) REFERENCES `complex_floors` (`id`) ON DELETE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1503 DEFAULT CHARSET = utf8;

CREATE TABLE complex_rooms_plans (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    room_id BIGINT UNSIGNED NULL,
    plan_id BIGINT UNSIGNED NOT NULL,
    CONSTRAINT complex_rooms_plans_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

CREATE TABLE complex_house_floor_plans (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    house_id BIGINT UNSIGNED NOT NULL,
    light_angle INT NULL,
    has_light_angle TINYINT(1) DEFAULT 0 NOT NULL,
    is_use_all_entrances TINYINT(1) DEFAULT 0 NOT NULL,
    entrances json NULL,
    floor_positions json NULL,
    image VARCHAR(255) COLLATE utf8_general_ci NULL,
    name varchar(255) COLLATE utf8_general_ci NULL,
    CONSTRAINT complex_house_floor_plans_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

CREATE TABLE complex_floors_plans (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    floor_id BIGINT UNSIGNED NOT NULL,
    floor_plan_id BIGINT UNSIGNED NOT NULL,
    CONSTRAINT complex_floors_plans_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

CREATE TABLE `complex_floors` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `position` int(11) NOT NULL,
    `complex_entrance_id` bigint(20) unsigned NOT NULL,
    `alignment` enum('left', 'center', 'right') NOT NULL DEFAULT 'left',
    PRIMARY KEY (`id`),
    KEY `complex_floors_FK` (`complex_entrance_id`),
    CONSTRAINT `complex_floors_FK` FOREIGN KEY (`complex_entrance_id`) REFERENCES `complex_entrances` (`id`) ON DELETE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 399 DEFAULT CHARSET = utf8;

CREATE TABLE complex_floor_plan_figures (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    floor_plan_id BIGINT UNSIGNED NOT NULL,
    room_plan_id BIGINT UNSIGNED NULL,
    background varchar(100) COLLATE utf8_general_ci NULL,
    points json NULL,
    CONSTRAINT complex_floor_plan_figures_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

CREATE TABLE complex_house_images (
    id bigint(20) unsigned auto_increment NOT NULL,
    house_id bigint(20) unsigned NOT NULL,
    url varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
    name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
    CONSTRAINT `PRIMARY` PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = '';

CREATE TABLE complex_house_facades (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    house_id BIGINT UNSIGNED NOT NULL,
    name varchar(255) NULL,
    is_published TINYINT(1) DEFAULT 0 NOT NULL,
    hover_color varchar(100) COLLATE utf8_general_ci NULL,
    opacity varchar(100) COLLATE utf8_general_ci NULL,
    image varchar(255) COLLATE utf8_general_ci NULL,
    `position` INT NULL,
    CONSTRAINT complex_house_facades_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

CREATE TABLE complex_facade_figures (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    facade_id BIGINT UNSIGNED NOT NULL,
    floor_id BIGINT UNSIGNED NULL,
    entrance_id BIGINT UNSIGNED NULL,
    points json NULL,
    CONSTRAINT complex_facade_figures_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

CREATE TABLE `complex_rooms` (
    `id` bigint(20) UNSIGNED NOT NULL,
    `position` int(11) DEFAULT NULL,
    `type` enum(
        'apartment',
        'apartments',
        'parking_space',
        'office',
        'storage_room',
        'warehouse',
        'retail_space',
        'commercial_space',
        'townhouse',
        'villa',
        'penthouse',
        'duplex',
        'cottage'
    ) CHARACTER SET latin1 DEFAULT NULL,
    `floor_id` bigint(20) UNSIGNED DEFAULT NULL,
    `number` varchar(100) DEFAULT NULL,
    `rooms_count` int(11) DEFAULT NULL,
    `area` int(11) DEFAULT NULL,
    `status` enum(
        'not_for_sale',
        'sold',
        'reservation',
        'available'
    ) CHARACTER SET latin1 DEFAULT NULL,
    `total_amount` bigint(20) DEFAULT NULL,
    `layout_code` varchar(100) DEFAULT NULL,
    `payment_types` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
    `layout_feature` enum(
        'none_of_the_above',
        'studio',
        'european_layout',
        'available'
    ) CHARACTER SET latin1 DEFAULT NULL,
    `on_site_number` int(11) DEFAULT NULL,
    `description` text CHARACTER SET latin1,
    `windows_placement_id` int(11) DEFAULT NULL,
    `decoration_id` bigint(20) UNSIGNED DEFAULT NULL,
    `estimated_area` decimal(8, 1) DEFAULT NULL,
    `living_area` decimal(8, 1) DEFAULT NULL,
    `kitchen_area` decimal(8, 1) DEFAULT NULL,
    `loggias_count` int(11) DEFAULT NULL,
    `balconies_count` int(11) DEFAULT NULL,
    `combined_bathrooms_count` int(11) DEFAULT NULL,
    `seperated_bathrooms_count` int(11) DEFAULT NULL,
    `bti_room_number` varchar(100) DEFAULT NULL,
    `bti_total_area` int(11) DEFAULT NULL,
    `area_without_balcony` int(11) DEFAULT NULL,
    `hallway_area` int(11) DEFAULT NULL,
    `ceiling_height` decimal(4, 2) DEFAULT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

ALTER TABLE `complex_rooms`
ADD PRIMARY KEY (`id`),
ADD KEY `complex_rooms_FK` (`floor_id`);

ALTER TABLE `complex_rooms`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 1;

ALTER TABLE complex_metro
ADD subway_type varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;

ALTER TABLE user_permissions
ADD menu_complexes TINYINT(4) DEFAULT 0 NOT NULL COMMENT 'Видимость: 0 - нет, 1 - да';

ALTER TABLE complex_houses ADD is_archive TINYINT(1) DEFAULT 0 NULL;

ALTER TABLE complex_list ADD rayon_id BIGINT UNSIGNED NULL;

ALTER TABLE complex_rooms
CHANGE windows_placement windows_placement_id INT NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN windows_placement_id INT NULL;

CREATE TABLE complex_rooms_windows_placements (
    id INT auto_increment NOT NULL,
    name varchar(255) COLLATE utf8_general_ci NOT NULL,
    CONSTRAINT complex_rooms_windows_placements_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

INSERT INTO
    complex_rooms_windows_placements (name)
VALUES ('Во двор'),
    ('На улицу'),
    ('Во двор и на улицу'),
    ('В парк'),
    ('Во двор и сквер'),
    ('Во двор-колодец'),
    ('Зеленый двор'),
    ('На 2 стороны'),
    ('На 3 стороны'),
    ('На залив'),
    ('На залив и во двор'),
    ('На залив и на улицу'),
    ('На лес'),
    ('На лес и во двор'),
    ('На набережную'),
    ('На набережную и в сквер'),
    ('На набережную и во двор'),
    ('На реку'),
    ('На озеро'),
    ('На площадь'),
    ('На площадь и во двор'),
    ('На поляну'),
    ('На пруды'),
    ('На сквер'),
    ('На собор'),
    ('На улицу'),
    ('На улицу и в парк'),
    ('На улицу и в сквер'),
    ('На улицу и на воду'),
    ('Панорама на город'),
    ('На море'),
    ('На горы'),
    ('На море и горы');

CREATE TABLE `complex_floors` (
    `id` bigint(20) UNSIGNED NOT NULL,
    `position` int(11) DEFAULT NULL,
    `complex_entrance_id` bigint(20) UNSIGNED DEFAULT NULL,
    `alignment` enum('left', 'center', 'right') DEFAULT 'left'
) ENGINE = InnoDB DEFAULT CHARSET = latin1;

ALTER TABLE `complex_floors`
ADD PRIMARY KEY (`id`),
ADD KEY `complex_floors_FK` (`complex_entrance_id`);

ALTER TABLE `complex_floors`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 1;

ALTER TABLE `complex_rooms`
ADD CONSTRAINT `complex_rooms_FK` FOREIGN KEY (`floor_id`) REFERENCES `complex_floors` (`id`) ON DELETE CASCADE;

CREATE TABLE `complex_entrances` (
    `id` bigint(20) UNSIGNED NOT NULL,
    `name` varchar(255) DEFAULT NULL,
    `position` int(11) DEFAULT NULL,
    `complex_house_id` bigint(20) UNSIGNED DEFAULT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

ALTER TABLE `complex_entrances`
ADD PRIMARY KEY (`id`),
ADD KEY `complex_entrances_FK` (`complex_house_id`);

ALTER TABLE `complex_entrances`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
AUTO_INCREMENT = 1;

ALTER TABLE `complex_entrances`
ADD CONSTRAINT `complex_entrances_FK` FOREIGN KEY (`complex_house_id`) REFERENCES `complex_houses` (`id`) ON DELETE CASCADE;

ALTER TABLE complex_address
CHANGE rayon_id rayon VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

ALTER TABLE complex_address
MODIFY COLUMN rayon VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

ALTER TABLE complex_address
ADD CONSTRAINT complex_address_PK PRIMARY KEY (id);

ALTER TABLE complex_address
MODIFY COLUMN id int(11) auto_increment NOT NULL;

ALTER TABLE requisitions
ADD status varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

ALTER TABLE requisitions ADD complex_room_id BIGINT UNSIGNED NULL;

DELETE FROM complex_floors
WHERE
    complex_entrance_id NOT IN(
        SELECT id
        FROM complex_entrances
    );
# USE THIS TO DELETE OLD RECORDS FROM DEV SERVER
ALTER TABLE complex_floors ADD CONSTRAINT complex_floors_FK FOREIGN KEY (complex_entrance_id) REFERENCES complex_entrances(id) ON DELETE CASCADE;

ALTER TABLE complex_houses
MODIFY COLUMN id bigint(20) unsigned auto_increment NOT NULL;

DELETE FROM complex_houses
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_houses
ADD CONSTRAINT complex_houses_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_house_facades
WHERE
    house_id NOT IN(
        SELECT id
        FROM complex_houses
    );

ALTER TABLE complex_house_facades
ADD CONSTRAINT complex_house_facades_FK FOREIGN KEY (house_id) REFERENCES complex_houses (id) ON DELETE CASCADE;

DELETE FROM complex_house_floor_plans
WHERE
    house_id NOT IN(
        SELECT id
        FROM complex_houses
    );

ALTER TABLE complex_house_floor_plans
ADD CONSTRAINT complex_house_floor_plans_FK FOREIGN KEY (house_id) REFERENCES complex_houses (id) ON DELETE CASCADE;

DELETE FROM complex_house_images
WHERE
    house_id NOT IN(
        SELECT id
        FROM complex_houses
    );

ALTER TABLE complex_house_images
ADD CONSTRAINT complex_house_images_FK FOREIGN KEY (house_id) REFERENCES complex_houses (id) ON DELETE CASCADE;

ALTER TABLE complex_house_metro
MODIFY COLUMN house_id bigint(20) unsigned NOT NULL;

DELETE FROM complex_house_metro
WHERE
    house_id NOT IN(
        SELECT id
        FROM complex_houses
    );

ALTER TABLE complex_house_metro
ADD CONSTRAINT complex_house_metro_FK FOREIGN KEY (house_id) REFERENCES complex_houses (id) ON DELETE CASCADE;

DELETE FROM complex_house_plans
WHERE
    house_id NOT IN(
        SELECT id
        FROM complex_houses
    );

ALTER TABLE complex_house_plans
ADD CONSTRAINT complex_house_plans_FK FOREIGN KEY (house_id) REFERENCES complex_houses (id) ON DELETE CASCADE;

DELETE FROM complex_house_plans_images
WHERE
    plan_id NOT IN(
        SELECT id
        FROM complex_house_plans
    );

ALTER TABLE complex_house_plans_images
ADD CONSTRAINT complex_house_plans_images_FK FOREIGN KEY (plan_id) REFERENCES complex_house_plans (id) ON DELETE CASCADE;

DELETE FROM complex_address
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_address
ADD CONSTRAINT complex_address_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_actions
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_actions
ADD CONSTRAINT complex_actions_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_advert
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_advert
ADD CONSTRAINT complex_advert_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_corpuses
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_corpuses
ADD CONSTRAINT complex_corpuses_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_dop
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_dop
ADD CONSTRAINT complex_dop_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_entrances
WHERE
    complex_house_id NOT IN(
        SELECT id
        FROM complex_houses
    );

ALTER TABLE complex_entrances
ADD CONSTRAINT complex_entrances_FK FOREIGN KEY (complex_house_id) REFERENCES complex_houses (id) ON DELETE CASCADE;

DELETE FROM complex_facade_figures
WHERE
    facade_id NOT IN(
        SELECT id
        FROM complex_house_facades
    );

ALTER TABLE complex_facade_figures
ADD CONSTRAINT complex_facade_figures_FK FOREIGN KEY (facade_id) REFERENCES complex_house_facades (id) ON DELETE CASCADE;

DELETE FROM complex_floor_plan_figures
WHERE
    floor_plan_id NOT IN(
        SELECT id
        FROM complex_house_floor_plans
    );

ALTER TABLE complex_floor_plan_figures
ADD CONSTRAINT complex_floor_plan_figures_FK FOREIGN KEY (floor_plan_id) REFERENCES complex_house_floor_plans (id) ON DELETE CASCADE;

DELETE FROM complex_floors_plans
WHERE
    floor_id NOT IN(
        SELECT id
        FROM complex_floors
    );

ALTER TABLE complex_floors_plans
ADD CONSTRAINT complex_floors_plans_FK FOREIGN KEY (floor_plan_id) REFERENCES complex_house_floor_plans (id) ON DELETE CASCADE;

ALTER TABLE complex_floors_plans
ADD CONSTRAINT complex_floors_plans_FK_floor FOREIGN KEY (floor_id) REFERENCES complex_floors (id) ON DELETE CASCADE;

DELETE FROM complex_images
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_images
ADD CONSTRAINT complex_images_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_infrastructure
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_infrastructure
ADD CONSTRAINT complex_infrastructure_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_metro
WHERE
    complex_id NOT IN(
        SELECT id
        FROM complex_list
    );

ALTER TABLE complex_metro
ADD CONSTRAINT complex_metro_FK FOREIGN KEY (complex_id) REFERENCES complex_list (id) ON DELETE CASCADE;

DELETE FROM complex_rooms
WHERE
    floor_id NOT IN(
        SELECT id
        FROM complex_floors
    );

ALTER TABLE complex_rooms
ADD CONSTRAINT complex_rooms_FK FOREIGN KEY (floor_id) REFERENCES complex_floors (id) ON DELETE CASCADE;

DELETE FROM complex_rooms_plans
WHERE
    room_id NOT IN(
        SELECT id
        FROM complex_rooms
    );

ALTER TABLE complex_rooms_plans
ADD CONSTRAINT complex_rooms_plans_FK FOREIGN KEY (room_id) REFERENCES complex_rooms (id) ON DELETE CASCADE;

DELETE FROM complex_rooms_plans
WHERE
    plan_id NOT IN(
        SELECT id
        FROM complex_house_plans
    );

ALTER TABLE complex_rooms_plans
ADD CONSTRAINT complex_rooms_plans_FK_1 FOREIGN KEY (plan_id) REFERENCES complex_house_plans (id) ON DELETE CASCADE;

CREATE TABLE job_delete_files (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    url varchar(500) COLLATE utf8_general_ci NOT NULL,
    CONSTRAINT job_delete_files_PK PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci;

-- ROOMS ADD
ALTER TABLE complex_rooms
ADD ceiling_height DECIMAL(4, 2) CHECK (
    value > 0
    AND value <= 99.99
)

-- HOUSE ADD
ALTER TABLE complex_houses ADD `security` TINYINT(1) DEFAULT 0 NULL;

ALTER TABLE complex_houses
ADD parking varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

ALTER TABLE complex_houses ADD fenced_area TINYINT(1) DEFAULT 0 NULL;

ALTER TABLE complex_houses
ADD sports_ground TINYINT(1) DEFAULT 0 NULL;

ALTER TABLE complex_houses ADD play_ground TINYINT(1) DEFAULT 0 NULL;

ALTER TABLE complex_houses ADD school TINYINT(1) DEFAULT 0 NULL;

ALTER TABLE complex_houses
ADD kinder_garten TINYINT(1) DEFAULT 0 NULL;

ALTER TABLE complex_house_UTP
CHANGE desck `desc` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

ALTER TABLE complex_house_UTP
CHANGE photos photo text CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

ALTER TABLE complex_list ADD domclick_id int(11) NULL;

ALTER TABLE complex_list ADD domclick_builder_id INT(11) NULL;

ALTER TABLE complex_list ADD cian_id INT(11) NULL;

ALTER TABLE complex_list ADD avito_id INT(11) NULL;

ALTER TABLE complex_list ADD yandex_id INT(11) NULL;

ALTER TABLE complex_houses ADD domclick_id int(11) NULL;

ALTER TABLE complex_houses ADD cian_id INT(11) NULL;

ALTER TABLE complex_houses ADD avito_id INT(11) NULL;

ALTER TABLE complex_houses ADD yandex_id INT(11) NULL;

ALTER TABLE complex_actions
MODIFY COLUMN id int(11) auto_increment NOT NULL;

ALTER TABLE complex_actions
ADD CONSTRAINT complex_actions_PK PRIMARY KEY (id);

ALTER TABLE complex_actions
CHANGE images image text CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

ALTER TABLE complex_list
ADD cadastral_number varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL;

ALTER TABLE complex_houses DROP COLUMN cadastral_number;

ALTER TABLE complex_list
ADD has_show_room tinyint(1) DEFAULT 0 NOT NULL;

ALTER TABLE complex_houses DROP COLUMN is_shop;

-- IMPORT FROM XLSX, XLS, CSV

CREATE TABLE `complex_house_import` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `document` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
    `document_preview` text CHARACTER SET utf8mb4,
    `house_name_to_id` text COLLATE utf8mb4_unicode_ci,
    `mapping_fields` text CHARACTER SET utf8,
    `step` int(11) NOT NULL,
    `status` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT '',
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `user_id` mediumint(9) NOT NULL,
    `dynamic_fields` text COLLATE utf8mb4_unicode_ci,
    PRIMARY KEY (`id`),
    KEY `complex_house_import_FK` (`user_id`),
    CONSTRAINT `complex_house_import_FK` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 138 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

ALTER TABLE complex_rooms MODIFY COLUMN area DECIMAL(8, 2) NOT NULL;

CREATE TABLE complex_status (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    name VARCHAR(100) NOT NULL,
    status ENUM(
        'not_for_sale',
        'sold',
        'reservation',
        'available'
    ) NOT NULL,
    agency_id MEDIUMINT(9),
    color varchar(7) NOT NULL,
    is_archive BIT NOT NULL DEFAULT 0,
    CONSTRAINT complex_status_pk PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT = 1;

ALTER TABLE complex_status
ADD CONSTRAINT complex_status_users_FK FOREIGN KEY (agency_id) REFERENCES users (id) ON DELETE CASCADE;

INSERT INTO
    complex_status (
        name,
        status,
        color,
        is_archive
    )
VALUES (
        'Не для продажи',
        'not_for_sale',
        '#dedede',
        0
    ),
    (
        'Продано',
        'sold',
        '#757575',
        0
    ),
    (
        'Бронь',
        'reservation',
        '#f2b720',
        0
    ),
    (
        'Свободные',
        'available',
        '#4caf50',
        0
    );

CREATE TABLE complex_status_reservation (
    id BIGINT UNSIGNED auto_increment NOT NULL,
    booking_hour INTEGER DEFAULT NULL,
    booking_day INTEGER DEFAULT NULL,
    reminder_hour INTEGER DEFAULT NULL,
    reminder_day INTEGER DEFAULT NULL,
    booking_type ENUM('temporary', 'constant') NOT NULL,
    complex_status_id BIGINT UNSIGNED NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT complex_status_reservation_complex_status_FK FOREIGN KEY (complex_status_id) REFERENCES complex_status (id) ON DELETE CASCADE,
    CONSTRAINT complex_status_reservation PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT = 1;

ALTER TABLE requisitions ADD is_interrupted BOOL DEFAULT 0 NULL;

ALTER TABLE complex_rooms
CHANGE status complex_status_id BIGINT UNSIGNED NOT NULL;

UPDATE complex_rooms
SET
    complex_status_id = 4
WHERE
    complex_status_id = 0;

ALTER TABLE complex_rooms
ADD CONSTRAINT complex_rooms_complex_status_FK FOREIGN KEY (complex_status_id) REFERENCES complex_status (id);

ALTER TABLE requisitions ADD complex_status_id BIGINT UNSIGNED;

UPDATE requisitions
SET
    complex_status_id = CASE
        WHEN status = '' THEN NULL
        WHEN status = 'not_for_sale' THEN 1
        WHEN status = 'sold' THEN 2
        WHEN status = 'reservation' THEN 3
        WHEN status = 'available' THEN 4
        ELSE status
    END;

ALTER TABLE requisitions DROP COLUMN status;

ALTER TABLE complex_rooms
MODIFY COLUMN description text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN estimated_area decimal(8, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN living_area decimal(8, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN kitchen_area decimal(8, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN estimated_area decimal(4, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN living_area decimal(4, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN kitchen_area decimal(4, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN hallway_area decimal(8, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN bti_total_area decimal(8, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN area_without_balcony decimal(8, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN total_amount decimal(12, 2) NOT NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN estimated_area decimal(8, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN living_area decimal(8, 2) NULL;

ALTER TABLE complex_rooms
MODIFY COLUMN kitchen_area decimal(8, 2) NULL;

ALTER TABLE requisitions
ADD er_is_interrupted tinyint(1) DEFAULT 0 NULL;

ALTER TABLE complex_status_reservation
ADD can_change_booking_queue VARCHAR(1000) DEFAULT NULL,
ADD reservation_extension BOOL DEFAULT 0,
ADD specify_comment_confirm BOOL DEFAULT 0,
ADD managers_of_confirm_reservation_extension VARCHAR(1000) DEFAULT NULL,
ADD employees_not_required_confirmation VARCHAR(1000) DEFAULT NULL,
ADD specify_comment_unconfirm BOOL DEFAULT 0,
ADD managers_of_unconfirm_reservation_extension VARCHAR(1000) DEFAULT NULL,
ADD employees_not_required_unconfirmation VARCHAR(1000) DEFAULT NULL;

ALTER TABLE requisitions
ADD reservation_updated_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL;

UPDATE requisitions SET reservation_updated_at = created_at;

-- Complex Status 3 dot
ALTER TABLE funnel_steps
ADD is_select_room tinyint(1) DEFAULT 0 NOT NULL,
ADD complex_status_id bigint(20) unsigned NULL;

ALTER TABLE requisitions ADD `position` int(11) DEFAULT 1 NOT NULL;

ALTER TABLE clients_funnel_step
ADD history_requisition TEXT NOT NULL;

ALTER TABLE clients_funnel_step
ADD transaction_amount decimal(12, 2) NULL;

ALTER TABLE user_client_events
MODIFY COLUMN partner_id int(11) DEFAULT 0 NOT NULL COMMENT 'ИД компании-партнера';

ALTER TABLE `complex_list`
ADD `domrf_id` INT NULL DEFAULT NULL COMMENT 'ИД комплекса на сайте Дом.рф' AFTER `has_show_room`;

ALTER TABLE `complex_houses`
ADD `domrf_id` INT NULL DEFAULT NULL COMMENT 'ИД дома на сайте Дом.рф' AFTER `is_partner_show`;