BEGIN;

DELETE FROM forms
WHERE name = 'Employee Account'
  AND tenant_id IS NULL
  AND branch_id IS NULL;

INSERT INTO forms (name, description, mapped_table, elements, tenant_id, branch_id)
VALUES (
  'Employee Account',
  'Create a basic employee account with branch assignment and contact details',
  'users',
  $form$[
    {
      "label": "Full Name",
      "field_name": "full_name",
      "data_column": "full_name",
      "type_id": 1,
      "order_index": 1,
      "required": true
    },
    {
      "label": "Emails",
      "field_name": "emails",
      "data_column": "emails",
      "type_id": 16,
      "order_index": 2,
      "required": true
    },
    {
      "label": "Phones",
      "field_name": "phones",
      "data_column": "phones",
      "type_id": 16,
      "order_index": 3
    },
    {
      "label": "Location",
      "field_name": "branch_id",
      "data_column": "branch_id",
      "type": "select",
      "type_id": 4,
      "order_index": 4,
      "required": true,
      "options_table": "branches",
      "options_label_column": "name",
      "options_value_column": "branch_id",
      "help_text": "Select the branch where this employee will work"
    }
  ]$form$::jsonb,
  NULL,
  NULL
);

-- Switch the Employees module to the JSON-config driven modal
UPDATE modules
SET config = $module$ {
      "title": "Employee Management",
      "header": [
        { "type": "button", "label": "Add Employee", "formName": "Employee Account" }
      ],
      "body": [
        {
          "type": "table",
          "options": {
            "tableName": "users",
            "showGlobalSearch": false,
            "addButton": false,
            "columnsToShow": ["full_name", "status", "user_emails", "phones"]
          }
        }
      ],
      "footer": [
        { "type": "button", "label": "Close", "onClick": "closeModal" }
      ]
    } $module$::jsonb,
    name = 'Employees',
    icon = 'fa-users',
    aria_label = 'Employees',
    description = 'Manage employees, rates, and types'
WHERE module_id = '0194a8b2-7777-7123-8123-123456789ac6'::uuid;

COMMIT;
