# 🚀 AI Event Boost — Test Guide

## Quick Start (3 bước)

### 1. Start Server
```bash
cd D:\cnf\chatbot-canifa-feedback
python backend/server.py
```
Chờ thấy: `Uvicorn running on http://0.0.0.0:8000`

### 2. Mở Chatbot Debug UI
```
http://localhost:8000/static/ai_event/chatbot.html
```

### 3. Run Automated Tests
```bash
python quick_test.py
```

---

## 📋 Test Cases

### ✅ Test 1: Event 30/4 (Giải phóng miền Nam)
**Query:** `tìm áo 30/4`

**Expected:**
- Event context: `mentioned_event.name = "Giải phóng miền Nam (30/4)"`
- Tags boost: `["Màu đỏ", "Đi chơi / dạo phố"]`
- Master color: `"Màu đỏ"`
- Product lines: `["Áo phông", "Quần jean", "Váy liền"]`
- Products trả về: Màu đỏ, áo phông, quần jean

**Check:**
- Debug Panel → Event Context card
- Server log: `[EVENT BOOST] Applied event 'Giải phóng miền Nam (30/4)'`

---

### ✅ Test 2: Event Valentine
**Query:** `váy Valentine` hoặc `áo Valentine`

**Expected:**
- Event: `Valentine (14/2)`
- Master color: `"Màu hồng"`
- Product lines: `["Váy liền", "Áo kiểu", "Chân váy"]`

---

### ✅ Test 3: Event Tết
**Query:** `áo Tết` hoặc `quần Tết` hoặc `mặc Tết`

**Expected:**
- Event: `Tết Nguyên Đán`
- Master color: `"Màu đỏ"`
- Product lines: `["Váy liền", "Chân váy", "Áo kiểu", "Blazer"]`

---

### ✅ Test 4: Regular Search (No Event)
**Query:** `quần jean nam` hoặc `áo polo`

**Expected:**
- `mentioned_event: null`
- Không có boost
- Search normal

---

### ✅ Test 5: Event Publish API
Test publish event vào event bus:
```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",
    "data": {"query": "test"}
  }'
```

Check logs:
```bash
curl "http://localhost:8000/api/ai-event/logs?limit=1"
```

---

## 🔍 Debug Panel Guide

Chatbot Debug UI có 4 card:

### 1. Event Context
- **Today**: Ngày hiện tại
- **Season**: Mùa (Xuân/Hè/Thu/Đông)
- **Mentioned**: Event được nhắc trong query
- **Next**: Event sắp tới
- **Details**: Full JSON của event context

### 2. Pipeline Flow
Hiển thị flow xử lý:
- 🔧 **Classifier**: Thời gian classify + log reasoning
- 📦 **Tool**: Thời gian search + số sản phẩm
- 💬 **Stylist**: Thời gian generate response

Click vào mỗi node để xem chi tiết (nếu có).

### 3. Products
Hiển thị danh sách sản phẩm trả về:
- Ảnh thumbnail
- Tên + SKU
- Giá (giá sale + discount)
- Click xem chi tiết

### 4. User Insight
- **Stage**: Giai đoạn mua hàng (1-5)
- **Tone**: Giọng điệu tư vấn
- **Goal**: Mục tiêu khách hàng
- **Target**: Đối tượng mua
- **Latest**: Sản phẩm quan tâm gần nhất

---

## 📊 API Endpoints

### Chatbot
```
POST /api/chatbot/chat
Content-Type: application/json

{
  "user_query": "string",
  "user_id": "string (optional)",
  "history": []  // optional, array of {role, content}
}

Response:
{
  "response": "string",
  "elapsed_ms": number,
  "lead_stage": {...},
  "updated_insight": {...},
  "pipeline": [...],
  "product_ids": [...],
  "products": [...],
  "event_context": {...}  // ← NEW!
}
```

### Event Tracking
```
GET    /api/ai-event/logs?limit=50&event_type=...&user_id=...
POST   /api/ai-event/publish
GET    /api/ai-event/stats
DELETE /api/ai-event/logs
GET    /api/ai-event/types
```

---

## 🐛 Troubleshooting

**Server not running?**
```bash
# Check if port 8000 is in use
netstat -ano | findstr :8000

# Start server
python backend/server.py
```

**404 on /api/chatbot/chat?**
- Check `backend/api/main_router.py` có include `chatbot_router`
- Restart server sau khi sửa code

**No event boost?**
- Check `event_hard.py` có event definition
- Query phải match keyword/date pattern
- Look for `[EVENT BOOST]` in server logs

**Products not showing?**
- Check database connection (StarRocks)
- Lead search tool phải trả về products
- Xem pipeline log是否有 tool step error

---

## 📁 Files Modified/Created

### Modified:
- `backend/agent/event_agent/product_search_engine.py` — Add event_context boost
- `backend/agent/event_agent/lead_search_tool.py` — Pass event_context
- `backend/agent/event_agent/graph.py` — Inject event_context into tool_args
- `backend/api/main_router.py` — Register ai_event_router

### Created:
- `backend/api/ai_event/ai_event_route.py` — Event API
- `backend/static/ai_event/main.html` — Event tracking UI
- `backend/static/ai_event/ai_event.css` — Styles
- `backend/static/ai_event/ai_event.js` — Frontend logic
- `backend/static/ai_event/chatbot.html` — Chatbot debug UI
- `quick_test.py` — Automated test script
- `TEST_AUTO_GUIDE.md` — This file

---

## 🎯 Success Criteria

✅ Event context detected from query (30/4, Tết, Valentine, etc.)
✅ Event boost applied to search params (tags, color, product_lines)
✅ Event log published and visible in UI
✅ Pipeline flow shows correct steps with timing
✅ Products returned match event theme (red for 30/4, pink for Valentine)
✅ No errors in server logs

---

**Happy testing! 🚀**
