-- Migration: Fix Employee Account form emails field name
-- Date: 2026-02-24
-- Description: Update Employee Account form to use 'emails' child table alias
--              to match the Users model and sync endpoint expectations

UPDATE forms 
SET elements = $form$[
  {
    "label": "Full Name",
    "type_id": 1,
    "required": true,
    "field_name": "full_name",
    "data_column": "full_name",
    "order_index": 1
  },
  {
    "type": "email",
    "label": "Email",
    "type_id": 1,
    "required": true,
    "help_text": "Primary email address for this employee",
    "field_name": "emails",
    "child_field": "email",
    "child_table": "user_emails",
    "data_column": "emails",
    "order_index": 2,
    "child_defaults": {
      "is_primary": true
    }
  },
  {
    "type": "tel",
    "label": "Phone",
    "type_id": 1,
    "help_text": "Primary phone number",
    "field_name": "phones[0].phone_number",
    "child_field": "phone_number",
    "child_table": "user_phones",
    "data_column": "phones",
    "order_index": 3,
    "child_defaults": {
      "is_primary": true
    }
  },
  {
    "type": "select",
    "label": "Location",
    "type_id": 4,
    "required": true,
    "help_text": "Select the branch where this employee will work",
    "field_name": "branch_memberships[0].branch_id",
    "child_field": "branch_id",
    "child_table": "user_branch_memberships",
    "data_column": "branch_memberships",
    "order_index": 4,
    "options_table": "branches",
    "child_defaults": {
      "role": "member"
    },
    "options_label_column": "name",
    "options_value_column": "branch_id"
  },
  {
    "type": "select",
    "label": "Groups",
    "type_id": 4,
    "multiple": true,
    "help_text": "Select groups for this employee (optional)",
    "field_name": "group_memberships",
    "data_column": "group_memberships",
    "order_index": 5,
    "options_table": "groups",
    "options_label_column": "name",
    "options_value_column": "group_id"
  },
  {
    "type": "password",
    "label": "Password",
    "type_id": 7,
    "required": false,
    "help_text": "Leave blank to keep current password. Minimum 8 characters if changing.",
    "field_name": "password.password_hash",
    "child_field": "password_hash",
    "child_table": "user_passwords",
    "data_column": "password",
    "order_index": 6,
    "child_defaults": {
      "algo": "bcrypt"
    }
  }
]$form$::jsonb
WHERE name = 'Employee Account';
