# 🚀 DOING: Reaction Simulator → Content Pipeline Integration

## 📁 Folders & Files liên quan
```
backend/
├── agent/
│   └── reaction_agent/
│       └── reaction_agent.py          ← OK (không đổi, logic đã hoàn thiện)
├── api/
│   ├── reaction_simulator/
│   │   ├── reaction_simulator_route.py ← MODIFY (hook save sau simulate)
│   │   └── reaction_db.py             ← 🆕 NEW (SQLite helpers)
│   └── content_approval/
│       └── content_approval_route.py   ← MODIFY (thêm /pretest endpoint)
└── database/
    └── simulator.db                    ← MODIFY (thêm bảng reaction_runs + reaction_details)
```

## 📌 Context
- **Status:** 🚧 In Progress

## 🎯 Goal
Lưu kết quả Reaction Simulation vào SQLite, kết nối với Content Approval pipeline để marketing có thể "pre-test" campaign trước khi publish.

## ⚠️ Active Trade-offs
- Storage: SQLite `database/simulator.db` (dùng chung với Doings #16)
- Không thay đổi logic reaction_agent hiện tại — chỉ wrap thêm persistence layer

---

## 🔍 Hiện trạng

### ✅ Đã có:
- `agent/reaction_agent/reaction_agent.py` — 7 persona segments, LLM generate reactions, sentiment calc, recommendations
- `api/reaction_simulator/reaction_simulator_route.py` — `/simulate` (LLM) + `/simulate-mock` (fake data)
- `api/content_approval/content_approval_route.py` — Full CRUD: create → submit → approve → schedule → publish

### ❌ Thiếu:
1. **Không lưu kết quả** — reaction results chỉ return JSON, mất khi response xong
2. **Không kết nối Content Approval** — approve content không có bước "pre-test reaction"
3. **Không compare** — không thể so sánh reactions giữa campaign v1 vs v2
4. **Mock mode không tag** — `/simulate-mock` trả fake data nhưng không phân biệt với real

---

## 🛠 Impact & Files Touched
| File | Symbol | Change | Blast Radius |
|------|--------|--------|--------------|
| NEW: `api/reaction_simulator/reaction_db.py` | `init_reaction_db`, `save_reaction_run` | ADD | reaction_simulator_route |
| `api/reaction_simulator/reaction_simulator_route.py` | `/simulate` | MODIFY (add save) | reaction_agent |
| `api/content_approval/content_approval_route.py` | `/pretest` | ADD endpoint | content_approval |

**Risk:** Low

## 📋 Execution Checklist

### Phase 1: SQLite Storage (~20m)
- [x] Task 1.1: Tạo `api/reaction_simulator/reaction_db.py`:
  - `init_reaction_db()` — tạo bảng `reaction_runs` + `reaction_details` trong `database/simulator.db`
  - Schema `reaction_runs`: `run_id, campaign_type, campaign_content_preview, timestamp, sentiment_positive_pct, sentiment_neutral_pct, sentiment_negative_pct, total_reactions, model_name, is_mock, recommendations_json`
  - Schema `reaction_details`: `id, run_id (FK), persona_id, persona_name, comment, sentiment, likes_estimate, shares_estimate, key_concern`
- [x] Task 1.2: Helper `save_reaction_run(run_data)` → insert run + details
- [x] Task 1.3: Helper `get_reaction_runs(limit)` → list past runs
- [x] Task 1.4: Helper `get_reaction_run(run_id)` → chi tiết 1 run
- [x] Task 1.5: Helper `compare_reaction_runs(run_id_1, run_id_2)` → sentiment delta

### Phase 2: Hook save vào existing endpoints (~10m)
- [x] Task 2.1: Hook `save_reaction_run()` vào cuối `/simulate` endpoint — auto-save khi chạy xong
- [x] Task 2.2: Hook `save_reaction_run(is_mock=True)` vào `/simulate-mock`
- [x] Task 2.3: Return `run_id` trong response để FE có thể reference

### Phase 3: API endpoints mới (~15m)
- [x] Task 3.1: `GET /api/reaction-simulator/history` — list past reaction runs
- [x] Task 3.2: `GET /api/reaction-simulator/history/{run_id}` — chi tiết 1 run
- [x] Task 3.3: `GET /api/reaction-simulator/compare?run1=xxx&run2=yyy` — compare 2 runs

### Phase 4: Content Approval Integration (~10m)
- [x] Task 4.1: Thêm endpoint `POST /api/content/{content_id}/pretest` → lấy caption từ content → chạy reaction simulation → return results + save
- [x] Task 4.2: Link `run_id` vào content metadata → marketing xem reaction results khi approve

### Phase 5: Testing (~10m)
- [x] Task 5.1: Test `/simulate` → verify saved vào SQLite
- [x] Task 5.2: Test history + detail endpoints
- [x] Task 5.3: Test pretest on a content item

## 🔄 Sub-Doings
_None yet_

## ✅ Completion Gate
- [x] All tasks [x] | [x] Sub-Doings resolved | [x] Smoke test passed | [x] Trade-offs confirmed

---
_Started: 2026-05-10 | Updated: 2026-05-10_
