# Task 14 — Volunteer History, Stats, Leaderboard

Three small screens for volunteer side. Group together since they share data.

## Files

```
apps/web/src/app/volunteer/history/page.tsx
apps/web/src/app/volunteer/stats/page.tsx
apps/web/src/app/volunteer/leaderboard/page.tsx
```

## History page

List the volunteer's past vihars (status in completed/auto_closed):

```
GET /api/vihars?page=1&pageSize=20
# Filter client-side OR add server-side filter:
# WHERE EXISTS (SELECT 1 FROM vihar_volunteer WHERE vihar_id = v.vihar_id AND user_id = ${currentUser.userId})
```

Better: server-side filter via a new endpoint or a flag. For Phase 1, filter
client-side from the same vihars list.

Layout: card per vihar with date, route, distance, "tap for details".

## Stats page

Reuse `/api/stats/me` from Task 12. Show:
- Big tiles: lifetime vihars, lifetime km, current month vihars, current month km
- Comparison badge: "↑ 12% vs last month" (saffron) or "↓ 8%" (rust)
- Streak: "3 weeks in a row" — Phase 2 calculation, hide if not implemented
- City rank: "You're #14 of 387 in South Mumbai"
- Mini line chart: last 6 months of monthly counts (data from `/api/stats/me?period=this_year`
  with grouping)

## Leaderboard page

Reuse `/api/stats/leaderboard` from Task 12.
- Tabs: This Month / This Year / Lifetime
- Toggle: By km / By count
- Optional locality filter
- Table:
  | Rank | Name | Vihars | Km |
  - Highlight current user's row in saffron, regardless of position
  - If user is below #10, show top 10 + "..." + user's row

## Acceptance

1. History: see past vihars in reverse-chronological order.
2. Tap a row → vihar detail (read-only summary).
3. Stats: tiles populate, comparison shows trend.
4. Leaderboard: top 10 visible. Your row highlighted.
5. Switch metric → order changes.
6. Switch period → list updates.

## Gotchas

- "Lifetime" period needs an open-ended `from` date. Server already handles
  this via `period=lifetime`.
- Leaderboard ordering ties: name A-Z as tiebreak (already in Task 12 SQL).
- If the user has zero vihars, stats page shows zeros gracefully — don't
  divide by zero in comparison %.

## Out of scope

- Badges / achievements UI — Phase 2 (data isn't computed yet)
- Sharing your stats card on WhatsApp — Phase 2
