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_REFERENCE_DETAILS_PROCESS', 'PREBOARDING_CANDIDATE_REFERENCE_DETAILS_PROCESS',
        'Recruitment - Onboarding Candidate Reference details', '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_REFERENCE_DETAILS_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending Reference details', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_PROCESS', 'Pending Reference details', 'SYSTEM', 'Complete', 'Reference details Completed', '0');

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

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

-- End: Workflow entity process configuration


-- Start: "Candidate Reference Details" Task configuration

INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `default_available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK',
        'Onboarding - Candidate Reference details 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_REFERENCE_DETAILS_TASK', 'Pending', 'CandidateUser', 'Save','Pending', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending', 'CandidateUser', 'Submit','Pending Approval', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending', 'Admin', 'Read', 'Pending', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending', 'HiringManager', 'Read', 'Pending', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending', 'PreboardingEventManager', 'Read', 'Pending', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Approval', 'CandidateUser', 'Read','Pending Approval', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Approval', 'Admin', 'Approve', 'Pending Initiation', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Approval', 'HiringManager', 'Approve', 'Pending Initiation', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Approval', 'PreboardingEventManager', 'Approve', 'Pending Initiation', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Approval', 'Admin', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Approval', 'HiringManager', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Approval', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Initiation', 'CandidateUser', 'Read','Pending Initiation', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Initiation', 'Admin', 'Initiate', 'Initiated', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Initiation', 'HiringManager', 'Initiate', 'Initiated', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Pending Initiation', 'PreboardingEventManager', 'Initiate', 'Initiated', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Initiated', 'CandidateUser', 'Read','Initiated', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Initiated', 'Admin', 'Read','Initiated', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Initiated', 'HiringManager', 'Read','Initiated', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Initiated', 'PreboardingEventManager', 'Read','Initiated', '0'),
#        ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Initiated', 'Referee', 'Save','Initiated', '0'),
#        ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Initiated', 'Referee', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Submitted', 'CandidateUser', 'Read', 'Submitted', '0'),
#        ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Submitted', 'Referee', 'Read', 'Submitted', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Approved', 'CandidateUser', 'Read','Approved', '0'),
#        ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Approved', 'Referee', 'Read','Approved', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Rejected', 'CandidateUser', 'Read','Rejected', '0'),
#        ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Rejected', 'Referee', 'Read','Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
       ('PREBOARDING_CANDIDATE_REFERENCE_DETAILS_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0');

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

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

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


-- 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),
               'count', 2
       )
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 "Reference details Completed" stage upon submitting the Reference Details

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

-- End: Candidate personal info configuration
