SET @template_id := (SELECT id
                     FROM ohrm_advanced_task_template
                     WHERE name = 'Push form data to a configured API endpoint'
                     LIMIT 1);

SET @entity_process_id := (SELECT `id`
                           FROM `ohrm_advanced_workflow_entity_process`
                           WHERE `name` = 'Memberships'
                             and workflow_id = 'PREBOARDING_MEMBERSHIPS_PROCESS');

SET @task_template_id := (SELECT `id`
    FROM ohrm_advanced_task_template WHERE
    `name` = 'Memberships' AND workflow = 'PREBOARDING_MEMBERSHIPS_TASK');


SET @form_version_id := (SELECT `id`
                                 FROM ohrm_dynamic_form_version
                                 WHERE name = 'Preboarding Memberships');

INSERT INTO `ohrm_advanced_task` (task_template_id, name, description, metadata)
VALUES (@template_id, 'Copy Memberships form data to Employee',
        'Copy Memberships form data to Employee upon form approval', '{}');


SET @metadata := CONCAT('{
  "entityProcessId": ', @entity_process_id, ',
  "taskTemplateId": ', @task_template_id, ',
  "formVersionId": ', @form_version_id, ',
  "dataTransfer": [
    {
      "processorClass": "PreboardingToPimMultiTabDataTransferProcessor",
      "formDataSortColumn": "name",
      "removeFieldsIfEmptyForMethod": "post",
      "get": {
        "endpointClass": "OrangeHRM\\\\Api\\\\Pim\\\\EmployeeMembershipsEndpoint",
        "endpointVersion": 1,
        "params": {
          "request": {
            "emp_number": "${empNumber}"
          }
        },
        "aggregate_column": "preboarding_memberships"
      },
      "delete": {
        "endpointClass": "OrangeHRM\\\\Api\\\\Pim\\\\EmployeeMembershipsEndpoint",
        "endpointVersion": 1,
        "uniqueRecordIdentifierInGet": "membershipId",
        "params": {
          "request": {
            "emp_number": "${emp_number}"
          }
        }
      },
      "post": {
        "endpointClass": "OrangeHRM\\\\Api\\\\Pim\\\\EmployeeMembershipsEndpoint",
        "endpointVersion": 1,
        "params": {
          "request": {
            "emp_number": "${emp_number}"
          },
          "body": {
             "membershipId": "${membership#id}",
             "subscriptionFee": "${subscription_amount}",
             "subscriptionCurrency": "${currency#id}",
             "subscriptionCommenceDate": "${subscription_commence_date}",
             "subscriptionRenewalDate": "${subscription_renewal_date}"
          }
        }
      }
    }
  ]
}');

SET @task_id := (SELECT `id`
                 FROM `ohrm_advanced_task`
                 WHERE `name` = 'Copy Memberships form data to Employee'
                   AND `task_template_id` = @template_id);

UPDATE `ohrm_advanced_task`
SET `metadata` = @metadata
WHERE `id` = @task_id;

-- Copy relevant information from "Memberships" form to the employee profile upon approving the form

INSERT INTO `ohrm_workflow_state_change_resolver` (`state_machine_id`, `resolver`, `options`)
SELECT `id`, 'TaskStateChangeTaskExecutorResolver', JSON_OBJECT('taskId', @task_id)
FROM `ohrm_workflow_state_machine`
WHERE `workflow` = 'PREBOARDING_MEMBERSHIPS_TASK'
  AND `state` = 'Submitted'
  AND `action` = 'Approve'
  AND `resulting_state` = 'Approved';

