# Debug Prompt (FormBuilder Booking)

Use this prompt with any developer/AI assistant to debug and fix booking issues quickly.

## Prompt

You are a senior PHP + JS debugging engineer.  
Project: FormBuilder (PHP, MySQL, vanilla JS), booking module under `api/booking.php`, `book.php`, `plans.php`, `admin/booking-plans.php`, `admin/booking-providers.php`.

### Goal
Find and fix why booking/admin data appears "not saved", and why booking Step 3 sometimes hangs or shows no providers.

### Requirements
1. Reproduce issue end-to-end.
2. Identify exact failing layer(s): UI state, request payload, API handler, DB write, DB readback.
3. Patch root cause(s), not only symptoms.
4. Keep changes minimal and safe.
5. Verify with concrete test steps + expected JSON responses.

### Known risk areas
- Checkbox UI may look selected but payload may not include `service_ids`.
- ID type mismatch (`"1"` vs `1`) can break strict JS comparisons.
- Provider creation may miss writing `bk_provider_services`.
- Booking Step 3 depends on:
  - `POST /api/booking/check-membership`
  - selected service id
  - `GET /api/booking/providers?service_id=X`
- `book.php`/`plans.php` require valid `APP_URL` usage and robust API error handling.

### Debug checklist
1. **Frontend selection state**
   - Confirm checked inputs via:
     - `#planSvcChecks input:checked`
     - `#provSvcChecks input:checked`
   - Confirm selected IDs are integers and non-empty before save.

2. **Network payload**
   - Inspect save requests in browser devtools:
     - `PUT/POST /api/booking/admin/plans...`
     - `PUT/POST /api/booking/admin/providers...`
   - Confirm `service_ids: [..]` exists and is correct.

3. **API handlers**
   - Validate both create and update routes persist service mappings:
     - `bk_plan_services`
     - `bk_provider_services`

4. **Readback**
   - Refresh and verify API read endpoints return persisted mappings:
     - `/api/booking/admin/plans/:id` -> `service_ids`
     - `/api/booking/admin/providers/:id` -> `services`

5. **Booking runtime**
   - Check `check-membership` returns unlocked services.
   - Ensure selected service/provider lookup handles string/number IDs consistently.
   - Ensure Step 3 shows:
     - providers list when linked
     - clear empty-state/error when not linked

### SQL quick checks
Run these against the same DB used by production:

```sql
SELECT * FROM bk_plan_services ORDER BY plan_id, service_id;
SELECT * FROM bk_provider_services ORDER BY provider_id, service_id;
SELECT id,name FROM bk_services ORDER BY id;
SELECT id,name,is_active FROM bk_providers ORDER BY id;
```

### Definition of done
- Selecting services in admin and clicking Save persists to DB.
- Reloading admin pages shows saved mappings (no false "No services linked").
- Booking flow Step 3 reliably loads providers for linked services.
- No PHP syntax errors and no new JS console errors.

