SET @recruitment_module_id := (SELECT `id`
                               FROM `ohrm_module`
                               WHERE `name` = 'recruitment');

SET @system_user_id := (SELECT `id`
                        FROM `ohrm_user`
                        WHERE `user_name` = 'System');

SET @system_user_role_id := (SELECT `id`
                             FROM `ohrm_user_role`
                             WHERE `name` = 'SYSTEM');

SET @admin_user_role_id := (SELECT `id`
                            FROM `ohrm_user_role`
                            WHERE `name` = 'Admin');

SET @hiring_manager_user_role_id := (SELECT `id`
                                     FROM `ohrm_user_role`
                                     WHERE `name` = 'HiringManager');

SET @candidate_user_role_id := (SELECT `id`
                                     FROM `ohrm_user_role`
                                     WHERE `name` = 'CandidateUser');

SET @admin_user_role_type_id := (SELECT `id`
                                 FROM `ohrm_user_role_type`
                                 WHERE `name` = 'Admin');

-- 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_OFFER_INFO_PROCESS', 'PREBOARDING_OFFER_INFO_PROCESS', 'Recruitment - Onboarding Offer Info',
        '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_OFFER_INFO_PROCESS', 'AWM_Initial', 'SYSTEM', 'AWM_Initialize', 'Pending Offer Information', '0'),
       ('PREBOARDING_OFFER_INFO_PROCESS', 'Pending Offer Information', 'SYSTEM', 'Complete', 'Offer Information Completed', '0');

INSERT INTO `ohrm_advanced_workflow_entity_process` (`entity`, `workflow_id`, `name`, `description`, `sequence`, `options`)
VALUES ('Candidate', 'PREBOARDING_OFFER_INFO_PROCESS', 'Offer Information', 'Offer Information', 2,
        '{"icon":"oxd-employee-about"}');

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

SET @offer_info_completed_status_id := (SELECT `id`
                                        FROM `ohrm_advanced_workflow_entity_process_status`
                                        WHERE `entity_process_id` = @offer_info_entity_process_id
                                          AND `name` = 'Offer Information Completed');

-- End: Workflow entity process configuration

-- Start: Offer Information Task 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_OFFER_INFO_TASK', 'PREBOARDING_OFFER_INFO_TASK', 'Onboarding - Offer Information Task',
        '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_OFFER_INFO_TASK', 'Pending', 'HiringManager', 'Submit', 'Submitted', '0'),
       ('PREBOARDING_OFFER_INFO_TASK', 'Pending', 'Admin', 'Submit', 'Submitted', '0'),
       ('PREBOARDING_OFFER_INFO_TASK', 'Pending', 'PreboardingEventManager', 'Submit', 'Submitted', '0'),
       ('PREBOARDING_OFFER_INFO_TASK', 'Submitted', 'Admin', 'Read', 'Submitted', '0'),
       ('PREBOARDING_OFFER_INFO_TASK', 'Submitted', 'HiringManager', 'Read', 'Submitted', '0'),
       ('PREBOARDING_OFFER_INFO_TASK', 'Submitted', 'CandidateUser', 'Read', 'Submitted', '0'),
       ('PREBOARDING_OFFER_INFO_TASK', 'Submitted', 'PreboardingEventManager', 'Read', 'Submitted', '0');

SET @offer_info_form_version_id := (SELECT `id`
                                    FROM `ohrm_dynamic_form_version`
                                    WHERE `name` = 'Job Offer Information');

SET @offer_info_task_options :=
        (SELECT JSON_OBJECT(
                        'entityProcessId', @offer_info_entity_process_id,
                        'ownerUserRoles', JSON_OBJECT('HiringManager', JSON_OBJECT()),
                        'sharedTask', true,
                        'sharedFormData', true,
                        'generatorHelper', 'FillInOfferInformationTaskGeneratorHelper',
                        'initialStatus', 'Pending',
                        'terminalStatus', 'Submitted'
                ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Fill-in Offer Information', 'Fill-in Offer Information for %preboardingEmployeeName%', @recruitment_module_id,
        'FillInOfferInformationTaskMediator', 'PREBOARDING_OFFER_INFO_TASK', @offer_info_form_version_id,
        @offer_info_task_options);

SET @offer_info_task_template_id := (SELECT `id`
                                     FROM ohrm_advanced_task_template
                                     WHERE `name` = 'Fill-in Offer Information');

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

-- Create Offer 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', @offer_info_task_template_id)));

-- Move the Offer Information to "Offer Information Completed" stage upon submitting the Offer Information
INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`,
       'TaskStateChangeWorkflowProcessStateChanger',
       JSON_OBJECT(
               'entityProcessId', @offer_info_entity_process_id,
               'action', 'Complete'
       )
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_OFFER_INFO_TASK'
  AND `state` = 'Pending'
  AND `action` = 'Submit'
  AND `resulting_state` = 'Submitted';

-- End: Offer Information Task configuration
