/*
 * 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 @module_id := (SELECT `id`
                               FROM `ohrm_module`
                               WHERE `name` = 'onboardingPro');
-- Work Experience
SET @work_experience_tab_screen_id := (SELECT id
                              FROM `ohrm_screen`
                              WHERE `name` = 'Qualifications');
SET @work_experience_tab_id := (SELECT id
                FROM `ohrm_menu_item`
                WHERE `menu_title` = 'Qualifications'
                  AND screen_id = @work_experience_tab_screen_id
                  AND status = 1);

SET @wizard_screen_id := (SELECT id
                FROM `ohrm_wizard_screen`
                WHERE `title` = 'Qualifications');

SET @work_experience_wizard_section_id := (SELECT id
                FROM `ohrm_wizard_section`
                WHERE `wizard_screen_id` = @wizard_screen_id AND name = 'PimWorkExperience');

SET @copy_preboarding_forms_task_template_id := (SELECT id FROM ohrm_advanced_task_template WHERE name = 'Copy Form data to Multiple tab' LIMIT 1);

SET @work_experience_metadata := CONCAT('{
  "formName": "Preboarding Work Experience",
  "wizardDataTemplate": {
    "sectionId": "@section_id",
    "wizardSectionName": "workExperience",
    "data": {
      "workExperience": {
        "employer": "${company}",
        "jobtitle": "${job_title}",
        "from_date": "${from_date}",
        "to_date": "${to_date}",
        "comments": "${comment}",
        "seqno": "${index}"
      },
      "actions":{
        "endpoint": "employees/<EMPNUMBER>/workExperience",
        "method": "POST",
        "data": {
            "employer": "${company}",
            "jobtitle": "${job_title}",
            "from_date": "${from_date}",
            "to_date": "${to_date}",
            "comments": "${comment}"
        },
        "extra": {
            "workExperienceId": "${index}"
        },
        "params": {}
      }
    }
  },
  "config":{
      "emptyValueMap": {
        "employer":"",
        "jobtitle":"",
        "from_date":null,
        "to_date":null,
        "comments":""
      },
      "aggregate_column": "work_experience"
   }
}');

SET @work_experience_metadata = REPLACE(@work_experience_metadata, '@section_id', @work_experience_wizard_section_id);

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', @work_experience_metadata);

-- Education
SET @education_tab_screen_id := (SELECT id
                              FROM `ohrm_screen`
                              WHERE `name` = 'Education List');
SET @education_tab_id := (SELECT id
                FROM `ohrm_menu_item`
                WHERE `menu_title` = 'Education'
                  AND screen_id = @education_tab_screen_id
                  AND status = 1);

SET @education_wizard_section_id := (SELECT id
                FROM `ohrm_wizard_section`
                WHERE `wizard_screen_id` = @wizard_screen_id AND name = 'PimEducation');

SET @education_metadata := CONCAT('{
  "formName": "Preboarding Education",
  "wizardDataTemplate": {
    "sectionId": "@section_id",
    "wizardSectionName": "education",
    "data": {
      "education": {
        "institute": "${institute}",
        "major": "${major_specialization}",
        "year": "${year}",
        "score": "${gpa_score}",
        "startDate": "${start_date}",
        "endDate": "${end_date}",
        "educationId": "${level#id}",
        "id": "${index}",
        "Education": {
           "id": "${level#id}",
           "name": "${level#label}"
        }
      },
      "actions":{
        "endpoint": "employees/<EMPNUMBER>/education",
        "method": "POST",
        "data": {
            "institute": "${institute}",
            "major": "${major_specialization}",
            "year": "${year}",
            "score": "${gpa_score}",
            "startDate": "${start_date}",
            "endDate": "${end_date}",
            "educationId": "${level}"
        },
        "extra": {
            "educationId": "${index}"
        },
        "params": {}
      }
    }
  },
  "config":{
      "emptyValueMap": {
        "institute": "",
        "major": "",
        "year": null,
        "score": "",
        "startDate": null,
        "endDate": null,
        "educationId": null
      },
      "aggregate_column": "education"
   }
}');

SET @education_metadata = REPLACE(@education_metadata, '@section_id', @education_wizard_section_id);

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', @education_metadata);

-- Skill
SET @skill_tab_screen_id := (SELECT id
                              FROM `ohrm_screen`
                              WHERE `name` = 'Skill List');
SET @skill_tab_id := (SELECT id
                FROM `ohrm_menu_item`
                WHERE `menu_title` = 'Skills'
                  AND screen_id = @skill_tab_screen_id
                  AND status = 1);
SET @skill_wizard_section_id := (SELECT id
                FROM `ohrm_wizard_section`
                WHERE `wizard_screen_id` = @wizard_screen_id AND name = 'PimSkill');

SET @skill_metadata := CONCAT('{
  "formName": "Preboarding Skills",
  "wizardDataTemplate": {
    "sectionId": "@section_id",
    "wizardSectionName": "skill",
    "data": {
      "skill": {
        "skillId": "${skill#id}",
        "years_of_exp": "${years_of_experience}",
        "comments": "${comment}",
        "id": "${index}",
        "Skill": {
           "id": "${skill#id}",
           "name": "${skill#label}"
        }
      },
      "actions":{
        "endpoint": "employees/<EMPNUMBER>/skills",
        "method": "POST",
        "data": {
            "skillId": "${skill#id}",
            "years_of_exp": "${years_of_experience}",
            "comments": "${comment}"
        },
        "extra": {
            "skillId": "${index}"
        },
        "params": {}
      }
    }
  },
  "config":{
      "emptyValueMap": {
        "skillId": "",
        "years_of_exp": null,
        "comments": ""
      },
      "aggregate_column": "skill"
   }
}');

SET @skill_metadata = REPLACE(@skill_metadata, '@section_id', @skill_wizard_section_id);

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', @skill_metadata);

-- Language
SET @language_tab_screen_id := (SELECT id
                              FROM `ohrm_screen`
                              WHERE `name` = 'Language List');
SET @language_tab_id := (SELECT id
                FROM `ohrm_menu_item`
                WHERE `menu_title` = 'Languages'
                  AND screen_id = @language_tab_screen_id
                  AND status = 1);

SET @language_wizard_section_id := (SELECT id
                FROM `ohrm_wizard_section`
                WHERE `wizard_screen_id` = @wizard_screen_id AND name = 'PimLanguage');

SET @language_metadata := CONCAT('{
  "formName": "Preboarding Language",
  "wizardDataTemplate": {
    "sectionId": "@section_id",
    "wizardSectionName": "language",
    "data": {
      "language": {
        "langId": "${language#id}",
        "competency": "${fluency_level#id}",
        "fluency": "${skill#id}",
        "comments": "${comment}",
        "Language": {
           "id": "${language#id}",
           "name": "${language#label}"
        }
      },
      "actions":{
        "endpoint": "employees/<EMPNUMBER>/languages",
        "method": "POST",
        "data": {
            "langId": "${language#id}",
            "competency": "${fluency_level#id}",
            "fluency": "${skill#id}",
            "comments": "${comment}"
        },
        "extra": {
            "languageId": "${index}"
        },
        "params": {}
      }
    }
  },
  "config":{
      "emptyValueMap": {
        "langId": "",
        "competency": "",
        "fluency": "",
        "comments": ""
      },
      "aggregate_column": "language",
      "index_generator_keys": [
            "language#id",
            "skill#id"
      ]
   }
}');

SET @language_metadata = REPLACE(@language_metadata, '@section_id', @language_wizard_section_id);

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', @language_metadata);

-- License
SET @license_tab_screen_id := (SELECT id
                              FROM `ohrm_screen`
                              WHERE `name` = 'License List');
SET @license_tab_id := (SELECT id
                FROM `ohrm_menu_item`
                WHERE `menu_title` = 'Licenses'
                  AND screen_id = @license_tab_screen_id
                  AND status = 1);

    SET @license_wizard_section_id := (SELECT id
                FROM `ohrm_wizard_section`
                WHERE `wizard_screen_id` = @wizard_screen_id AND name = 'PimLicense');

SET @license_metadata := CONCAT('{
  "formName": "Preboarding License",
  "wizardDataTemplate": {
    "sectionId": "@section_id",
    "wizardSectionName": "license",
    "data": {
      "license": {
        "licenseId": "${license_type#id}",
        "licenseNo": "${license_number}",
        "licenseIssuedDate": "${issued_date}",
        "licenseExpiryDate": "${expiry_date}",
        "License": {
           "id": "${license_type#id}",
           "name": "${license_type#label}"
        }
      },
      "actions":{
        "endpoint": "employees/<EMPNUMBER>/EmployeeLicense",
        "method": "POST",
        "data": {
            "licenseId": "${license_type#id}",
            "licenseNo": "${license_number}",
            "licenseIssuedDate": "${issued_date}",
            "licenseExpiryDate": "${expiry_date}"
        },
        "extra": {
            "licenseId": "${license_type#id}"
        },
        "params": {}
      }
    }
  },
  "config":{
      "emptyValueMap": {
        "licenseId": null,
        "licenseNo": "",
        "licenseIssuedDate": null,
        "licenseExpiryDate": null
      },
      "aggregate_column": "license",
      "index_generator_keys": [
        "license_type#id"
      ]
   }
}');

SET @license_metadata = REPLACE(@license_metadata, '@section_id', @license_wizard_section_id);

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', @license_metadata);
