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_DEPENDENT_INFORMATION_PROCESS', 'PREBOARDING_DEPENDENT_INFORMATION_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_DEPENDENT_INFORMATION_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending', '0'),
       ('PREBOARDING_DEPENDENT_INFORMATION_PROCESS', 'Pending', 'SYSTEM', 'Complete', 'Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Preboarding Employee', 'PREBOARDING_DEPENDENT_INFORMATION_PROCESS', 'Dependent Information', 'Submit Details of Dependents', 8,
        '{"icon":"oxd-dependents"}');

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

-- End: Workflow entity process configuration

-- Start: Dependent Information Task configuration
INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `group_name`, `group_label`, `available_to`, `default_available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'PREBOARDING_DEPENDENT_INFORMATION_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_DEPENDENT_INFORMATION_TASK', 'Pending', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Pending', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Pending', 'ESS User', 'Save', 'Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Pending', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Pending', 'PreboardingEventManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Pending', 'Admin', 'Save', 'Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Pending', 'HiringManager', 'Save', 'Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Pending', 'Supervisor', 'Save', 'Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Pending', 'HeadOfDepartment', 'Save', 'Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'PreboardingNewHire', 'Read','Submitted', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'ESS User', 'Read','Submitted', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'Admin', 'Approve', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'Admin', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'PreboardingEventManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'PreboardingEventManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'HiringManager', 'Approve', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'HiringManager', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'Supervisor', 'Approve', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'Supervisor', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'HeadOfDepartment', 'Approve', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Submitted', 'HeadOfDepartment', 'Reject', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Approved', 'PreboardingNewHire', 'Read','Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Approved', 'ESS User', 'Read','Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Approved', 'Admin', 'Read', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Approved', 'PreboardingEventManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Approved', 'HiringManager', 'Read', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Approved', 'Supervisor', 'Read', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Approved', 'HeadOfDepartment', 'Read', 'Approved', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'PreboardingNewHire', 'Submit','Submitted', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'PreboardingNewHire', 'Save','Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'ESS User', 'Submit','Submitted', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'ESS User', 'Save','Pending', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'Admin', 'Read', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'PreboardingEventManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'HiringManager', 'Read', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'Supervisor', 'Read', 'Rejected', '0'),
    ('PREBOARDING_DEPENDENT_INFORMATION_TASK', 'Rejected', 'HeadOfDepartment', 'Read', 'Rejected', '0');

SET @dependent_information_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding Dependent Information');

SET @dependent_information_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', 'getDependents',
                                'formObjectName', 'dependent_information',
                                'multiValuedFields', 'relationship',
                                'fieldMap', JSON_OBJECT(
                                    'name', JSON_OBJECT('fieldValueGetter','getName'),
                                    'date_of_birth', JSON_OBJECT('fieldValueGetter','getDateOfBirth'),
                                    'relationship', JSON_OBJECT('relatedEntityGetter','getRelationshipType','labelValueGetter','getRelationshipType'),
                                    'relationship_type', JSON_OBJECT('fieldValueGetter','getRelationship', 'ifNoValue', null)
                                )
                            )
                        ),
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Completed'
                ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Dependent Information', 'Dependent Information for %preboardingEmployeeName%', @pro_module_id,
        '', 'PREBOARDING_DEPENDENT_INFORMATION_TASK', @dependent_information_form_version_id,
        @dependent_information_task_options);

SET @dependent_information_task_template_id := (SELECT `id` FROM ohrm_advanced_task_template WHERE `name` = 'Dependent Information');

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

-- Create Dependent Information 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', @dependent_information_task_template_id)));
-- End: Dependent Information 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_DEPENDENT_INFORMATION_TASK'
  AND `state` = 'Submitted'
  AND `action` = 'Approve'
  AND `resulting_state` = 'Approved';

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

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