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

-- 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_PERSONAL_DETAILS_PROCESS', 'PREBOARDING_PERSONAL_DETAILS_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_PERSONAL_DETAILS_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending', '0'),
       ('PREBOARDING_PERSONAL_DETAILS_PROCESS', 'Pending', 'SYSTEM', 'Complete', 'Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Preboarding Employee', 'PREBOARDING_PERSONAL_DETAILS_PROCESS', 'Personal Details', 'Submit Personal Details', 5,
        '{"icon":"oxd-personal"}');

SET @entity_process_id := (SELECT `id` FROM `ohrm_advanced_workflow_entity_process` WHERE `name` = 'Personal Details');
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',
        'Personal Details are pending to be completed', '1'),
       (@entity_process_id, @max_entity_process_status_id + 3, 'Completed',
        'Personal Details are completed', '2');

-- End: Workflow entity process configuration

-- Start: Personal Details Task configuration
INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `group_name`, `group_label`, `available_to`, `default_available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_PERSONAL_DETAILS_TASK', 'PREBOARDING_PERSONAL_DETAILS_TASK', null,
        'preboarding', 'Preboarding', '1,3', '2', @preboarding_module_id, @max_wf_order + 2);

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

-- End: Personal Details Task configuration

SET @personal_details_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding Personal Details');

SET @personal_details_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(
                            'multiValuedFields', 'gender,marital_status,nationality',
                            'fieldMap', JSON_OBJECT(
                                'first_name', JSON_OBJECT('fieldValueGetter','getFirstName'),
                                'middle_name', JSON_OBJECT('fieldValueGetter','getMiddleName'),
                                'last_name', JSON_OBJECT('fieldValueGetter','getLastName'),
                                'date_of_birth', JSON_OBJECT('fieldValueGetter','getEmpBirthday'),
                                'gender', JSON_OBJECT('relatedEntityGetter','getGender', 'idGetter', 'getId', 'entityClass','Gender'),
                                'marital_status', JSON_OBJECT('relatedEntityGetter','getMaritalStatus', 'idGetter', 'getId', 'entityClass','MaritalStatus'),
                                'nationality', JSON_OBJECT('relatedEntityGetter','getNationality', 'idGetter', 'getId', 'entityClass','Nationality'),
                                'driver_license_number', JSON_OBJECT('fieldValueGetter','getLicenseNo'),
                                'license_expiry_date', JSON_OBJECT('fieldValueGetter','getEmpDriLiceExpDate')
                                )
                            )
                        ),
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Completed'
                ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Personal Details', 'Personal Details for %preboardingEmployeeName%', @pro_module_id,
        '', 'PREBOARDING_PERSONAL_DETAILS_TASK', @personal_details_form_version_id,
        @personal_details_task_options);

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

SET @preboarding_initiation_wfsm_id := (SELECT `id`
                                        FROM `ohrm_workflow_state_machine`
                                        WHERE `workflow` = 'PREBOARDING_PERSONAL_DETAILS_PROCESS'
                                          AND `state` = 'AWM_Initial'
                                          AND `role` = 'SYSTEM'
                                          AND `action` = 'AWM_Initialize'
                                          AND `resulting_state` = 'Pending');

-- Create Personal Details Submission task upon moving the Candidate to the Onboarding stage
INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
VALUES (@preboarding_initiation_wfsm_id, 'WorkflowStateChangeTaskCreator',
        JSON_OBJECT('task', JSON_OBJECT('templateId', @personal_details_task_template_id)));
-- End: Personal Details Task configuration

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

SET @data_group_id := (SELECT id FROM ohrm_data_group WHERE name = 'preboarding_personal_details');


SET @master_state_change_id := (SELECT `id` FROM `ohrm_workflow_state_machine` WHERE `workflow` = 'PREBOARDING_PERSONAL_DETAILS_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_PERSONAL_DETAILS_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'));
