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_PERMISSION_EXAMPLE_PROCESS', 'PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_PROCESS',
        'Recruitment - Onboarding Candidate Permission Example', '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_PERMISSION_EXAMPLE_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending Permission Example', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_PROCESS', 'Pending Permission Example', 'SYSTEM', 'Complete', 'Permission Example Completed', '0');

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

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

-- End: Workflow entity process configuration


-- Start: "Candidate Permission Example" Task configuration

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

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

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', 'CandidateUser', 'Submit','PendingStep1', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', 'CandidateUser', 'Submit','PendingStep2', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', 'CandidateUser', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', 'CandidateUser', 'Read','Pending', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', 'CandidateUser', 'Read','PendingStep1', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', 'CandidateUser', 'Read','PendingStep2', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'CandidateUser', 'Read','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Rejected', 'CandidateUser', 'Read','Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Rejected', 'CandidateUser', 'Submit','Submitted', '0'),

       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', 'Admin', 'Submit','PendingStep1', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', 'Admin', 'Submit','PendingStep2', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', 'Admin', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', 'Admin', 'Read','Pending', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', 'Admin', 'Read','PendingStep1', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', 'Admin', 'Read','PendingStep2', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'Admin', 'Reject','Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'Admin', 'Read','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Rejected', 'Admin', 'Read','Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Rejected', 'Admin', 'Submit','Submitted', '0'),

       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', 'HiringManager', 'Submit','PendingStep1', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', 'HiringManager', 'Submit','PendingStep2', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', 'HiringManager', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', 'HiringManager', 'Read','Pending', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', 'HiringManager', 'Read','PendingStep1', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', 'HiringManager', 'Read','PendingStep2', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'HiringManager', 'Reject','Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'HiringManager', 'Read','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Rejected', 'HiringManager', 'Read','Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Rejected', 'HiringManager', 'Submit','Submitted', '0'),

       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', 'PreboardingEventManager', 'Submit','PendingStep1', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', 'PreboardingEventManager', 'Submit','PendingStep2', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', 'PreboardingEventManager', 'Submit','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', 'PreboardingEventManager', 'Read','Pending', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', 'PreboardingEventManager', 'Read','PendingStep1', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', 'PreboardingEventManager', 'Read','PendingStep2', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'PreboardingEventManager', 'Reject','Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Submitted', 'PreboardingEventManager', 'Read','Submitted', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Rejected', 'PreboardingEventManager', 'Read','Rejected', '0'),
       ('PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Rejected', 'PreboardingEventManager', 'Submit','Submitted', '0');

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', 'Submitted'
                    ));

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

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


-- 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 "Permission Example Completed" stage upon submitting the Permission Example

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_PERMISSION_EXAMPLE_TASK'
  AND `state` = 'PendingStep2'
  AND `action` = 'Submit'
  AND `resulting_state` = 'Submitted';

-- End: Candidate personal info configuration

-- Start data field permission for form
SET @form_data_group_id := (SELECT id FROM ohrm_data_group WHERE name = 'recruitment_preboarding_permission_example');

-- 'Pending' -> Editable: first_name, middle_name, Read-only: last_name, date_of_birth, ni_number, gender
INSERT INTO ohrm_workflow_state_data_field_permission(workflow_id, workflow_state, user_role_id, data_field_id, can_read, can_create, can_update, can_delete, self)
SELECT 'PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'Pending', urdf.user_role_id, df.id, 1, 0, 0, 0, 0
FROM ohrm_data_field df
    LEFT JOIN ohrm_user_role_data_field urdf ON df.id = urdf.data_field_id
WHERE df.data_group_id = @form_data_group_id AND df.name IN ('last_name', 'date_of_birth', 'ni_number', 'gender');

-- 'PendingStep1' -> Editable: last_name, date_of_birth, Read-only: first_name, middle_name, ni_number, gender
INSERT INTO ohrm_workflow_state_data_field_permission(workflow_id, workflow_state, user_role_id, data_field_id, can_read, can_create, can_update, can_delete, self)
SELECT 'PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep1', urdf.user_role_id, df.id, 1, 0, 0, 0, 0
FROM ohrm_data_field df
         LEFT JOIN ohrm_user_role_data_field urdf ON df.id = urdf.data_field_id
WHERE df.data_group_id = @form_data_group_id AND df.name IN ('first_name', 'middle_name', 'ni_number', 'gender');

-- 'PendingStep2' -> Editable: ni_number, gender Read-only: first_name, middle_name, last_name, date_of_birth
INSERT INTO ohrm_workflow_state_data_field_permission(workflow_id, workflow_state, user_role_id, data_field_id, can_read, can_create, can_update, can_delete, self)
SELECT 'PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep2', urdf.user_role_id, df.id, 1, 0, 0, 0, 0
FROM ohrm_data_field df
         LEFT JOIN ohrm_user_role_data_field urdf ON df.id = urdf.data_field_id
WHERE df.data_group_id = @form_data_group_id AND df.name IN ('first_name', 'middle_name', 'last_name', 'date_of_birth');

-- 'Submitted' -> Editable: None Read-only: first_name, middle_name, last_name, date_of_birth, ni_number, gender
INSERT INTO ohrm_workflow_state_data_field_permission(workflow_id, workflow_state, user_role_id, data_field_id, can_read, can_create, can_update, can_delete, self)
SELECT 'PREBOARDING_CANDIDATE_PERMISSION_EXAMPLE_TASK', 'PendingStep3', urdf.user_role_id, df.id, 1, 0, 0, 0, 0
FROM ohrm_data_field df
         LEFT JOIN ohrm_user_role_data_field urdf ON df.id = urdf.data_field_id
WHERE df.data_group_id = @form_data_group_id AND df.name IN ('first_name', 'middle_name', 'last_name', 'date_of_birth', 'ni_number', 'gender');

-- 'Rejected' -> Editable: first_name, middle_name, last_name, date_of_birth, ni_number, gender Read-only: None
-- No configuration needed.

-- End data field permission for form
