# Mawidi N8N Workflows

Pre-built N8N workflows for Mawidi AI agent integration.

## Workflows

### 1. `mawidi-test-webhook.json`
Simple test workflow to verify connectivity between N8N and Mawidi.

**What it does:**
- Receives a webhook request
- Forwards it to Mawidi's conversation callback
- Returns the result

### 2. `mawidi-whatsapp-ai-agent.json`
Complete WhatsApp AI agent workflow with context lookup and OpenAI integration.

**What it does:**
1. Receives incoming WhatsApp message
2. Looks up user context from Mawidi (knowledge base, FAQs, preferences)
3. Generates AI response using OpenAI
4. Logs both messages to Mawidi dashboard
5. Returns response

## Setup Instructions

### Step 1: Import Workflow

1. Open your N8N instance
2. Go to **Workflows** > **Import from File**
3. Select the `.json` file you want to import

### Step 2: Configure Environment Variables

In N8N, set these environment variables (**Settings** > **Environment Variables**):

```
MAWIDI_API_URL=http://localhost:9000    # Or your production URL
MAWIDI_WEBHOOK_TOKEN=your-secret-token  # Must match N8N_WEBHOOK_AUTH_TOKEN in Mawidi
```

### Step 3: Configure Credentials (for AI workflows)

For the WhatsApp AI Agent workflow:
1. Go to **Credentials** in N8N
2. Add **OpenAI API** credentials
3. Link them to the "Generate AI Response" node

### Step 4: Activate Workflow

1. Open the imported workflow
2. Click **Activate** in the top right
3. Copy the webhook URL

### Step 5: Add Link in Staff Dashboard

1. Go to `/staff/dashboard/users`
2. Click on a user
3. Go to the **Links** tab
4. Click **Add Link**
5. Select type: `AI_AGENT` or `WHATSAPP_INTEGRATION`
6. Paste the N8N webhook URL

## Testing

### Using the Staff Test Endpoint

```bash
# Check N8N status
curl http://localhost:9000/api/staff/n8n-test \
  -H "Cookie: your-staff-session-cookie"

# Run integration test
curl -X POST http://localhost:9000/api/staff/n8n-test \
  -H "Content-Type: application/json" \
  -H "Cookie: your-staff-session-cookie" \
  -d '{"testType": "context"}'
```

### Testing the Webhook Directly

```bash
# Test the N8N webhook
curl -X POST https://your-n8n.com/webhook/mawidi-test \
  -H "Content-Type: application/json" \
  -d '{
    "user_id": "user-123",
    "customer_phone": "+974-1234-5678",
    "customer_name": "Test Customer",
    "message_role": "user",
    "message_content": "Hello, I need help!"
  }'
```

## Environment Variables (Mawidi Side)

Add these to your `.env` file:

```bash
# N8N Configuration
N8N_WEBHOOK_AUTH_TOKEN=your-secret-token  # Shared with N8N
N8N_BASE_URL=https://your-n8n-instance.com
```

## Workflow Architecture

```
┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   WhatsApp/     │────▶│     N8N         │────▶│    Mawidi       │
│   Voice/Form    │     │   Workflow      │     │    API          │
└─────────────────┘     └─────────────────┘     └─────────────────┘
                               │                        │
                               ▼                        ▼
                        ┌─────────────────┐     ┌─────────────────┐
                        │   OpenAI/       │     │   Dashboard     │
                        │   Claude        │     │   Updates       │
                        └─────────────────┘     └─────────────────┘
```

## API Endpoints Used

| Endpoint | Purpose |
|----------|---------|
| `/api/n8n/context` | Get user settings, knowledge base, preferences |
| `/api/n8n/kb/query` | Search knowledge base for relevant content |
| `/api/n8n/availability` | Check booking availability |
| `/api/webhooks/n8n/conversation` | Log conversation messages |
| `/api/webhooks/n8n/booking` | Create appointments |
| `/api/webhooks/n8n/lead` | Capture leads |
| `/api/webhooks/n8n/analytics` | Update metrics |
