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_PERSONAL_INFO_PROCESS', 'PREBOARDING_CANDIDATE_PERSONAL_INFO_PROCESS',
        'Recruitment - Onboarding Candidate Personal information', '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_PERSONAL_INFO_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending Personal info', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_PROCESS', 'Pending Personal info', 'SYSTEM', 'Complete', 'Personal info Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Candidate', 'PREBOARDING_CANDIDATE_PERSONAL_INFO_PROCESS', 'Personal Information', 'Personal Information', 4,
        '{"icon":"oxd-job"}');

SET @candidate_personal_info_process_id := (SELECT `id` FROM `ohrm_advanced_workflow_entity_process` WHERE `name` = 'Personal Information' and workflow_id = 'PREBOARDING_CANDIDATE_PERSONAL_INFO_PROCESS');

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_personal_info_process_id, @max_entity_process_status_id + 1, 'AWM_Initial',
        'Process to be initialized by the System', '0'),
       (@candidate_personal_info_process_id, @max_entity_process_status_id + 2, 'Pending Personal info',
        'Pending Personal info', '1'),
       (@candidate_personal_info_process_id, @max_entity_process_status_id + 3, 'Personal info Completed',
        'Personal info Completed', '2');

-- End: Workflow entity process configuration


-- Start: "Candidate Personal Information" Task configuration

INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK',
        'Onboarding - Candidate Personal info 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_PERSONAL_INFO_TASK', 'Pending', 'CandidateUser', 'Save','Pending', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Pending', 'CandidateUser', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Submitted', 'CandidateUser', 'Read','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Approved', 'CandidateUser', 'Read','Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Rejected', 'CandidateUser', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Pending', 'Admin', 'Save', 'Pending', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Pending', 'HiringManager', 'Save', 'Pending', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Pending', 'PreboardingEventManager', 'Save', 'Pending', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Rejected', 'Admin', 'Save', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Rejected', 'HiringManager', 'Save', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK', 'Rejected', 'PreboardingEventManager', 'Save', 'Rejected', '0');

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

SET @task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @candidate_personal_info_process_id,
                        'ownerUserRoles', JSON_OBJECT('CandidateUser', JSON_OBJECT()),
                        'sharedTask', true,
                        'sharedFormData', true,
                        'generatorHelper', 'FillCandidatePersonalInfoTaskGeneratorHelper',
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Approved'
                ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Personal Information', 'Fill-in Personal Information for %preboardingEmployeeName%', @recruitment_module_id,
        '', 'PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK',
        @form_version_id, @task_options);

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


-- Create the personal info Process task upon completing the the Initiate Preboarding

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 personal info to "Personal info Completed" stage upon submitting the Personal Information

INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`,
       'TaskStateChangeWorkflowProcessStateChanger',
       JSON_OBJECT(
               'entityProcessId', @candidate_personal_info_process_id,
               'action', 'Complete'
       )
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK'
  AND `state` = 'Submitted'
  AND `action` = 'Approve'
  AND `resulting_state` = 'Approved';

-- End: Candidate personal info configuration

SET @data_group_id := (SELECT id FROM ohrm_data_group WHERE name = 'recruitment_preboarding_personal_information');

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 @preboarding_event_manager_user_role_id := (SELECT `id`
                                     FROM `ohrm_user_role`
                                     WHERE `name` = 'PreboardingEventManager');

INSERT INTO ohrm_workflow_state_data_group_permission(workflow_id, workflow_state, user_role_id, data_group_id, can_read, can_create, can_update, can_delete) VALUES
('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK','Submitted',@admin_user_role_id,@data_group_id,1,0,0,0),
('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK','Submitted',@hiring_manager_user_role_id,@data_group_id,1,0,0,0),
('PREBOARDING_CANDIDATE_PERSONAL_INFO_TASK','Submitted',@preboarding_event_manager_user_role_id,@data_group_id,1,0,0,0);
