# Spouse ↔ Emergency Contact Bi-Directional Auto-Fill Testing Guide

## What's New ✨
You can now fill EITHER the Emergency Contact fields OR the Spouse fields FIRST, and they will sync automatically!

---

## Test Case 1: Fill Emergency Contact FIRST ✅ (NEW - This was the missing piece!)

### Steps:
1. Open Client Creation Form (Lawyer, Staff, or Agent portal)
2. Go to **Emergency** tab
3. Fill these fields:
   - **Contact Name**: "Alexa Golden"
   - **Contact Phone**: "0123456789"
   - **Contact Email**: "alexa@email.com"
   - **Relationship**: Select "Spouse" from dropdown

4. Go back to **Account** tab
5. Scroll down to see **Spouse Details** section

### Expected Result:
✅ The following fields should be AUTO-POPULATED:
- **Spouse Name**: "Alexa Golden"
- **Spouse Phone**: "0123456789"
- **Spouse Email**: "alexa@email.com"

**What happens in console:**
```
🔄 Auto-populate Spouse from Emergency - Name: Alexa Golden, Relationship: spouse
✅ Spouse Name updated from Emergency Contact: Alexa Golden
✅ Spouse Phone updated from Emergency Contact: 0123456789
✅ Spouse Email updated from Emergency Contact: alexa@email.com
```

---

## Test Case 2: Fill Spouse FIRST (Already Working)

### Steps:
1. Open Client Creation Form
2. Go to **Account** tab
3. Go to Marital Status and set to "Married"
4. Fill **Spouse Details** section:
   - **Spouse Name**: "Alexa Golden"
   - **Spouse Phone**: "0123456789"
   - **Spouse Email**: "alexa@email.com"

5. Go to **Emergency** tab
6. Look at **Emergency Contact 1** section

### Expected Result:
✅ The following fields should be AUTO-POPULATED:
- **Contact Name**: "Alexa Golden"
- **Contact Phone**: "0123456789"
- **Contact Email**: "alexa@email.com"
- **Relationship**: "Spouse"

---

## Test Case 3: Real-Time Sync (Type as you go)

### Steps:
1. Open Client Creation Form
2. Go to **Emergency** tab
3. Start TYPING in Contact Name: "A" → "Al" → "Ale" → "Alexa" → "Alexa G" → "Alexa Golden"
4. Watch the Account/Spouse field update in REAL-TIME as you type
5. Fill Contact Phone field
6. Watch Spouse Phone field update as you type
7. Select Relationship = "Spouse"

### Expected Result:
✅ Each keystroke triggers auto-population
✅ Spouse fields update as you type (real-time sync)
✅ No need to wait for blur/change event

---

## Test Case 4: Smart Logic - Only Populates for "Spouse" Relationship

### Steps:
1. Open Client Creation Form
2. Go to **Emergency** tab
3. Fill Contact Name: "John Smith"
4. Set Relationship to "Friend" (NOT Spouse)
5. Go to **Account** tab

### Expected Result:
❌ Spouse fields should REMAIN EMPTY (not populated)
✅ This is correct behavior - only "spouse" relationship triggers auto-fill

### Now test with "Spouse":
1. Back to **Emergency** tab
2. Change Relationship to "Spouse"
3. Go to **Account** tab

### Expected Result:
✅ Now Spouse Name field = "John Smith"
✅ Auto-filled because relationship = "spouse"

---

## Test Case 5: Edit After Auto-Fill

### Steps:
1. Fill Emergency Contact fields with spouse data and set relationship to "Spouse"
2. Go to Account tab - Spouse fields auto-populated ✅
3. Edit the Spouse Name to different value
4. Go back to Emergency tab
5. Click in Emergency Contact Name field

### Expected Result:
✅ Account → Emergency auto-fill still works
✅ You can edit either side and they stay in sync
✅ No conflicts or overwrites

---

## Test Case 6: Empty Fields Handling

### Steps:
1. Go to Emergency tab
2. Fill Contact Name: "Alexa Golden"
3. Leave Contact Phone EMPTY
4. Set Relationship to "Spouse"
5. Go to Account tab

### Expected Result:
✅ Spouse Name = "Alexa Golden"
✅ Spouse Phone = Empty (not filled because emergency phone is empty)
✅ Gracefully handles partial data

---

## What to Look For (Browser Console)

**Open browser Developer Tools** (F12 or Right-click → Inspect)
Go to **Console** tab

### When filling Emergency Contact fields, you should see:
```
🔄 Auto-populate Spouse from Emergency - Name: Alexa Golden, Relationship: spouse
✅ Spouse Name updated from Emergency Contact: Alexa Golden
✅ Spouse Phone updated from Emergency Contact: 0123456789
✅ Spouse Email updated from Emergency Contact: alexa@email.com
```

### When filling Spouse fields, you should see:
```
🔄 Auto-populate Emergency Contact - Spouse: Alexa Golden, Phone: 0123456789, Email: alexa@email.com
✅ Emergency Contact Name updated: Alexa Golden
✅ Emergency Contact Phone updated: 0123456789
✅ Emergency Contact Email updated: alexa@email.com
✅ Emergency Contact Relationship set to: spouse
```

---

## Portals to Test
- ✅ Lawyer Portal (/lawyer/clients/create)
- ✅ Staff Portal (/staff/clients/create)
- ✅ Agent Portal (/agent/clients/create)

All three have the bi-directional sync implemented!

---

## Key Points to Remember

| Feature | Status |
|---------|--------|
| Fill Spouse → Auto-populate Emergency | ✅ Working |
| Fill Emergency → Auto-populate Spouse | ✅ NEW - NOW WORKING |
| Real-time sync while typing | ✅ Working |
| Only syncs when relationship="spouse" | ✅ Working |
| Works across all 3 portals | ✅ Working |
| Handles empty/partial data | ✅ Working |
| No data loss on edit | ✅ Working |

---

## If Something Doesn't Work

1. **Open browser console** (F12) and check for JavaScript errors
2. **Look for the 🔄 and ✅ logs** - they show if auto-fill triggered
3. **Verify relationship field** = "Spouse" (case-insensitive, should work)
4. **Check field names** - emergency_contacts[0][name], spouse_name, etc.
5. **Clear browser cache** - old JavaScript might be cached

---

## Questions?

If auto-fill doesn't trigger:
1. ✅ Did you set Relationship to "Spouse"?
2. ✅ Did you fill at least one field with data?
3. ✅ Are you on the correct portal (not a different one)?
4. ✅ Check console for errors (F12 → Console tab)

Everything should work now! Enjoy the bi-directional sync! 🎉
