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 ('Copy Payroll Details', 'Copy Payroll Details', @recruitment_module_id,
        '', NULL, NULL, @copy_preboarding_task_options);

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

SET @salary_custom_field_screen_id := (SELECT id FROM ohrm_custom_field_screen WHERE name = 'salary');
SET @payroll_detail_custom_field_section_id := (SELECT id FROM ohrm_custom_field_section WHERE custom_field_screen_id = @salary_custom_field_screen_id AND display_name = 'Payroll Details');

SET @payroll_custom_field_1 := (SELECT id FROM ohrm_custom_field WHERE name = 'Do you have a P45' AND deleted = 0);
SET @payroll_custom_field_2 := (SELECT id FROM ohrm_custom_field WHERE name = 'Select one of the below' AND deleted = 0);
SET @payroll_custom_field_3 := (SELECT id FROM ohrm_custom_field WHERE name = 'I have been receipt of Employment & Support Allowance (ESA) before starting work?' AND deleted = 0);
SET @payroll_custom_field_4 := (SELECT id FROM ohrm_custom_field WHERE name = 'Are you able to produce the linking letter ESA220 (or similar)?' AND deleted = 0);
SET @payroll_custom_field_5 := (SELECT id FROM ohrm_custom_field WHERE name = 'Do you have a student loan that is not fully repaid?' AND deleted = 0);
SET @payroll_custom_field_6 := (SELECT id FROM ohrm_custom_field WHERE name = 'Are you repaying the student loan direct to the Student Loans Company by agreed monthly payments and what is your student loan plan?' AND deleted = 0);
SET @payroll_custom_field_7 := (SELECT id FROM ohrm_custom_field WHERE name = 'Did you finish your studies before the last April?' AND deleted = 0);


SET @metadata := CONCAT('{
  "formName": "Payroll Details",
  "wizardDataTemplate": {
    "data": {
      "customFieldValue": {
        "', @payroll_detail_custom_field_section_id, '": {
          "', @payroll_custom_field_1, '": "${payroll_custom_field_1}",
          "', @payroll_custom_field_2,'": "${payroll_custom_field_2}",
          "', @payroll_custom_field_3,'": "${payroll_custom_field_3}",
          "', @payroll_custom_field_4, '": "${payroll_custom_field_4}",
          "', @payroll_custom_field_5, '": "${payroll_custom_field_5}",
          "', @payroll_custom_field_6, '": "${payroll_custom_field_6}",
          "', @payroll_custom_field_7, '": "${payroll_custom_field_7}"
        }
      }
    }
  }
}');

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);
