# Campaign Script Generation Issue - Manual Workaround

## Problem Identified
The campaign creation at `/campaigns/create/35` gets stuck showing:
**"جاري توليد السيناريو... قد يستغرق بضع ثوان"** (Generating script... may take a few seconds)

But the script is never generated and the page doesn't redirect.

## Root Cause
The script generation is failing silently due to:
1. Module import issues (flask_login dependency)
2. AI API connection problems
3. Timeout during script generation

## Immediate Solutions

### Option 1: Create Campaign Manually via Database ✅

You can create the campaign directly in the database and let it redirect to the detail page:

```sql
INSERT INTO campaigns (user_id, report_id, name, script, storyboard_json, video_provider, status, created_at)
VALUES (
    (SELECT id FROM users WHERE id = (SELECT user_id FROM reports WHERE id = 35)),
    35,
    'حملة فيديو قهوة المترو',
    '{"title": "سيناريو فيديو لمشروع قهوة المترو", "scenes": [{"scene": "مقدمة", "duration": "5s", "description": "عرض المشكلة والحل"}]}',
    NULL,
    'default',
    'script_ready',
    NOW()
);
```

Then access: `https://jadwaai.com/campaigns/detail/[new_campaign_id]`

### Option 2: Try Creating via Different Approach ✅

1. **Go to dashboard first**: `/campaigns/`
2. **Create campaign from dashboard** instead of direct report link
3. **Use simpler script notes** or leave them empty
4. **Clear browser cache** before trying again

### Option 3: Fix the Underlying Issue 🔧

The issue is in `/app/routes/campaigns.py` around lines 64-80 where script generation happens:

```python
# Current problematic code
try:
    from app.ai.prompts.video_script import get_video_script_prompt
    from app.ai.diamond import call_ai, _parse_json

    messages = get_video_script_prompt(...)
    response = run_sync(call_ai(messages, model_type="deep"))
    # ... rest of script generation
except Exception as e:
    # This error is caught but the loading state persists
    current_app.logger.error(f"Script generation failed: {e}")
    flash("فشل توليد السيناريو. حاول مرة أخرى.", "error")
    return redirect(url_for("campaigns.create", report_id=report_id))
```

## Recommended Immediate Action

1. **Create campaign manually** using the SQL above
2. **Access the detail page** where you can:
   - Generate script from the detail page (which has better error handling)
   - Use the "توليد السيناريو" button on the detail page
   - Get better error messages if it fails

## Testing Steps

1. **Run the manual SQL** to create a basic campaign
2. **Go to the campaign detail page** for the new campaign
3. **Try generating script from there** (button at top)
4. **Check browser console** for any JavaScript errors
5. **Check network tab** for failed API requests

## Alternative: Quick Fix for Create Page

If you want to fix the create page directly, you can modify the create process to:

1. **Create campaign first without script**
2. **Redirect to detail page immediately**
3. **Generate script asynchronously** from detail page

This would avoid the hanging issue on the create page.

---

**Status**: 🟡 Requires manual intervention or code fix
**Workaround**: Use SQL to create campaign, then generate script from detail page
**Root Cause**: Script generation timeout/failure on create page