SET @preboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE `name` = 'preboarding');

-- Start: Workflow entity process configuration
SET @max_wf_order := (SELECT MAX(`order_no`) FROM `ohrm_workflow`);
INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `group_name`, `group_label`, `available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_WELCOME_TO_THE_TEAM_PROCESS', 'PREBOARDING_WELCOME_TO_THE_TEAM_PROCESS', null,
        'preboarding', 'Preboarding', '1,3', @preboarding_module_id, @max_wf_order + 1);

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES ('PREBOARDING_WELCOME_TO_THE_TEAM_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_PROCESS', 'Pending', 'SYSTEM', 'Complete', 'Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Preboarding Employee', 'PREBOARDING_WELCOME_TO_THE_TEAM_PROCESS', 'Welcome New Hire to the Team', 'Send Welcome Email to the New Hire', 1,
        '{"icon":"oxd-welcome-email"}');

SET @welcome_to_the_team_entity_process_id := (SELECT `id` FROM `ohrm_advanced_workflow_entity_process` WHERE `name` = 'Welcome New Hire to the Team');
SET @max_entity_process_status_id := (SELECT GREATEST(100, IFNULL(MAX(`status_id`), 0)) FROM `ohrm_advanced_workflow_entity_process_status`);

INSERT INTO `ohrm_advanced_workflow_entity_process_status`
(`entity_process_id`, `status_id`, `name`, `description`, `sequence`)
VALUES (@welcome_to_the_team_entity_process_id, @max_entity_process_status_id + 1 , 'AWM_Initial',
        'Process to be initialized by the System', '0'),
       (@welcome_to_the_team_entity_process_id, @max_entity_process_status_id + 2, 'Pending',
        'Welcome to the team is pending to be completed', '1'),
       (@welcome_to_the_team_entity_process_id, @max_entity_process_status_id + 3, 'Completed',
        'Welcome to the team is completed', '2');

-- End: Workflow entity process configuration

-- Start: Welcome to the Team Task configuration
INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `group_name`, `group_label`, `available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'PREBOARDING_WELCOME_TO_THE_TEAM_TASK', null,
        'preboarding', 'Preboarding', '1,3', @preboarding_module_id, @max_wf_order + 2);

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Pending', 'HiringManager', 'Send', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Pending', 'Admin', 'Send', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Pending', 'Supervisor', 'Send', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Pending', 'HeadOfDepartment', 'Send', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Pending', 'PreboardingEventManager', 'Send', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Completed', 'Admin', 'Read', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Completed', 'Supervisor', 'Read', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Completed', 'HeadOfDepartment', 'Read', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Completed', 'HiringManager', 'Read', 'Completed', '0'),
       ('PREBOARDING_WELCOME_TO_THE_TEAM_TASK', 'Completed', 'PreboardingEventManager', 'Read', 'Completed', '0');

SET @welcome_to_the_team_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding Welcome to the Team');

SET @welcome_to_the_team_task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @welcome_to_the_team_entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('Admin', JSON_OBJECT()),
                        'sharedTask', true,
                        'sharedFormData', true,
                        'generatorHelper', 'PreboardingTaskGeneratorHelper',
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Completed'
                ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Send Welcome Email to the New Hire', 'Send Welcome to the Team Email for %preboardingEmployeeName%', @preboarding_module_id,
        '', 'PREBOARDING_WELCOME_TO_THE_TEAM_TASK', @welcome_to_the_team_form_version_id,
        @welcome_to_the_team_task_options);

SET @welcome_to_the_team_task_template_id := (SELECT `id` FROM ohrm_advanced_task_template WHERE `name` = 'Send Welcome Email to the New Hire');

SET @preboarding_initiation_wfsm_id := (SELECT `id`
                                        FROM `ohrm_workflow_state_machine`
                                        WHERE `workflow` = 'PREBOARDING_WELCOME_TO_THE_TEAM_PROCESS'
                                          AND `state` = 'AWM_Initial'
                                          AND `role` = 'SYSTEM'
                                          AND `action` = 'AWM_Initialize'
                                          AND `resulting_state` = 'Pending');

-- Create Welcome to the Team Submission task upon moving the Candidate to the Onboarding stage
INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
VALUES (@preboarding_initiation_wfsm_id, 'WorkflowStateChangeTaskCreator',
        JSON_OBJECT('task', JSON_OBJECT('templateId', @welcome_to_the_team_task_template_id)));

-- Start: "Create New Hire Login" Task configuration

INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `group_name`, `group_label`, `available_to`, `default_available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_CREATE_NEW_HIRE_LOGIN_TASK', 'PREBOARDING_CREATE_NEW_HIRE_LOGIN_TASK',
        'Onboarding - Create New Hire Login Task','preboarding', 'Preboarding', '1', '1', @preboarding_module_id, @max_wf_order + 3);

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES ('PREBOARDING_CREATE_NEW_HIRE_LOGIN_TASK', 'Pending', 'SYSTEM', 'Create New Hire Login', 'Completed', 0);

SET @task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @welcome_to_the_team_entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('SYSTEM', JSON_OBJECT()),
                        'generatorHelper', 'CreateNewHireGuestLoginTaskGeneratorHelper',
                        'executorHelper', 'CreateNewHireGuestLoginTaskExecutorHelper',
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Completed',
                        'autoExecute', true
                ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `options`)
VALUES ('Create New Hire Login', 'Create New Hire Login for %preboardingEmployeeName%', @preboarding_module_id,
        '', 'PREBOARDING_CREATE_NEW_HIRE_LOGIN_TASK',
        @task_options);

SET @create_new_hire_login_task_template_id := (SELECT `id`
                                                   FROM ohrm_advanced_task_template
                                                   WHERE `name` = 'Create New Hire Login');

-- Create the Create New Hire Login task upon completing the Initiate Onboarding Process Task
INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`,
       'WorkflowStateChangeTaskCreator',
       JSON_OBJECT('task', JSON_OBJECT('templateId', @create_new_hire_login_task_template_id))
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_WELCOME_TO_THE_TEAM_TASK'
  AND `state` = 'Pending'
  AND `action` = 'Send'
  AND `resulting_state` = 'Completed';
-- End: "Create New Hire Login" Task configuration
-- Move the Welcome to the Team to "Completed" stage upon submitting the Welcome to the Team
INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`,
       'TaskStateChangeWorkflowProcessStateChanger',
       JSON_OBJECT(
               'entityProcessId', @welcome_to_the_team_entity_process_id,
               'action', 'Complete'
       )
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_CREATE_NEW_HIRE_LOGIN_TASK'
  AND `state` = 'Pending'
  AND `action` = 'Create New Hire Login'
  AND `resulting_state` = 'Completed';

-- End: Welcome to the Team Task configuration

SET @master_state_change_id := (SELECT `id` FROM `ohrm_workflow_state_machine` WHERE `workflow` = 'PREBOARDING_WELCOME_TO_THE_TEAM_PROCESS' AND `role` = 'SYSTEM' AND `action` = 'Complete');
SET @slave_state_change_id := (SELECT `id` FROM `ohrm_workflow_state_machine` WHERE `workflow` = 'PREBOARDING_EMPLOYEE_ENTITY' AND `role` = 'SYSTEM' AND `action` = 'Complete');
INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`, `options`)
VALUES (@master_state_change_id, @slave_state_change_id, 'AdvancedWorkflowProcessToRequestStateChangeTrigger',
        '{"validateSameStateForAllProcesses":true,"considerOnlyEnabledProcesses":true}');
