-- Migration: Fix Employee Account form emails field name
-- Date: 2026-02-24
-- Description: The form was using user_emails[0].email but should use user_emails
--              to match the child table alias defined in models/Users.php

UPDATE forms
SET elements = jsonb_set(
  elements,
  '{1}',
  jsonb_build_object(
    'label', 'Email',
    'field_name', 'user_emails',
    'data_column', 'user_emails',
    'type', 'email',
    'type_id', 1,
    'order_index', 2,
    'required', true,
    'child_table', 'user_emails',
    'child_field', 'email',
    'child_defaults', jsonb_build_object('is_primary', true),
    'help_text', 'Primary email address for this employee'
  )
)
WHERE name = 'Employee Account'
  AND elements->1->>'field_name' = 'user_emails[0].email';

-- Verify the update
SELECT 
  name,
  elements->1 as email_field
FROM forms
WHERE name = 'Employee Account';
