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_CONSENT_PROCESS', 'PREBOARDING_CANDIDATE_CONSENT_PROCESS',
        'Recruitment - Onboarding Candidate Candidate consent', '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_CONSENT_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending Candidate consent', '0'),
       ('PREBOARDING_CANDIDATE_CONSENT_PROCESS', 'Pending Candidate consent', 'SYSTEM', 'Complete', 'Candidate consent Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Candidate', 'PREBOARDING_CANDIDATE_CONSENT_PROCESS', 'Consent', 'Consent', 12,
        '{"icon":"oxd-job"}');

SET @candidate_personal_info_process_id := (SELECT `id` FROM `ohrm_advanced_workflow_entity_process` WHERE `name` = 'Consent' and workflow_id = 'PREBOARDING_CANDIDATE_CONSENT_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 Candidate consent',
        'Pending Candidate consent', '1'),
       (@candidate_personal_info_process_id, @max_entity_process_status_id + 3, 'Candidate consent Completed',
        'Candidate consent Completed', '2');

-- End: Workflow entity process configuration


-- Start: "Candidate Candidate Consent" Task configuration

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

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

SET @task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @candidate_personal_info_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 ('Consent', 'Fill-in Candidate Consent for %preboardingEmployeeName%', @recruitment_module_id,
        '', 'PREBOARDING_CANDIDATE_CONSENT_TASK',
        @form_version_id, @task_options);

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


-- Create the candidate consent 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  consent to "Candidate consent Completed" stage upon submitting the Candidate Consent

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_CONSENT_TASK'
  AND `state` = 'Submitted'
  AND `action` = 'Approve'
  AND `resulting_state` = 'Approved';

-- End: Candidate consent configuration
