USE iot;

-- DROP TABLE IF EXISTS `zero_enum_genealogy`;
-- DROP TABLE IF EXISTS `zero_enums`;

--  ///  zero_enums BEGIN 

CREATE TABLE IF NOT EXISTS `zero_enums` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`features` JSON NOT NULL,
	`enum_name` VARCHAR(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `zero_enums` ADD PRIMARY KEY pk_zero_enums_id (`id`);
ALTER TABLE `zero_enums` ADD KEY idx_zero_enums_create_time (`create_time`);
ALTER TABLE `zero_enums` ADD KEY idx_zero_enums_update_time (`update_time`);
ALTER TABLE `zero_enums` ADD UNIQUE KEY uk_zero_enums_name (`enum_name`);

DROP TRIGGER IF EXISTS `zero_enums_uuid`;
DELIMITER $
CREATE TRIGGER `zero_enums_uuid`
BEFORE INSERT
ON `zero_enums`
FOR EACH ROW
BEGIN
	IF new.id = '-'
    THEN 
		SET new.id = (SELECT uuid());
	END IF;
END$
DELIMITER ;

DROP TRIGGER IF EXISTS `zero_enums_update_time`;
DELIMITER $
CREATE TRIGGER `zero_enums_update_time`
BEFORE UPDATE
ON `zero_enums`
FOR EACH ROW
BEGIN
	SET new.update_time = (SELECT now());
END$
DELIMITER ;

--  ///  zero_enums END 

--  ///  zero_enum_genealogy BEGIN 

CREATE TABLE IF NOT EXISTS `zero_enum_genealogy` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`enum_id` VARCHAR(36) NOT NULL,
	`option_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `zero_enum_genealogy` ADD PRIMARY KEY pk_zero_enums_id (`id`);
ALTER TABLE `zero_enum_genealogy` ADD CONSTRAINT fk_zero_folder_enum_id
	FOREIGN KEY (`enum_id`) 
 		REFERENCES zero_enums(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `zero_enum_genealogy` ADD CONSTRAINT fk_zero_folder_option_id
	FOREIGN KEY (`option_id`) 
 		REFERENCES zero_enums(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

DROP TRIGGER IF EXISTS `zero_enum_genealogy_uuid`;
DELIMITER $
CREATE TRIGGER `zero_enum_genealogy_uuid`
BEFORE INSERT
ON `zero_enum_genealogy`
FOR EACH ROW
BEGIN
	IF new.id = '-'
    THEN 
		SET new.id = (SELECT uuid());
	END IF;
END$
DELIMITER ;

--  ///  zero_enum_genealogy END 

-- // DML VIEW `zero_enum_view` BEGIN

DROP VIEW IF EXISTS `zero_enum_view`;
CREATE VIEW `zero_enum_view` AS
SELECT 
	a.* ,b.enum_id 
FROM 
	zero_enums a,
    zero_enum_genealogy b
WHERE 
	a.ID = b.option_id;

-- // DML VIEW `zero_enum_view` END

-- // DML VIEW `zero_root_enum_view` BEGIN

DROP VIEW IF EXISTS `zero_root_enum_view`;
CREATE VIEW `zero_root_enum_view` AS
SELECT 
	t.* 
FROM 
(
	SELECT 
		a.*, b.ID AS LINK_ID
	FROM 
		zero_enums a
	LEFT JOIN
	    zero_enum_genealogy b
	ON
		a.id = b.option_id
) t WHERE 
	t.LINK_ID IS NULL;

-- // DML VIEW `zero_root_enum_view` END

-- // INIT 

DELIMITER $$ 
DROP PROCEDURE IF EXISTS `ENUM_CREATE_GROUP` $$
CREATE PROCEDURE `ENUM_CREATE_GROUP`(
	IN C_ENUM_NAME VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
	IN C_FEATURES json
)
BEGIN
	IF (SELECT count(1) FROM zero_enums WHERE enum_name = C_ENUM_NAME) <= 0
    THEN 
		INSERT INTO zero_enums(enum_name, features) VALUES (C_ENUM_NAME ,C_FEATURES);
		SELECT * FROM zero_enums WHERE enum_name = C_ENUM_NAME;
	END IF;
END 
$$
DELIMITER ;

DELIMITER $$ 
DROP PROCEDURE IF EXISTS `ENUM_ADD` $$
CREATE PROCEDURE `ENUM_ADD`(
	IN C_PARENT_ENUM_NAME VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
	IN C_ENUM_NAME VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
	IN C_FEATURES json
)
BEGIN
	IF (SELECT count(1) FROM zero_enums WHERE enum_name = C_ENUM_NAME) <= 0
    THEN 
		INSERT INTO zero_enums(enum_name, features) VALUES (C_ENUM_NAME ,C_FEATURES);
		INSERT INTO zero_enum_genealogy(enum_id ,option_id) VALUES ((SELECT ID FROM curacao_spring_enums WHERE enum_name = C_PARENT_ENUM_NAME) ,(SELECT ID FROM curacao_spring_enums WHERE enum_name = C_ENUM_NAME));
		SELECT * FROM zero_enums WHERE enum_name = C_ENUM_NAME;
	END IF;
END 
$$
DELIMITER ;

DELIMITER $$ 
DROP PROCEDURE IF EXISTS `ENUM_PROTECTED_CREATE_GROUP` $$
CREATE PROCEDURE `ENUM_PROTECTED_CREATE_GROUP`(
	IN C_ENUM_NAME VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci
)
BEGIN
	IF (SELECT count(1) FROM zero_enums WHERE enum_name = C_ENUM_NAME) <= 0
    THEN 
		INSERT INTO zero_enums(enum_name, features) VALUES (C_ENUM_NAME ,'{}');
		SELECT * FROM zero_enums WHERE enum_name = C_ENUM_NAME;
	END IF;
END 
$$
DELIMITER ;

-- // INIT 
