SET @preboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE `name` = 'preboarding');
SET @pro_module_id := (SELECT `id` FROM `ohrm_module` WHERE `name` = 'onboardingPro');
SET @data_group_id := (SELECT id FROM ohrm_data_group WHERE name = 'preboarding_qualifications');

-- 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`, `module_id`, `order_no`)
VALUES ('PREBOARDING_QUALIFICATIONS_PROCESS', 'PREBOARDING_QUALIFICATIONS_PROCESS', null,
        'preboarding', 'Preboarding', '1,3', @preboarding_module_id, @max_wf_order + 1);

INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES ('PREBOARDING_QUALIFICATIONS_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending', '0'),
       ('PREBOARDING_QUALIFICATIONS_PROCESS', 'Pending', 'SYSTEM', 'Complete', 'Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Preboarding Employee', 'PREBOARDING_QUALIFICATIONS_PROCESS', 'Qualifications', 'Submit Educational and Professional Qualifications', 10,
        '{"icon":"oxd-qualification"}');

SET @entity_process_id := (SELECT `id` FROM `ohrm_advanced_workflow_entity_process` WHERE `name` = 'Qualifications');
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 (@entity_process_id, @max_entity_process_status_id + 1 , 'AWM_Initial',
        'Process to be initialized by the System', '0'),
       (@entity_process_id, @max_entity_process_status_id + 2, 'Pending',
        'Qualifications are pending to be completed', '1'),
       (@entity_process_id, @max_entity_process_status_id + 3, 'Completed',
        'Qualifications are completed', '2');

-- End: Workflow entity process configuration

-- Start: Work Experience Task configuration
INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `group_name`, `group_label`, `available_to`, `default_available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_WORK_EXPERIENCE_TASK', 'PREBOARDING_WORK_EXPERIENCE_TASK', null,
        'preboarding', 'Preboarding', '1,3', '2', @preboarding_module_id, @max_wf_order + 2),
       ('PREBOARDING_EDUCATION_TASK', 'PREBOARDING_EDUCATION_TASK', null,
        'preboarding', 'Preboarding', '1,3', '2', @preboarding_module_id, @max_wf_order + 3),
       ('PREBOARDING_SKILL_TASK', 'PREBOARDING_SKILL_TASK', null,
        'preboarding', 'Preboarding', '1,3', '2', @preboarding_module_id, @max_wf_order + 4),
       ('PREBOARDING_LANGUAGE_TASK', 'PREBOARDING_LANGUAGE_TASK', null,
        'preboarding', 'Preboarding', '1,3', '2', @preboarding_module_id, @max_wf_order + 5),
       ('PREBOARDING_LICENSE_TASK', 'PREBOARDING_LICENSE_TASK', null,
        'preboarding', 'Preboarding', '1,3', '2', @preboarding_module_id, @max_wf_order + 6);


INSERT INTO `ohrm_workflow_state_machine` (`workflow`, `state`, `role`, `action`, `resulting_state`, `priority`)
VALUES
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'ESS User', 'Save', 'Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'PreboardingEventManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'Admin', 'Save', 'Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'HiringManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'Supervisor', 'Save', 'Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Pending', 'HeadOfDepartment', 'Save', 'Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'PreboardingNewHire', 'Read','Submitted', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'ESS User', 'Read','Submitted', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'Supervisor', 'Approve', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'Supervisor', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'HeadOfDepartment', 'Approve', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', 'HeadOfDepartment', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Approved', 'PreboardingNewHire', 'Read','Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Approved', 'ESS User', 'Read','Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Approved', 'Supervisor', 'Read', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Approved', 'HeadOfDepartment', 'Read', 'Approved', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'ESS User', 'Save','Pending', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'Supervisor', 'Read', 'Rejected', '0'),
    ('PREBOARDING_WORK_EXPERIENCE_TASK', 'Rejected', 'HeadOfDepartment', 'Read', 'Rejected', '0'),

    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'ESS User', 'Save', 'Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'PreboardingEventManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'Admin', 'Save', 'Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'HiringManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'Supervisor', 'Save', 'Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Pending', 'HeadOfDepartment', 'Save', 'Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'PreboardingNewHire', 'Read','Submitted', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'ESS User', 'Read','Submitted', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'Supervisor', 'Approve', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'Supervisor', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'HeadOfDepartment', 'Approve', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Submitted', 'HeadOfDepartment', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Approved', 'PreboardingNewHire', 'Read','Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Approved', 'ESS User', 'Read','Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Approved', 'Supervisor', 'Read', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Approved', 'HeadOfDepartment', 'Read', 'Approved', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'ESS User', 'Save','Pending', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'Supervisor', 'Read', 'Rejected', '0'),
    ('PREBOARDING_EDUCATION_TASK', 'Rejected', 'HeadOfDepartment', 'Read', 'Rejected', '0'),

    ('PREBOARDING_SKILL_TASK', 'Pending', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Pending', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_SKILL_TASK', 'Pending', 'ESS User', 'Save', 'Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Pending', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_SKILL_TASK', 'Pending', 'PreboardingEventManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Pending', 'Admin', 'Save', 'Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Pending', 'HiringManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Pending', 'Supervisor', 'Save', 'Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Pending', 'HeadOfDepartment', 'Save', 'Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'PreboardingNewHire', 'Read','Submitted', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'ESS User', 'Read','Submitted', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'Supervisor', 'Approve', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'Supervisor', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'HeadOfDepartment', 'Approve', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Submitted', 'HeadOfDepartment', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Approved', 'PreboardingNewHire', 'Read','Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Approved', 'ESS User', 'Read','Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Approved', 'Supervisor', 'Read', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Approved', 'HeadOfDepartment', 'Read', 'Approved', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'ESS User', 'Save','Pending', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'Supervisor', 'Read', 'Rejected', '0'),
    ('PREBOARDING_SKILL_TASK', 'Rejected', 'HeadOfDepartment', 'Read', 'Rejected', '0'),

    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'ESS User', 'Save', 'Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'PreboardingEventManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'Admin', 'Save', 'Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'HiringManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'Supervisor', 'Save', 'Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Pending', 'HeadOfDepartment', 'Save', 'Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'PreboardingNewHire', 'Read','Submitted', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'ESS User', 'Read','Submitted', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'Supervisor', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'Supervisor', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'HeadOfDepartment', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Submitted', 'HeadOfDepartment', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Approved', 'PreboardingNewHire', 'Read','Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Approved', 'ESS User', 'Read','Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Approved', 'Supervisor', 'Read', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Approved', 'HeadOfDepartment', 'Read', 'Approved', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'ESS User', 'Save','Pending', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'Supervisor', 'Read', 'Rejected', '0'),
    ('PREBOARDING_LANGUAGE_TASK', 'Rejected', 'HeadOfDepartment', 'Read', 'Rejected', '0'),

    ('PREBOARDING_LICENSE_TASK', 'Pending', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Pending', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Pending', 'ESS User', 'Save', 'Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Pending', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Pending', 'PreboardingEventManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Pending', 'Admin', 'Save', 'Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Pending', 'HiringManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Pending', 'Supervisor', 'Save', 'Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Pending', 'HeadOfDepartment', 'Save', 'Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'PreboardingNewHire', 'Read','Submitted', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'ESS User', 'Read','Submitted', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'Supervisor', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'Supervisor', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'HeadOfDepartment', 'Approve', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Submitted', 'HeadOfDepartment', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Approved', 'PreboardingNewHire', 'Read','Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Approved', 'ESS User', 'Read','Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Approved', 'Supervisor', 'Read', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Approved', 'HeadOfDepartment', 'Read', 'Approved', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'ESS User', 'Save','Pending', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'Supervisor', 'Read', 'Rejected', '0'),
    ('PREBOARDING_LICENSE_TASK', 'Rejected', 'HeadOfDepartment', 'Read', 'Rejected', '0');

-- End: License Task configuration

SET @qualification_task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('Admin', JSON_OBJECT()),
                        'sharedTask', true,
                        'sharedFormData', true,
                        'generatorHelper', 'PreboardingTaskGeneratorHelper',
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Completed'
                ));

SET @work_experience_task_options :=
    (SELECT JSON_OBJECT(
        'entityProcessId', @entity_process_id,
        'ownerUserRoles', JSON_OBJECT('Admin', JSON_OBJECT()),
        'sharedTask', true,
        'sharedFormData', true,
        'generatorHelper', 'FillInFormRecordsTaskGeneratorHelper',
        'extraData', JSON_OBJECT(
            'formFillMapping', JSON_OBJECT(
                'oneToManyEntityGetter', 'getWorkExperience',
                'formObjectName', 'work_experience',
                'fieldMap', JSON_OBJECT(
                        'company', JSON_OBJECT('fieldValueGetter','getEmployer'),
                        'job_title', JSON_OBJECT('fieldValueGetter','getJobTitle'),
                        'from_date', JSON_OBJECT('fieldValueGetter','getFromDate', 'formatter', 'set_datepicker_date_format'),
                        'to_date', JSON_OBJECT('fieldValueGetter','getToDate', 'formatter', 'set_datepicker_date_format'),
                        'comment', JSON_OBJECT('fieldValueGetter','getComments')
                )
            )
        ),
        'initialStatus', 'Pending',
        'terminalStatus', 'Pending'
    ));

SET @education_task_options :=
    (SELECT JSON_OBJECT(
        'entityProcessId', @entity_process_id,
        'ownerUserRoles', JSON_OBJECT('Admin', JSON_OBJECT()),
        'sharedTask', true,
        'sharedFormData', true,
        'generatorHelper', 'FillInFormRecordsTaskGeneratorHelper',
        'extraData', JSON_OBJECT(
            'formFillMapping', JSON_OBJECT(
                'oneToManyEntityGetter', 'getEmployeeEducation',
                'formObjectName', 'education',
                'multiValuedFields', 'level',
                'fieldMap', JSON_OBJECT(
                    'level', JSON_OBJECT('relatedEntityGetter','getEducation', 'idGetter', 'getId', 'entityClass','Education'),
                    'institute', JSON_OBJECT('fieldValueGetter','getInstitute'),
                    'major_specialization', JSON_OBJECT('fieldValueGetter','getMajor'),
                    'year', JSON_OBJECT('fieldValueGetter','getYear'),
                    'gpa_score', JSON_OBJECT('fieldValueGetter','getScore'),
                    'start_date', JSON_OBJECT('fieldValueGetter','getStartDate'),
                    'end_date', JSON_OBJECT('fieldValueGetter','getEndDate')
                )
            )
        ),
        'initialStatus', 'Pending',
        'terminalStatus', 'Completed'
    )
);

SET @skill_task_options :=
    (SELECT JSON_OBJECT(
                    'entityProcessId', @entity_process_id,
                    'ownerUserRoles', JSON_OBJECT('Admin', JSON_OBJECT()),
                    'sharedTask', true,
                    'sharedFormData', true,
                    'generatorHelper', 'FillInFormRecordsTaskGeneratorHelper',
                    'extraData', JSON_OBJECT(
                        'formFillMapping', JSON_OBJECT(
                            'oneToManyEntityGetter', 'getSkills',
                            'formObjectName', 'skill',
                            'multiValuedFields', 'skill',
                            'fieldMap', JSON_OBJECT(
                                'skill', JSON_OBJECT('relatedEntityGetter','getSkill', 'idGetter', 'getId', 'entityClass','Skill'),
                                'years_of_experience', JSON_OBJECT('fieldValueGetter','getYearsOfExp'),
                                'comment', JSON_OBJECT('fieldValueGetter','getComments')
                            )
                        )
                    ),
                    'initialStatus', 'Pending',
                    'terminalStatus', 'Completed'
    )
);

SET @languages_task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('Admin', JSON_OBJECT()),
                        'sharedTask', true,
                        'sharedFormData', true,
                        'generatorHelper', 'FillInFormRecordsTaskGeneratorHelper',
                        'extraData', JSON_OBJECT(
                            'formFillMapping', JSON_OBJECT(
                                'oneToManyEntityGetter', 'getLanguages',
                                'formObjectName', 'language',
                                'multiValuedFields', 'language,skill,fluency_level',
                                'fieldMap', JSON_OBJECT(
                                    'language', JSON_OBJECT('relatedEntityGetter','getLanguage', 'idGetter', 'getId', 'entityClass','Language'),
                                    'skill', JSON_OBJECT('relatedEntityGetter','getCompetency','labelValueGetter','getFluencyAsString'),
                                    'fluency_level', JSON_OBJECT('relatedEntityGetter','getFluency','labelValueGetter','getCompetencyAsString'),
                                    'comment', JSON_OBJECT('fieldValueGetter','getComments')
                                )
                            )
                        ),
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Completed'
                ));

SET @license_task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('Admin', JSON_OBJECT()),
                        'sharedTask', true,
                        'sharedFormData', true,
                        'generatorHelper', 'FillInFormRecordsTaskGeneratorHelper',
                        'extraData', JSON_OBJECT(
                            'formFillMapping', JSON_OBJECT(
                                'oneToManyEntityGetter', 'getEmployeeLicense',
                                'formObjectName', 'license',
                                'multiValuedFields', 'license_type',
                                'fieldMap', JSON_OBJECT(
                                    'license_type', JSON_OBJECT('relatedEntityGetter','getLicense', 'idGetter', 'getId', 'entityClass','License'),
                                    'license_number', JSON_OBJECT('fieldValueGetter','getLicenseNo'),
                                    'issued_date', JSON_OBJECT('fieldValueGetter','getLicenseIssuedDate'),
                                    'expiry_date', JSON_OBJECT('fieldValueGetter','getLicenseExpiryDate')
                                )
                            )
                        ),
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Completed'
                ));


SET @work_experience_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding Work Experience');
SET @education_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding Education');
SET @skill_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding Skills');
SET @languages_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding Language');
SET @license_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding License');

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Work Experience', 'Work Experience for %preboardingEmployeeName%', @pro_module_id,
        '', 'PREBOARDING_WORK_EXPERIENCE_TASK', @work_experience_form_version_id,
        @work_experience_task_options),
       ('Educations', 'Educations for %preboardingEmployeeName%', @pro_module_id,
        '', 'PREBOARDING_EDUCATION_TASK', @education_form_version_id,
        @education_task_options),
       ('Skills', 'Skills for %preboardingEmployeeName%', @pro_module_id,
        '', 'PREBOARDING_SKILL_TASK', @skill_form_version_id,
        @skill_task_options),
       ('Languages', 'Languages for %preboardingEmployeeName%', @pro_module_id,
        '', 'PREBOARDING_LANGUAGE_TASK', @languages_form_version_id,
        @languages_task_options),
       ('License', 'License for %preboardingEmployeeName%', @pro_module_id,
        '', 'PREBOARDING_LICENSE_TASK', @license_form_version_id,
        @license_task_options);

-- End: License Task configuration
SET @work_experience_task_template_id := (SELECT `id` FROM ohrm_advanced_task_template WHERE `name` = 'Work Experience');
SET @educations_task_template_id := (SELECT `id` FROM ohrm_advanced_task_template WHERE `name` = 'Educations');
SET @skills_task_template_id := (SELECT `id` FROM ohrm_advanced_task_template WHERE `name` = 'Skills');
SET @languages_task_template_id := (SELECT `id` FROM ohrm_advanced_task_template WHERE `name` = 'Languages');
SET @license_task_template_id := (SELECT `id` FROM ohrm_advanced_task_template WHERE `name` = 'License');
SET @preboarding_initiation_wfsm_id := (SELECT `id`
                                        FROM `ohrm_workflow_state_machine`
                                        WHERE `workflow` = 'PREBOARDING_QUALIFICATIONS_PROCESS'
                                          AND `state` = 'AWM_Initial'
                                          AND `role` = 'SYSTEM'
                                          AND `action` = 'AWM_Initialize'
                                          AND `resulting_state` = 'Pending');

INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`) VALUES
      (@preboarding_initiation_wfsm_id, 'WorkflowStateChangeTaskCreator',
       JSON_OBJECT('task', JSON_OBJECT('templateId', @work_experience_task_template_id))),
      (@preboarding_initiation_wfsm_id, 'WorkflowStateChangeTaskCreator',
       JSON_OBJECT('task', JSON_OBJECT('templateId', @educations_task_template_id))),
      (@preboarding_initiation_wfsm_id, 'WorkflowStateChangeTaskCreator',
       JSON_OBJECT('task', JSON_OBJECT('templateId', @skills_task_template_id))),
      (@preboarding_initiation_wfsm_id, 'WorkflowStateChangeTaskCreator',
       JSON_OBJECT('task', JSON_OBJECT('templateId', @languages_task_template_id))),
      (@preboarding_initiation_wfsm_id, 'WorkflowStateChangeTaskCreator',
       JSON_OBJECT('task', JSON_OBJECT('templateId', @license_task_template_id)));

INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`,
       'TaskStateChangeWorkflowProcessStateChangerForProcessesWithMultipleTasks',
       JSON_OBJECT(
               'entityProcessId', @entity_process_id,
               'action', 'Complete',
               'requiredState', 'Approved',
               'requiredTaskCount', '5'
       )
FROM `ohrm_workflow_state_machine`
WHERE `workflow` IN ('PREBOARDING_WORK_EXPERIENCE_TASK',
                     'PREBOARDING_EDUCATION_TASK',
                     'PREBOARDING_SKILL_TASK',
                     'PREBOARDING_LANGUAGE_TASK',
                     'PREBOARDING_LICENSE_TASK')
  AND `state` = 'Submitted'
  AND `action` = 'Approve'
  AND `resulting_state` = 'Approved';

SET @master_state_change_id := (SELECT `id` FROM `ohrm_workflow_state_machine` WHERE `workflow` = 'PREBOARDING_QUALIFICATIONS_PROCESS' AND `role` = 'SYSTEM' AND `action` = 'Complete');
SET @slave_state_change_id := (SELECT `id` FROM `ohrm_workflow_state_machine` WHERE `workflow` = 'PREBOARDING_EMPLOYEE_ENTITY' AND `role` = 'SYSTEM' AND `action` = 'Complete');
INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`, `options`)
VALUES (@master_state_change_id, @slave_state_change_id, 'AdvancedWorkflowProcessToRequestStateChangeTrigger',
        '{"validateSameStateForAllProcesses":true,"considerOnlyEnabledProcesses":true}');

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)
SELECT 'PREBOARDING_WORK_EXPERIENCE_TASK', 'Submitted', user_role_id, data_group_id, 1, 0, 0, 0
FROM `ohrm_user_role_data_group` where data_group_id = @data_group_id AND can_read = 1
                                   AND user_role_id NOT IN (SELECT id FROM ohrm_user_role WHERE name IN ('PreboardingNewHire', 'ESS'));

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)
SELECT 'PREBOARDING_EDUCATION_TASK', 'Submitted', user_role_id, data_group_id, 1, 0, 0, 0
FROM `ohrm_user_role_data_group` where data_group_id = @data_group_id AND can_read = 1
                                   AND user_role_id NOT IN (SELECT id FROM ohrm_user_role WHERE name IN ('PreboardingNewHire', 'ESS'));

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)
SELECT 'PREBOARDING_SKILL_TASK', 'Submitted', user_role_id, data_group_id, 1, 0, 0, 0
FROM `ohrm_user_role_data_group` where data_group_id = @data_group_id AND can_read = 1
                                   AND user_role_id NOT IN (SELECT id FROM ohrm_user_role WHERE name IN ('PreboardingNewHire', 'ESS'));

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)
SELECT 'PREBOARDING_LANGUAGE_TASK', 'Submitted', user_role_id, data_group_id, 1, 0, 0, 0
FROM `ohrm_user_role_data_group` where data_group_id = @data_group_id AND can_read = 1
                                   AND user_role_id NOT IN (SELECT id FROM ohrm_user_role WHERE name IN ('PreboardingNewHire', 'ESS'));

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)
SELECT 'PREBOARDING_LICENSE_TASK', 'Submitted', user_role_id, data_group_id, 1, 0, 0, 0
FROM `ohrm_user_role_data_group` where data_group_id = @data_group_id AND can_read = 1
                                   AND user_role_id NOT IN (SELECT id FROM ohrm_user_role WHERE name IN ('PreboardingNewHire', 'ESS'));