# 🚀 Quick Start - Heat User Study Session

## Setup trong 5 phút ⏱️

### 1️⃣ Chạy Migration

```bash
cd /var/www/html/lms_hocmai
php artisan migrate
```

### 2️⃣ Test Cron Command

```bash
# Test thử command
php artisan sessions:auto-end

# Kết quả mong đợi:
# Starting auto-end inactive sessions job...
# Active sessions before: 0
# ✓ Auto ended 0 sessions in 5.25ms
```

### 3️⃣ Kiểm tra Cron Schedule

```bash
# Xem danh sách scheduled tasks
php artisan schedule:list

# Tìm dòng:
# sessions:auto-end .......... Every 5 minutes
```

### 4️⃣ Test API

```bash
# Start session
curl -X POST http://localhost/api/heat-user/study-session/start \
  -H "Content-Type: application/json" \
  -d '{
    "student_id": 1,
    "id_history_contest": 100,
    "id_bai_kiem_tra": 456,
    "id_history": 200
  }'

# Response:
# {
#   "success": true,
#   "type": "new",
#   "session_id": 1,
#   "message": "Bắt đầu session mới"
# }
```

---

## 📡 API Endpoints Summary

| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/api/heat-user/study-session/start` | Bắt đầu session |
| POST | `/api/heat-user/study-session/continue` | Tiếp tục session |
| POST | `/api/heat-user/study-session/heartbeat` | Gửi heartbeat (mỗi 5 phút) |
| POST | `/api/heat-user/study-session/submit` | Nộp bài |
| POST | `/api/heat-user/study-session/stop` | Dừng hẳn session |
| GET | `/api/heat-user/study-session/statistics` | Thống kê |
| GET | `/api/heat-user/study-session/active` | Check session active |

---

## 🔄 Workflow

```
┌─────────┐     ┌──────────┐     ┌─────────┐     ┌────────┐
│  START  │────▶│ HEARTBEAT│────▶│  SUBMIT │────▶│  END   │
└─────────┘     │ (5 mins) │     └─────────┘     └────────┘
                └──────────┘            │
                     │                  │
                     ▼                  ▼
                 ┌───────┐         ┌────────┐
                 │ CLOSE │────────▶│ CONTINUE│
                 └───────┘         └────────┘
                     │
                     ▼
                 ┌──────────┐
                 │ AUTO-END │ (Cron: 5 mins)
                 └──────────┘
```

---

## 💻 Frontend Integration (Minimal)

```javascript
// 1. Start
fetch('/api/heat-user/study-session/start', {
  method: 'POST',
  body: JSON.stringify({ 
    id_history_contest: 100, 
    id_bai_kiem_tra: 456, 
    id_history: 200 
  })
})

// 2. Heartbeat every 5 minutes
setInterval(() => {
  fetch('/api/heat-user/study-session/heartbeat', {
    method: 'POST',
    body: JSON.stringify({ session_id: sessionId })
  })
}, 5 * 60 * 1000)

// 3. Submit
fetch('/api/heat-user/study-session/submit', {
  method: 'POST',
  body: JSON.stringify({ session_id: sessionId })
})
```

---

## ✅ Checklist

- [ ] Migration đã chạy thành công
- [ ] Cron command test OK
- [ ] Cron schedule đã active
- [ ] API start test OK
- [ ] Cache driver = redis/memcached (recommended)

---

## 🔗 Integration với EMS

### **Flow START (3 bước):**
```
1. FE → EMS: /lmsnew1/mockcontest/session/start → idHistoryContest
2. FE → EMS: /exam/start → idHistory  
3. FE → Heat User: /study-session/start → sessionId
```

### **Flow SUBMIT (2 bước):**
```
1. FE → Heat User: /study-session/submit → duration
2. FE → EMS: /exam/submit → score
```

### **Flow CONTINUE:**
```
1. FE → Heat User: /study-session/continue → new sessionId
2. FE → EMS: /exam/start (with is_continue=true) → saved answers
```

⚠️ **Quan trọng:**
- idHistory KHÔNG ĐỔI khi continue (same idHistory)
- idHistory MỚI khi stop → start lại
- Phải gọi EMS APIs trước khi gọi Heat User

---

## 📚 Xem thêm

- **⭐ Integration Flow:** [HEAT_USER_INTEGRATION_FLOW.md](./HEAT_USER_INTEGRATION_FLOW.md) - **BẮT BUỘC ĐỌC cho FE dev**
- **Full Documentation:** [HEAT_USER_STUDY_SESSION.md](./HEAT_USER_STUDY_SESSION.md)
- **Frontend Example:** Complete JavaScript class trong Integration Flow doc

---

**Ready to go! 🎉**

