SET @max_wf_order := (SELECT MAX(`order_no`)
                      FROM `ohrm_workflow`);

SET @recruitment_module_id := (SELECT `id`
                               FROM `ohrm_module`
                               WHERE `name` = 'recruitment');

SET @system_user_id := (SELECT `id`
                        FROM `ohrm_user`
                        WHERE `user_name` = 'System');

SET @system_user_role_id := (SELECT `id`
                             FROM `ohrm_user_role`
                             WHERE `name` = 'SYSTEM');

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

SET @hiring_manager_user_role_id := (SELECT `id`
                                     FROM `ohrm_user_role`
                                     WHERE `name` = 'HiringManager');

SET @admin_user_role_type_id := (SELECT `id`
                                 FROM `ohrm_user_role_type`
                                 WHERE `name` = 'Admin');

SET @offer_info_entity_process_id := (SELECT `id`
                                      FROM `ohrm_advanced_workflow_entity_process`
                                      WHERE `name` = 'Offer Information');

-- 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`, `default_available_to`,
                             `module_id`, `order_no`)
VALUES ('PREBOARDING_INITIATE_PROCESS', 'PREBOARDING_INITIATE_PROCESS', 'Recruitment - Onboarding Initiate Process',
        'candidate', 'Candidate', '1', '1', @recruitment_module_id, @max_wf_order + 1);

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

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Candidate', 'PREBOARDING_INITIATE_PROCESS', 'Initiate Onboarding', 'Initiate Onboarding', 3,
        '{"icon":"oxd-people-management"}');

SET @initiate_pb_entity_process_id := (SELECT `id`
                                       FROM `ohrm_advanced_workflow_entity_process`
                                       WHERE `name` = 'Initiate Onboarding');

SET @max_entity_process_status_id :=
        (SELECT IFNULL((SELECT IFNULL(MAX(`status_id`), 0) FROM `ohrm_advanced_workflow_entity_process_status`), 0));

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

-- End: Workflow entity process configuration

-- Start: "Initiate Pre Boarding Process" Task configuration
INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `group_name`, `group_label`, `available_to`, `default_available_to`,
                             `module_id`, `order_no`)
VALUES ('PREBOARDING_INIT_PREBOARDING_PROCESS_TASK', 'PREBOARDING_INIT_PREBOARDING_PROCESS_TASK',
        'Onboarding - Initiate Onboarding Process Task', 'candidate', 'Candidate', '1', '1', @recruitment_module_id,
        @max_wf_order + 1);

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES ('PREBOARDING_INIT_PREBOARDING_PROCESS_TASK', 'Pending', 'HiringManager', 'Submit', 'Completed', '0'),
       ('PREBOARDING_INIT_PREBOARDING_PROCESS_TASK', 'Pending', 'Admin', 'Submit', 'Completed', '0'),
       ('PREBOARDING_INIT_PREBOARDING_PROCESS_TASK', 'Pending', 'PreboardingEventManager', 'Submit', 'Completed', '0'),
       ('PREBOARDING_INIT_PREBOARDING_PROCESS_TASK', 'Completed', 'HiringManager', 'Read', 'Completed', '0'),
       ('PREBOARDING_INIT_PREBOARDING_PROCESS_TASK', 'Completed', 'Admin', 'Read', 'Completed', '0'),
       ('PREBOARDING_INIT_PREBOARDING_PROCESS_TASK', 'Completed', 'PreboardingEventManager', 'Read', 'Completed', '0');

SET @form_version_id := (SELECT `id`
                         FROM `ohrm_dynamic_form_version`
                         WHERE `name` = 'Initiate Onboarding Process');

SET @task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @initiate_pb_entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('HiringManager', 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 ('Initiate Onboarding', 'Initiate the Onboarding process for %preboardingEmployeeName%', @recruitment_module_id,
        'InitiatePreboardingTaskMediator', 'PREBOARDING_INIT_PREBOARDING_PROCESS_TASK',
        @form_version_id, @task_options);

SET @task_template_id := (SELECT `id`
                          FROM ohrm_advanced_task_template
                          WHERE `name` = 'Initiate Onboarding');

-- Create the Initiate Onboarding Process task upon completing the the Offer Information process
INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`,
       'WorkflowStateChangeTaskCreator',
       JSON_OBJECT('task', JSON_OBJECT('templateId', @task_template_id))
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_OFFER_INFO_PROCESS'
  AND `state` = 'Pending Offer Information'
  AND `action` = 'Complete'
  AND `resulting_state` = 'Offer Information Completed';

-- Start: "Create Candidate Account" Task configuration
SET @max_wf_order := (SELECT MAX(`order_no`)
                      FROM `ohrm_workflow`);

INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `module_id`, `order_no`)
VALUES ('PREBOARDING_CREATE_CANDIDATE_ACCOUNT_TASK', 'PREBOARDING_CREATE_CANDIDATE_ACCOUNT_TASK',
        'Onboarding - Create Candidate Account Task', @recruitment_module_id, @max_wf_order + 1);

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES ('PREBOARDING_CREATE_CANDIDATE_ACCOUNT_TASK', 'Pending', 'SYSTEM', 'Create Candidate Account', 'Completed', 0);

SET @task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @initiate_pb_entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('SYSTEM', JSON_OBJECT()),
                        'generatorHelper', 'PreboardingTaskGeneratorHelper',
                        'executorHelper', 'CreateCandidateAccountTaskExecutorHelper',
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Completed',
                        'autoExecute', true
                ));

INSERT INTO `ohrm_advanced_task_template`
    (`name`, `description`, `module_id`, `mediator`, `workflow`, `options`)
VALUES ('Create Candidate Account', 'Create Candidate Account for %preboardingEmployeeName%', @recruitment_module_id,
        'CreateCandidateAccountTaskMediator', 'PREBOARDING_CREATE_CANDIDATE_ACCOUNT_TASK',
        @task_options);

SET @create_candidate_account_task_template_id := (SELECT `id`
                                                   FROM ohrm_advanced_task_template
                                                   WHERE `name` = 'Create Candidate Account');

-- Create the Create Candidate Account 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_candidate_account_task_template_id))
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_INIT_PREBOARDING_PROCESS_TASK'
  AND `state` = 'Pending'
  AND `action` = 'Submit'
  AND `resulting_state` = 'Completed';
-- End: "Create Candidate Account" Task configuration

-- Move the Initiate Onboarding to "Onboarding Initiation Completed" stage upon Completion of Candidate Account Creation Task
INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`,
       'TaskStateChangeWorkflowProcessStateChanger',
       JSON_OBJECT(
               'entityProcessId', @initiate_pb_entity_process_id,
               'action', 'Complete'
       )
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_CREATE_CANDIDATE_ACCOUNT_TASK'
  AND `state` = 'Pending'
  AND `action` = 'Create Candidate Account'
  AND `resulting_state` = 'Completed';

-- End: Initiate Onboarding Task configuration
