SET @admin_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'Admin');
SET @config_admin_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'ConfigAdmin');

INSERT INTO `ohrm_module` (`name`, `status`, `data_group_prefix`) VALUES ('ldapAuthentication', 1, 'Admin - LDAP');

SET @module_id := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`) VALUES
('LDAP Configuration', @module_id, 'configureLDAPAuthentication');

SET @ldap_configuration_screen_id := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_user_role_screen (user_role_id, screen_id, can_read, can_create, can_update, can_delete) VALUES
(@admin_role_id, @ldap_configuration_screen_id, 1, 0, 0, 0),
(@config_admin_role_id, @ldap_configuration_screen_id, 1, 1, 1, 0);

INSERT INTO `ohrm_data_group` ( `name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
('ldap_configuration', 'HR Administration - LDAP Configuration', 1, 0, 0, 0);

SET @ldap_configuration_data_group_id := (SELECT LAST_INSERT_ID());

INSERT INTO `ohrm_user_role_data_group` (`user_role_id`, `data_group_id`, `can_read`, `can_create`, `can_update`, `can_delete`, `self`) VALUES
(@admin_role_id, @ldap_configuration_data_group_id, 1, 0, 0, 0, 0),
(@config_admin_role_id, @ldap_configuration_data_group_id, 1, 1, 1, 0, 0);

SET @view_admin_module_id := (SELECT `id` FROM `ohrm_screen` WHERE `module_id` =2 AND `action_url` = 'viewAdminModule');
SET @configure_authentication_screen_id := (SELECT `id` from ohrm_screen where `module_id` = 2 AND `action_url` = 'authenticationConfiguration');

INSERT INTO `ohrm_data_group_screen`(`data_group_id`, `screen_id`, `permission`) VALUES
(@ldap_configuration_data_group_id, @ldap_configuration_screen_id, 1),
(@ldap_configuration_data_group_id, @view_admin_module_id, 1),
(@ldap_configuration_data_group_id, @configure_authentication_screen_id, 1);

INSERT INTO ohrm_user_role_type_data_group(user_role_type_id, data_group_id) VALUES
(@admin_role_id, @ldap_configuration_data_group_id);

SET @people_management_pillar_menu_id := (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'People Management' AND `level` = 1);
SET @hr_administration_module_menu_id := (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'HR Administration'
                                                                            AND `level` = 2 AND parent_id = @people_management_pillar_menu_id);
SET @configuration_menu_id := (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'Configurations' AND `level` = 3 AND `parent_id` = @hr_administration_module_menu_id);
SET @authentication_menu_id := (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'Authentication' AND `level` = 4 AND `parent_id` = @configuration_menu_id);
INSERT INTO ohrm_menu_item (`menu_title`, `screen_id`, `parent_id`, `level`, `order_hint`, `uisref`, `status`) VALUES
    ('LDAP', @ldap_configuration_screen_id, @authentication_menu_id, 5, 200, 'admin.ldap_authentication', 1);
