INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('recruitment_preboarding_candidate_consent', 'Recruitment (ATS) - Onboarding - Candidate Consent', 1, 0, 1, 0);

SET @data_group_id := (SELECT LAST_INSERT_ID());
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 @preboarding_event_manager_user_role_id := (SELECT `id`
                                     FROM `ohrm_user_role`
                                     WHERE `name` = 'PreboardingEventManager');

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

INSERT INTO ohrm_user_role_data_group (user_role_id, data_group_id, can_read, can_create, can_update, can_delete, self,
                                       restriction_id)
VALUES (@admin_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
       (@hiring_manager_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
       (@candidate_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
       (@hiring_manager_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
       (@preboarding_event_manager_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL);

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

INSERT INTO ohrm_user_role_type_data_group (user_role_type_id, data_group_id, can_read, can_create, can_update,
                                            can_delete)
VALUES (@admin_user_role_type, @data_group_id, 1, 0, 1, 0);


INSERT INTO ohrm_dynamic_form (`name`, description, `action`, endpoint, meta_data, data_group_id, created_by,
                               updated_by, created_by_name, version)
VALUES ('Consent', 'Consent', 'POST', 'preboarding-candidate-consent', null, @data_group_id,
        @system_user_id, @system_user_id, 'System', 1);
SET @dynamic_form_id := (SELECT `id`
                         FROM ohrm_dynamic_form
                         WHERE name = 'Consent');

INSERT INTO ohrm_dynamic_form_version (`name`, description, `action`, endpoint, meta_data, data_group_id, created_by,
                                       updated_by, created_by_name, version, dynamic_form_id)
VALUES ('Consent', 'Consent', 'POST',  'preboarding-candidate-consent', null, @data_group_id,
        @system_user_id, @system_user_id, 'System', 1, @dynamic_form_id);
SET @dynamic_form_version_id := (SELECT `id`
                                 FROM ohrm_dynamic_form_version
                                 WHERE name = 'Consent');

SET @metadata := CONCAT('
{
  "version": {
    "schema-version": "1.0.0"
  },
  "fields": [
    {
      "uid": "photographic_consent_1001",
      "name": "photographic_consent",
      "widget": "checkbox",
      "label": "",
      "attr": {
        "required": true,
        "optionLabel": "I consent to the use of my photograph",
        "trueValue": "Yes",
        "falseValue": "No"
      },
      "validations": [
      ]
    },
    {
      "uid": "third_party_consent_1002",
      "name": "third_party_consent",
      "widget": "checkbox",
      "label": "",
      "attr": {
        "required": true,
        "optionLabel": "I consent to my data being used by third parties",
        "trueValue": "Yes",
        "falseValue": "No"
      },
      "validations": [
      ]
    },
    {
      "uid": "working_time_regulation_1003",
      "name": "working_time_regulation",
      "widget": "checkbox",
      "label": "",
      "attr": {
        "required": true,
        "optionLabel": "I consent to the working time regulations",
        "trueValue": "Yes",
        "falseValue": "No"
      },
      "validations": [
      ]
    }
  ],
  "views": {
    "form": {
      "uid": "form010",
      "action": "POST",
      "endpoint": "forms/10",
      "body": [
        {
          "uid": "form010_title",
          "tag": "oxd-text",
          "props": {
            "type": "card-title"
          },
          "slots": {
            "default": [
              "Consent"
            ]
          }
        },
        {
          "uid": "form010_br",
          "tag": "br"
        },
        {
          "uid": "form010_grid",
          "tag": "oxd-grid",
          "props": {
            "cols": 2,
            "style": "margin-left: -10px;margin-right: -10px;"
          },
          "slots": {
            "default": [
              {
                "uid": "form010_grid_item_1",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "photographic_consent_1001"
                    }
                  ]
                }
              },
              {
                "uid": "form010_grid_item_2",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "third_party_consent_1002"
                    }
                  ]
                }
              },
              {
                "uid": "form010_grid_item_3",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "working_time_regulation_1003"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "uid": "form010_divider",
          "tag": "oxd-divider",
          "props": {
            "style": "margin-bottom: 1rem;"
          }
        },
        {
          "uid": "form010_buttons",
          "tag": "div",
          "props": {
            "style": "display: flex; justify-content: space-between;margin-top: 1rem"
          },
          "slots": {
              "default": [
                {
                  "uid": "form010_button_save",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Save",
                    "display-type": "secondary",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSaveButton"
                  }
                },
                {
                  "uid": "form010_button_submit",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Submit",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSubmitButton"
                  }
                },
                {
                  "uid": "form010_button_reject",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Reject",
                    "display-type": "danger",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickRejectButton"
                  }
                },
                {
                  "uid": "form010_button_approve",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Approve",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickApproveButton"
                  }
                }
              ]
            }
        }
      ],
      "model": {
        "photographic_consent": "",
        "third_party_consent": "",
        "working_time_regulation": ""
      },
      "computed": {},
      "methods": {
        "onClickSubmitButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Consent will be submitted.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#decodeB64Files": {
            "model": ["photo"]
          },
          "#save": {
            "action": "Submit"
          },
          "#toast": {
            "error": "Failed to Submit",
            "success": "Successfully Submitted"
          }
        },
        "onClickSaveButton": {
          "#validate": {},
          "#decodeB64Files": {
            "model": ["photo"]
          },
          "#save": {
             "action": "Save"
          },
          "#toast": {
              "error": "Failed to Save",
              "success": "Successfully Saved"
          }
        },
        "onClickApproveButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Consent will be Approved.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Approve",
            "skipFormData": true
          },
          "#toast": {
            "error": "Failed to Approve",
            "success": "Consent has been Approved"
          }
        },
        "onClickRejectButton": {
          "#validate": {},
          "#prompt": {
            "title": "Rejection Confirmation",
            "confirmDisplayType": "label-feedback-danger",
            "confirmText": "Reject",
            "inputType": "textarea",
            "inputLabel": "Comment"
          },
          "#save": {
            "action": "Reject",
            "skipFormData": true
          },
          "#toast": {
            "error": "Failed to Reject",
            "success": "Consent has been Rejected"
          }
        }
      }
    }
  }
}
');

UPDATE ohrm_dynamic_form
SET meta_data = @metadata
WHERE id = @dynamic_form_id;
UPDATE ohrm_dynamic_form_version
SET meta_data = @metadata
WHERE id = @dynamic_form_version_id;
