INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('recruitment_preboarding_address_contact_detail', 'Recruitment (ATS) - Onboarding - Address/Contact Detail', 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_data_field (`name`, `label`, `data_group_id`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
    ('email', 'email', @data_group_id, 1, 0, 1, 0);
SET @email_data_field := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_user_role_data_field (user_role_id, data_field_id, can_read, can_update) VALUES
(@candidate_user_role_id,@email_data_field,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 ('Address/Contact Details', 'Address/Contact Details', 'POST', 'preboarding-address-contact-detail', null, @data_group_id,
        @system_user_id, @system_user_id, 'System', 1);
SET @dynamic_form_id := (SELECT `id`
                         FROM ohrm_dynamic_form
                         WHERE name = 'Address/Contact Details');

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 ('Address/Contact Details', 'Address/Contact Details', 'POST', 'preboarding-address-contact-detail', 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 = 'Address/Contact Details');

SET @metadata := CONCAT('
{
  "version": {
    "schema-version": "1.0.0"
  },
  "fields": [
    {
      "uid": "address_street1_1001",
      "name": "address_street1",
      "widget": "textbox",
      "label": "Address Street 1",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:70"
      ]
    },
    {
      "uid": "address_street2_1002",
      "name": "address_street2",
      "widget": "textbox",
      "label": "Address Street 2",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#length:0:70"
      ]
    },
    {
      "uid": "city_1003",
      "name": "city",
      "widget": "textbox",
      "label": "Town/City",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:70"
      ]
    },
    {
      "uid": "country_1004",
      "name": "country",
      "label": "Country",
      "widget": "oxd-master-data-input",
      "attr": {
        "service": "Country"
      },
      "validations": [
        "#required",
        "#masterDataSelect:Country"
      ]
    },
    {
      "uid": "region_1005",
      "name": "region",
      "widget": "textbox",
      "label": "Region",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#length:0:70"
      ]
    },
    {
      "uid": "postal_code_1006",
      "name": "postal_code",
      "widget": "text",
      "label": "Postal Code",
      "validations": [
        "#required",
        "#length:0:10"
      ]
    },
    {
      "uid": "phone_number_1007",
      "name": "phone_number",
      "widget": "text",
      "label": "Phone Number",
      "validations": [
        "#required",
        "#phone",
        "#length:0:25"
      ]
    },
    {
      "uid": "home_telephone_1008",
      "name": "home_telephone",
      "widget": "text",
      "label": "Home Telephone",
      "validations": [
        "#phone",
        "#length:0:25"
      ]
    },
    {
      "uid": "email_1009",
      "name": "email",
      "widget": "textbox",
      "label": "Email",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#email"
      ]
    }
  ],
  "views": {
    "form": {
      "uid": "form001",
      "action": "POST",
      "endpoint": "forms/1",
      "body": [
        {
          "uid": "form001_title",
          "tag": "oxd-text",
          "props": {
            "type": "card-title"
          },
          "slots": {
            "default": [
              "Address/Contact Details"
            ]
          }
        },
        {
          "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": "address_street1_1001"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_2",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "address_street2_1002"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_3",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "city_1003"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_4",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "country_1004"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_5",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "region_1005"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_6",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "postal_code_1006"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_7",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "phone_number_1007"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_8",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "home_telephone_1008"
                    }
                  ]
                }
              },
              {
                "uid": "form001_grid_item_9",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "email_1009"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "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": "form001_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": "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": {
        "address_street1": "",
        "address_street2": "",
        "city": "",
        "country": "",
        "region": "",
        "postal_code": "",
        "phone_number": "",
        "home_telephone": "",
        "email": ""
      },
      "computed": {},
      "methods": {
        "onClickSubmitButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Address/Contact details 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": "Address/Contact details will be Approved.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Approve"
          },
          "#toast": {
            "error": "Failed to Approve",
            "success": "Address/Contact details 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": "Address/Contact details 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;
