# AI Event Tracking — Test Guide

## Quick Test

### 1. Start Server
```bash
cd D:\cnf\chatbot-canifa-feedback
python backend/server.py
```

Server sẽ chạy tại `http://localhost:8000`

### 2. Run Automated Test
```bash
python test_ai_event_api.py
```

### 3. Manual API Test với curl

**Publish event:**
```bash
curl -X POST "http://localhost:8000/api/ai-event/publish" \
  -H "Content-Type: application/json" \
  -d '{
    "event_type": "chat:search_completed",
    "channel": "in_app",
    "user_id": "test_user",
    "data": {
      "query": "áo 30/4",
      "products_found": 5,
      "elapsed_ms": 150
    }
  }'
```

**Get logs:**
```bash
curl "http://localhost:8000/api/ai-event/logs?limit=10"
```

**Get stats:**
```bash
curl "http://localhost:8000/api/ai-event/stats"
```

**Clear logs:**
```bash
curl -X DELETE "http://localhost:8000/api/ai-event/logs"
```

### 4. Open Web UI
- Navigate to: `http://localhost:8000/static/ai_event/main.html`
- Or access via: `http://localhost:8000/home/` → find "AI Event Tracking" in menu (if added)

### 5. Test Event Context Boost
To test event 30/4 boosting:

1. Send query to chatbot API:
```bash
curl -X POST "http://localhost:8000/api/chatbot/chat" \
  -H "Content-Type: application/json" \
  -d '{
    "user_query": "tìm áo 30/4",
    "user_id": "test_123"
  }'
```

2. Check logs for `[EVENT BOOST]` message in server console

3. Expected behavior:
   - Event context detected: "Giải phóng miền Nam (30/4)"
   - Tags boosted: `["Màu đỏ", "Đi chơi / dạo phố"]`
   - Color boosted: `"Màu đỏ"`
   - Product lines boosted: `["Áo phông", "Quần jean", "Váy liền"]`

### 6. Check Event Types
```bash
curl "http://localhost:8000/api/ai-event/types"
```

Should return list of event types from `common.notification.events.EventType.ALL`

---

## Features Implemented

✅ API Routes (`/api/ai-event/*`)
  - `GET /logs` — Retrieve event logs
  - `POST /publish` — Publish new event
  - `GET /stats` — Get statistics
  - `DELETE /logs` — Clear all logs
  - `GET /types` — List event types

✅ Web UI (`/static/ai_event/`)
  - Live Feed — Real-time event display
  - Statistics — Charts (Chart.js) + summary cards
  - Test Publisher — Manual event testing
  - Search & Filter — By type & text
  - Auto-refresh toggle

✅ Event Context Integration
  - `event_context` passed to `lead_search_tool`
  - `_apply_event_context()` method boosts search params
  - Logged with `[EVENT BOOST]` prefix

---

## Troubleshooting

**Server not starting?**
- Check Python dependencies: `pip install -r backend/requirements.txt`
- Ensure Redis is running (if enabled in config)

**API returns 404?**
- Ensure `ai_event_router` is imported in `main_router.py`
- Check server logs for import errors

**Events not appearing?**
- Check browser console for JS errors
- Verify API is accessible: `curl http://localhost:8000/health`
- Check server logs for event publication

**Event boost not working?**
- Check that `event_context` is in tool call logs
- Verify `event_hard.py` has the event definition
- Look for `[EVENT BOOST]` in server logs
