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_DBS_DETAIL_PROCESS', 'PREBOARDING_CANDIDATE_DBS_DETAIL_PROCESS',
        'Recruitment - Onboarding Candidate DBS Detail', '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_DBS_DETAIL_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending DBS Detail', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_PROCESS', 'Pending DBS Detail', 'SYSTEM', 'Complete', 'DBS Detail Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Candidate', 'PREBOARDING_CANDIDATE_DBS_DETAIL_PROCESS', 'DBS Details', 'DBS Details', 7,
        '{"icon":"oxd-job"}');

SET @candidate_process_id := (SELECT `id` FROM `ohrm_advanced_workflow_entity_process` WHERE `name` = 'DBS Details' and workflow_id = 'PREBOARDING_CANDIDATE_DBS_DETAIL_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_process_id, @max_entity_process_status_id + 1, 'AWM_Initial',
        'Process to be initialized by the System', '0'),
       (@candidate_process_id, @max_entity_process_status_id + 2, 'Pending DBS Detail',
        'Pending DBS Detail', '1'),
       (@candidate_process_id, @max_entity_process_status_id + 3, 'DBS Detail Completed',
        'DBS Detail Completed', '2');

-- End: Workflow entity process configuration


-- Start: "Candidate DBS Detail" Task configuration

INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `default_available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'PREBOARDING_CANDIDATE_DBS_DETAIL_TASK',
        'Onboarding - Candidate DBS Detail 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_DBS_DETAIL_TASK', 'Pending', 'CandidateUser', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Pending', 'Admin', 'Read','Pending', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Pending', 'HiringManager', 'Read','Pending', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Pending', 'PreboardingEventManager', 'Read','Pending', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Submitted', 'CandidateUser', 'Read','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Approved', 'CandidateUser', 'Read','Approved', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Rejected', 'CandidateUser', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_DBS_DETAIL_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0');

SET @form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Disclosure and Barring Service Details');

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

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('DBS Detail', 'Fill-in DBS Details for %preboardingEmployeeName%', @recruitment_module_id,
        '', 'PREBOARDING_CANDIDATE_DBS_DETAIL_TASK',
        @form_version_id, @task_options);

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


-- Create the DBS detail 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 DBS to "DBS Detail Completed" stage upon approve the DBS detail

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

-- End: Candidate DBS Detail configuration
