SET @candidate_user_role_id := (SELECT `id` FROM `ohrm_user_role` WHERE `name` = 'CandidateUser');

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

-- 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_CANDIDATE_WELCOME_PROCESS', 'PREBOARDING_CANDIDATE_WELCOME_PROCESS',
        'Recruitment - Onboarding Candidate Welcome', '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_CANDIDATE_WELCOME_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending Candidate Welcome', '0'),
       ('PREBOARDING_CANDIDATE_WELCOME_PROCESS', 'Pending Offer Information', 'SYSTEM', 'Complete', 'Candidate Welcome Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Candidate', 'PREBOARDING_CANDIDATE_WELCOME_PROCESS', 'Welcome', 'Candidate Welcome', 1,
        '{"icon":"oxd-people-management"}');

SET @candidate_welcome_process_id := (SELECT `id` FROM `ohrm_advanced_workflow_entity_process` WHERE `name` = 'Welcome');

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 (@candidate_welcome_process_id, @max_entity_process_status_id + 1, 'AWM_Initial',
        'Process to be initialized by the System', '0'),
       (@candidate_welcome_process_id, @max_entity_process_status_id + 2, 'Pending Candidate Welcome',
        'Initialized candidate welcome', '1'),
       (@candidate_welcome_process_id, @max_entity_process_status_id + 3, 'Candidate Welcome Completed',
        'Candidate welcome completed', '2');

-- End: Workflow entity process configuration

-- Start: "Candidate welcome Process" Task configuration

INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `default_available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_CANDIDATE_WELCOME_TASK', 'PREBOARDING_CANDIDATE_WELCOME_TASK',
        'Onboarding - Candidate Welcome Process Task', '1', @recruitment_module_id, @max_wf_order + 1);

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES ('PREBOARDING_CANDIDATE_WELCOME_TASK', 'Pending', 'CandidateUser', 'Submit','Completed', '0'),
('PREBOARDING_CANDIDATE_WELCOME_TASK', 'Completed', 'CandidateUser', 'Read','Completed', '0');

SET @form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Welcome Candidate');

SET @task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @candidate_welcome_process_id,
                        'ownerUserRoles', JSON_OBJECT('CandidateUser', 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 ('Welcome Candidate', 'Welcome %preboardingEmployeeName%', @recruitment_module_id,
        '', 'PREBOARDING_CANDIDATE_WELCOME_TASK',
        @form_version_id, @task_options);

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


-- Create the welcome Process task upon completing the the Initiate Onboarding

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_INITIATE_PROCESS'
  AND `state` = 'Pending Onboarding Initiation'
  AND `action` = 'Complete'
  AND `resulting_state` = 'Onboarding Initiation Completed';


-- Move the Candidate Welcome to "Candidate Welcome 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', @candidate_welcome_process_id,
               'action', 'Complete'
       )
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_CANDIDATE_WELCOME_TASK'
  AND `state` = 'Pending'
  AND `action` = 'Submit'
  AND `resulting_state` = 'Completed';

-- End: Candidate Welcome configuration
