/*
 * OrangeHRM Enterprise is a closed sourced comprehensive Human Resource Management (HRM)
 * System that captures all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM Inc is the owner of the patent, copyright, trade secrets, trademarks and any
 * other intellectual property rights which subsist in the Licensed Materials. OrangeHRM Inc
 * is the owner of the media / downloaded OrangeHRM Enterprise software files on which the
 * Licensed Materials are received. Title to the Licensed Materials and media shall remain
 * vested in OrangeHRM Inc. For the avoidance of doubt title and all intellectual property
 * rights to any design, new software, new protocol, new interface, enhancement, update,
 * derivative works, revised screen text or any other items that OrangeHRM Inc creates for
 * Customer shall remain vested in OrangeHRM Inc. Any rights not expressly granted herein are
 * reserved to OrangeHRM Inc.
 *
 * Please refer http://www.orangehrm.com/Files/OrangeHRM_Commercial_License.pdf for the license which includes terms and conditions on using this software.
 *
 */

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

SET @copy_preboarding_task_options :=
        (SELECT JSON_OBJECT(
                        'executorHelper', 'CopyFormRecordToWizardSessionDataExecutionHelper'
                    ));

INSERT INTO `ohrm_advanced_task_template`
(`name`, `description`, `module_id`, `mediator`, `workflow`, `form_version_id`, `options`)
VALUES ('Personal info Forms to Employee', 'Copy Preboarding Forms to Employee', @recruitment_module_id,
        '', NULL, NULL, @copy_preboarding_task_options);

SET @copy_preboarding_forms_task_template_id := (SELECT LAST_INSERT_ID());

SET @job_standard_custom_field_screen_id := (SELECT id FROM ohrm_custom_field_screen WHERE name = 'job-standard');
SET @job_custom_field_screen_id := (SELECT id FROM ohrm_custom_field_screen WHERE name = 'job');

SET @job_other_details_section_id := (SELECT id FROM ohrm_custom_field_section WHERE custom_field_screen_id = @job_standard_custom_field_screen_id);
SET @nurse_info_custom_field_section_id := (SELECT id FROM ohrm_custom_field_section WHERE display_name = 'Nurses Information' AND custom_field_screen_id = @job_custom_field_screen_id);

SET @nmc_pin_number := (SELECT id FROM ohrm_custom_field WHERE name = 'NMC Pin Number' AND custom_field_section_id = @nurse_info_custom_field_section_id AND deleted = 0);
SET @nmc_qualification := (SELECT id FROM ohrm_custom_field WHERE name = 'NMC Qualification' AND custom_field_section_id = @nurse_info_custom_field_section_id AND deleted = 0);
SET @expiry_date := (SELECT id FROM ohrm_custom_field WHERE name = 'Expiry Date' AND custom_field_section_id = @nurse_info_custom_field_section_id AND deleted = 0);

SET @metadata := CONCAT('{
  "formName": "Personal Information",
  "wizardDataTemplate": {
    "data": {
      "employee": {
        "firstName": "${first_name}",
        "middleName": "${middle_name}",
        "lastName": "${last_name}",
        "emp_birthday": "${date_of_birth}",
        "ssn": "${ni_number}",
        "emp_gender": "${gender}"
      },
      "customFieldValue": {
          "', @nurse_info_custom_field_section_id, '": {
          "', @nmc_pin_number, '": "${nmc_pin_number}",
          "', @nmc_qualification,'": "${nmc_qualification}",
          "', @expiry_date,'": "${nmc_expiry_date}"
        }
      }
    }
  }
}');

INSERT INTO `ohrm_advanced_task` (task_template_id, name, description, metadata)
VALUES (@copy_preboarding_forms_task_template_id, 'pre_add_candidate_as_employee',
        'task run at when candidate added as employee to copy preboarding form information to employee', @metadata);
