# Milestone 3 — XP, mastery, teacher dashboard

**Outcome:** Teachers see **motivational XP/levels** and **learning mastery** side by side. VR keeps posting results (M2); M3 awards XP and stores per-objective mastery (with teacher overrides).

Interactive API docs (Swagger): [http://localhost:3001/docs](http://localhost:3001/docs)  
OpenAPI JSON: [http://localhost:3001/docs.json](http://localhost:3001/docs.json)

---

## Prerequisites

1. Schema through M3 (`sql/mastery_xp.sql` / `sql/migrations/005_mastery_xp.sql`)
2. Redis + `.env` as in M1/M2
3. Seeds + API:

```bash
npm install
npm run seed:auth
npm run seed:curriculum
npm run seed:m3
npm run dev
```

API base: `http://localhost:3001` — health reports `"milestone": 3`.

---

## Acceptance criteria

| # | Criterion | How to verify |
|---|---|---|
| 1 | XP and levels update independently of mastery | Submit results → `xpAwarded` / `studentXp` rise; mastery status is separate |
| 2 | One correct answer never → Mastered | After 1 correct: `developing`; after ≥3 correct (≥80%, ≥2 correct): `mastered` |
| 3 | Teacher override persisted | `PATCH /teacher/students/:id/mastery/:objectiveId` sticks until `DELETE` |
| 4 | Teacher only sees assigned students’ mastery/XP | Cross-school / unassigned → 403/404 |

One-shot check:

```bash
npm run smoke:m3
```

Expect `ACCEPTANCE_OK` at the end.

---

## Locked rules

### XP (on each result submit)

- Attempt 1→100, 2→80, 3→60, 4+→40
- **+20** completion bonus if `correct`
- Never decreases; ledger in `xp_events`; denormalized `students.xp`
- Level: `max(1, floor(xp / 1000) + 1)` (same as dashboard `levelFromXp`)

### Mastery (per student × objective)

- `not_yet_mastered` — no correct results
- `developing` — ≥1 correct but not mastered
- `mastered` — **≥3** distinct result rows, **≥80%** correct, and **≥2** correct
- Teacher override wins for `effective_status` until cleared; `system_status` still recomputed underneath

### Cosmetics

- `avatar_unlocks` keyed by `min_level`; granted into `student_avatar_unlocks` when level crosses threshold
- API list only — no VR shop UI in M3

---

## New / extended APIs

| Method | Path | Who | Notes |
|---|---|---|---|
| GET | `/student/me` | student | `xp`, `level`, `unlocks`, catalog unlocks |
| POST | `/student/results` / `/vr/results` | student | Returns `{ result, xpAwarded, studentXp, level, newUnlocks, mastery }` |
| GET | `/teacher/mastery` | teacher | Filters: `classId`, `studentId`, `subject`, `objectiveId` |
| GET | `/teacher/students/:id/xp` | teacher | Total, level, recent events, unlocks |
| PATCH | `/teacher/students/:id/mastery/:objectiveId` | teacher | `{ status, note? }` — objective UUID or code |
| DELETE | `/teacher/students/:id/mastery/:objectiveId` | teacher | Clear override |
| GET | `/teacher/results` | teacher | Also includes `mastery` array |

---

## Manual smoke (curl)

### Health

```bash
curl http://localhost:3001/health
```

Expect `"milestone": 3`.

### Submit → XP + developing

After student login, post a correct result once and check `mastery.effectiveStatus` is `developing` and `xpAwarded` is `120`.

### Three correct → mastered

Submit two more correct results for the same objective; expect `mastered` and XP `360`.

### Override

```bash
curl -X PATCH http://localhost:3001/teacher/students/STUDENT_ID/mastery/fractions ^
  -H "Authorization: Bearer TEACHER_TOKEN" ^
  -H "Content-Type: application/json" ^
  -d "{\"status\":\"developing\",\"note\":\"manual review\"}"
```

`effectiveStatus` stays overridden even after further submits until `DELETE` the same path.

---

## Dashboard (FE)

Teacher Learning Objectives / Results / student profile / overview should read **stored** mastery and XP from these APIs — no demo/localStorage mastery when API rows exist; empty = “no evidence yet”.

---

## Out of scope (Week 4+)

PDF/CSV exports, DB-backed recommendations, multiplayer, full VR cosmetics shop.
