INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('recruitment_preboarding_permission_example', 'Recruitment (ATS) - Onboarding - Permission Example', 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),
       (@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_data_field (`name`, `label`, `data_group_id`, `can_read`, `can_create`, `can_update`, `can_delete`)
VALUES
    ('first_name', 'First Name', @data_group_id, 1, 0, 1, 0),
    ('middle_name', 'Middle Name', @data_group_id, 1, 0, 1, 0),
    ('last_name', 'Last Name', @data_group_id, 1, 0, 1, 0),
    ('date_of_birth', 'Date of Birth', @data_group_id, 1, 0, 1, 0),
    ('ni_number', 'NI Number', @data_group_id, 1, 0, 1, 0),
    ('gender', 'Gender', @data_group_id, 1, 0, 1, 0);

INSERT INTO ohrm_user_role_data_field (user_role_id, data_field_id, can_read, can_update)
SELECT urdg.user_role_id, df.id, urdg.can_read, urdg.can_update
    FROM ohrm_user_role_data_group urdg
        LEFT JOIN ohrm_data_field df ON df.data_group_id = urdg.data_group_id
    WHERE urdg.data_group_id = @data_group_id ORDER BY urdg.user_role_id, df.id;

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

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 ('Permission Example', 'Permission Example', 'POST', 'preboarding-permission-example', 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 = 'Permission Example');

SET @metadata := CONCAT('
{
  "version": {
    "schema-version": "1.0.0"
  },
  "fields": [
    {
      "uid": "first_name_1001",
      "name": "first_name",
      "widget": "text",
      "label": "First Name",
      "attr": {
        "required": true
      },
      "validations": [
        "#required",
        "#length:0:30"
      ]
    },
    {
      "uid": "middle_name_1002",
      "name": "middle_name",
      "widget": "text",
      "label": "Middle Name",
      "validations": [
        "#length:0:30"
      ]
    },
    {
      "uid": "last_name_1003",
      "name": "last_name",
      "widget": "text",
      "label": "Last Name",
      "attr": {
        "required": true
      },
      "validations": [
        "#required",
        "#length:0:30"
      ]
    },
    {
      "uid": "date_of_birth_1004",
      "name": "date_of_birth",
      "widget": "datepicker",
      "label": "Date Of Birth",
      "attr": {
        "required": true
      },
      "validations": [
        "#required",
        "#date_format:yyyy-mm-dd",
        "#date"
      ]
    },
    {
      "uid": "ni_number_1005",
      "name": "ni_number",
      "widget": "text",
      "label": "NI Number",
      "attr": {
        "required": true
      },
      "validations": [
        "#required",
        "#length:0:30"
      ]
    },
    {
      "uid": "gender_1006",
      "name": "gender",
      "label": "Gender",
      "widget": "oxd-master-data-input",
      "attr": {
        "service": "Gender",
        "required": true
      },
      "validations": [
        "#required",
        "#masterDataSelect:Gender"
      ]
    }
  ],
  "views": {
    "form": {
      "uid": "form001",
      "action": "POST",
      "endpoint": "forms/1",
      "body": [
        {
          "uid": "form001_title",
          "tag": "oxd-text",
          "props": {
            "type": "card-title"
          },
          "slots": {
            "default": [
              "Permission Example"
            ]
          }
        },
        {
          "uid": "form001_br",
          "tag": "br"
        },
        {
          "uid": "form001_grid",
          "tag": "oxd-grid",
          "props": {
            "cols": 2,
            "style": "margin-left: -10px;margin-right: -10px;"
          },
          "slots": {
            "default": [
              {
                "uid": "form001_grid_item_1",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "first_name_1001"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_2",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "middle_name_1002"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_3",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "last_name_1003"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_4",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "date_of_birth_1004"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_5",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "ni_number_1005"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_6",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "gender_1006"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "uid": "form007_divider",
          "tag": "oxd-divider",
          "props": {
            "style": "margin-bottom: 1rem;"
          }
        },
        {
          "uid": "form001_buttons",
          "tag": "div",
          "props": {
            "style": "display: flex; justify-content: space-between;margin-top: 1rem"
          },
          "slots": {
              "default": [
                {
                  "uid": "form007_required_text",
                  "tag": "oxd-required-text"
                },
                {
                  "uid": "form001_button_save",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Save",
                    "display-type": "secondary",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSaveButton"
                  }
                },
                {
                  "uid": "form001_button_submit",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Submit",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSubmitButton"
                  }
                },
                {
                  "uid": "form001_button_reject",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Reject",
                    "display-type": "ghost-danger",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickRejectButton"
                  }
                },
                {
                  "uid": "form001_button_approve",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Approve",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickApproveButton"
                  }
                }
              ]
            }
        }
      ],
      "model": {
        "first_name": "",
        "middle_name": "",
        "last_name": "",
        "date_of_birth": "",
        "ni_number": "",
        "gender": ""
      },
      "computed": {},
      "methods": {
        "onClickSubmitButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Permission Example will be submitted.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Submit"
          },
          "#toast": {
            "error": "Failed to Submit",
            "success": "Successfully Submitted"
          }
        },
        "onClickSaveButton": {
          "#validate": {},
          "#save": {
             "action": "Save"
          },
          "#toast": {
              "error": "Failed to Save",
              "success": "Successfully Saved"
          }
        },
        "onClickApproveButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Permission Example will be Approved.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Approve"
          },
          "#toast": {
            "error": "Failed to Approve",
            "success": "Permission Example has been Approved"
          }
        },
        "onClickRejectButton": {
          "#validate": {},
          "#prompt": {
            "title": "Rejection Confirmation",
            "confirmDisplayType": "label-feedback-danger",
            "confirmText": "Reject",
            "inputType": "textarea",
            "inputLabel": "Comment"
          },
          "#save": {
            "action": "Reject"
          },
          "#toast": {
            "error": "Failed to Reject",
            "success": "Permission Example 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;
