# Slipping Away Scripts Fix - Investigation & Plan

> **Status:** ✅ COMPLETE - Deployed Jan 18, 2026 (with error handling)
> **Created:** Jan 17, 2026
> **Issue:** "Re-engage with Script" button doesn't load scripts

---

## Investigation Summary

### User Report
- Dashboard "Slipping Away" section has "Re-engage with Script" button
- When pressed, scripts don't load
- Page appears empty

### Root Cause Analysis

**Two separate script systems exist:**

| System | Route | Table | Status |
|--------|-------|-------|--------|
| Prompts Hub (old) | `/prompts` | `prompt_templates` | Empty/Missing |
| Scripts Command Center (new) | `/scripts` | `scripts` | Has 30 seed scripts |

**The bug:** SlippingLeads.jsx navigates to `/prompts` which queries `prompt_templates` - a table that was never properly migrated or seeded.

### Code Trace

**1. SlippingLeads.jsx (lines 256-262):**
```jsx
<button
  onClick={() => navigate('/prompts', {
    state: {
      selectedLeads: data.leads,
      filter: 'slipping'
    }
  })}
>
  Re-engage with Script
</button>
```

**2. Prompts.jsx (line 231):**
```jsx
const result = await promptsApi.list(params);
// Calls GET /api/prompts
```

**3. Backend prompts.js (line 22-24):**
```javascript
let query = supabase
  .from('prompt_templates')  // <-- This table is empty!
  .select('*')
```

**4. Meanwhile, Scripts Command Center has data:**
- `/scripts` route exists and works
- `scripts` table has 30 seed scripts (10 SMS, 10 Call, 8 Objection, 2 Email)
- Added in today's Scripts Command Center build

---

## Solution Options

### Option A: Quick Fix (Recommended)
Change SlippingLeads to navigate to `/scripts` instead of `/prompts`

**Pros:**
- 1-line change
- Uses existing working system with 30 scripts
- Immediate fix

**Cons:**
- Leaves `/prompts` page broken (but unused)

### Option B: Run Migration
Run the `prompts-hub-migration.sql` to create and seed `prompt_templates`

**Pros:**
- Fixes the Prompts Hub page

**Cons:**
- Uses `uuid_generate_v4()` which may fail (should be `gen_random_uuid()`)
- Creates duplicate script system
- More complexity

### Option C: Unify Systems
Merge `prompt_templates` and `scripts` into one system

**Pros:**
- Single source of truth
- Cleaner architecture

**Cons:**
- Significant refactor
- Multiple files to change
- Risk of breaking things

---

## Recommended Plan: Option A

### Step 1: Update SlippingLeads navigation

**File:** `admin/src/components/dashboard/SlippingLeads.jsx`
**Line:** 257

**Before:**
```jsx
onClick={() => navigate('/prompts', {
  state: {
    selectedLeads: data.leads,
    filter: 'slipping'
  }
})}
```

**After:**
```jsx
onClick={() => navigate('/scripts', {
  state: {
    selectedLeads: data.leads,
    filter: 'slipping',
    category: 'reengagement'  // Pre-filter to re-engagement scripts
  }
})}
```

### Step 2: Update ScriptsCommandCenter to accept selectedLeads state

**File:** `admin/src/pages/ScriptsCommandCenter.jsx`

Check if it handles `location.state.selectedLeads` - if not, add support.

### Step 3: Build and deploy

```bash
cd admin && npm run build
cp -r dist/* ../public/
git add . && git commit && git push
```

---

## Files to Modify

| File | Change |
|------|--------|
| `admin/src/components/dashboard/SlippingLeads.jsx` | Change `/prompts` to `/scripts` |
| `admin/src/pages/ScriptsCommandCenter.jsx` | Handle `selectedLeads` state (if needed) |

---

## Future Consideration

After this fix, consider:
1. Deprecating the unused `/prompts` page
2. Or migrating `prompt_templates` data to `scripts` table
3. Removing duplicate code

---

## Acceptance Criteria

- [ ] "Re-engage with Script" button loads Scripts Command Center
- [ ] Scripts display correctly (30 scripts visible)
- [ ] Pre-selected leads are passed through (if supported)
- [ ] Category filter defaults to "reengagement" scripts
