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

-- 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_SELF_INTRODUCTION_PROCESS', 'PREBOARDING_SELF_INTRODUCTION_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_SELF_INTRODUCTION_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_PROCESS', 'Pending', 'SYSTEM', 'Complete', 'Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Preboarding Employee', 'PREBOARDING_SELF_INTRODUCTION_PROCESS', 'Self Introduction', 'Submit Self Introduction', 2,
        '{"icon":"oxd-self-intro"}');

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

-- End: Workflow entity process configuration

-- Start: Self Introduction Task configuration
INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `group_name`, `group_label`, `available_to`, `default_available_to`, `module_id`, `order_no`)
VALUES ('PREBOARDING_SELF_INTRODUCTION_TASK', 'PREBOARDING_SELF_INTRODUCTION_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_SELF_INTRODUCTION_TASK', 'Pending', 'HiringManager', 'Read', 'Pending', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Pending', 'Admin', 'Read', 'Pending', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Pending', 'Supervisor', 'Read', 'Pending', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Pending', 'HeadOfDepartment', 'Read', 'Pending', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Pending', 'PreboardingEventManager', 'Read', 'Pending', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Pending', 'PreboardingNewHire', 'Submit', 'Submitted', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Pending', 'ESS User', 'Submit', 'Submitted', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Submitted', 'Admin', 'Save', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Submitted', 'Supervisor', 'Save', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Submitted', 'HeadOfDepartment', 'Save', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Submitted', 'HiringManager', 'Save', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Submitted', 'PreboardingEventManager', 'Save', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Submitted', 'PreboardingNewHire', 'Read', 'Submitted', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Submitted', 'ESS User', 'Read', 'Submitted', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Completed', 'Admin', 'Read', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Completed', 'Supervisor', 'Read', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Completed', 'HeadOfDepartment', 'Read', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Completed', 'HiringManager', 'Read', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Completed', 'PreboardingEventManager', 'Read', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Completed', 'PreboardingNewHire', 'Read', 'Completed', '0'),
       ('PREBOARDING_SELF_INTRODUCTION_TASK', 'Completed', 'ESS User', 'Read', 'Completed', '0');

SET @self_introduction_form_version_id := (SELECT `id` FROM `ohrm_dynamic_form_version` WHERE `name` = 'Preboarding Self Introduction');

SET @self_introduction_task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @self_introduction_entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('Admin', JSON_OBJECT()),
                        'sharedTask', true,
                        'sharedFormData', true,
                        'generatorHelper', 'PreboardingTaskGeneratorHelper',
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Submitted'
                ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Self Introduction Submission', 'Self Introduction Submission for %preboardingEmployeeName%', @preboarding_module_id,
        '', 'PREBOARDING_SELF_INTRODUCTION_TASK', @self_introduction_form_version_id,
        @self_introduction_task_options);

SET @self_introduction_task_template_id := (SELECT `id` FROM ohrm_advanced_task_template WHERE `name` = 'Self Introduction Submission');

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

-- Create Self Introduction 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', @self_introduction_task_template_id)));

-- Move the Self Introduction to "Completed" stage upon submitting the Self Introduction
INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`,
       'TaskStateChangeWorkflowProcessStateChanger',
       JSON_OBJECT(
               'entityProcessId', @self_introduction_entity_process_id,
               'action', 'Complete'
       )
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_SELF_INTRODUCTION_TASK'
  AND `state` = 'Submitted'
  AND `action` = 'Save'
  AND `resulting_state` = 'Completed';

-- End: Self Introduction Task configuration

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