# Activity Feed & History Implementation

## Status: ✓ COMPLETED

A comprehensive activity tracking system with enriched data, pagination, and filtering has been implemented.

---

## Activity Sources

The activity feed aggregates data from multiple sources:

1. **Completed Appointments** - Sessions with coaches (completed status)
2. **Orders/Purchases** - Store orders with total amount
3. **Health Data Entries** - Blood pressure, heart rate, sleep, weight logs
4. **Medical Reports** - Uploaded documents with file size
5. **Intake Forms** - Health assessment submissions

---

## API Endpoint

**File:** `/api/users/activity-feed.php`
**Method:** GET
**Authentication:** Required (client role)

### Query Parameters
- `type` (optional): Filter by type - 'all', 'appointment', 'order', 'health', 'document', 'intake'
- `limit` (optional, default: 20, max: 100): Items per page
- `offset` (optional, default: 0): Pagination offset

### Response Format
```json
{
  "success": true,
  "activities": [
    {
      "id": 123,
      "type": "appointment",
      "action": "Completed session",
      "status": "completed",
      "description": "Session with Coach Smith",
      "created_at": "2026-01-27T14:00:00",
      "time_ago": "2 days ago",
      "formatted_date": "Monday, January 27, 2026 2:00 PM",
      "duration_minutes": 60
    },
    {
      "id": 456,
      "type": "order",
      "action": "Made purchase",
      "status": "confirmed",
      "description": "Order #456 - 3 item(s)",
      "created_at": "2026-01-25T10:30:00",
      "time_ago": "4 days ago",
      "formatted_date": "Saturday, January 25, 2026 10:30 AM",
      "total_amount": "89.99"
    },
    {
      "id": 789,
      "type": "health",
      "action": "Logged blood pressure",
      "status": "logged",
      "description": "BP: 120/80",
      "created_at": "2026-01-20T08:00:00",
      "time_ago": "9 days ago",
      "formatted_date": "Sunday, January 20, 2026 8:00 AM"
    }
  ],
  "pagination": {
    "offset": 0,
    "limit": 20,
    "total": 156,
    "has_more": true
  },
  "type_counts": {
    "appointments": 24,
    "orders": 12,
    "health": 45,
    "documents": 8,
    "intakes": 3
  }
}
```

---

## Activity Types & Status

### Types
| Type | Icon | Color | Description |
|------|------|-------|-------------|
| appointment | fas fa-calendar-check | #3b82f6 | Completed coaching sessions |
| order | fas fa-shopping-bag | #8b5cf6 | Store purchases |
| health | fas fa-heartbeat | #ef4444 | Health metrics logged |
| document | fas fa-file-pdf | #f59e0b | Medical reports uploaded |
| intake | fas fa-clipboard-list | #10b981 | Intake forms submitted |

### Status Badges
| Status | Color | Meaning |
|--------|-------|---------|
| completed | #059669 | Action finished |
| confirmed | #3b82f6 | Order confirmed |
| pending | #f59e0b | Awaiting action |
| logged | #10b981 | Data recorded |
| uploaded | #8b5cf6 | File stored |
| submitted | #06b6d4 | Form submitted |

---

## Dashboard Integration

### Notifications Section
**File:** `/dashboard/user/index.php` (lines 355-387)

Features:
- Shows latest 10 activities
- Type filtering (All, Sessions, Orders, Health, Documents)
- Pagination controls (10 items per page)
- "View All" link to full activity history page

### Activity Feed Data Displayed
- Activity icon with color-coded type
- Action label and status badge
- Full description
- Time ago or formatted date
- Relevant details (duration, amount, file size)

---

## Activity History Page

**File:** `/dashboard/user/activity.php`

Full-featured activity history page with:

### Features
✓ Display all activity across all types
✓ Type filtering (6 categories)
✓ Pagination (20 items per page)
✓ Activity type statistics
✓ Detailed activity cards
✓ Hover effects and animations
✓ Smooth scrolling to top on page change

### Statistics Cards
Shows total count for:
- Sessions (completed appointments)
- Orders placed
- Health data entries
- Documents uploaded
- Intake forms submitted

### Filtering
Six filter buttons:
1. **All Activity** - Show everything
2. **Sessions** - Only completed appointments
3. **Orders** - Only purchases
4. **Health Data** - Only health metrics
5. **Documents** - Only medical reports
6. **Intake Forms** - Only assessments

### Pagination Controls
- Previous/Next buttons
- Page number buttons (shows first 5 pages)
- Smart navigation (disabled when at start/end)
- Scroll to top on page change

---

## Data Enrichment

### Time Formatting
Activities show relative time:
- "Just now" (0 hours)
- "23 minutes ago" (< 1 hour)
- "2 hours ago" (same day)
- "Yesterday"
- "5 days ago"
- "Jan 20, 2026" (older than 7 days)

Plus full formatted date: "Monday, January 27, 2026 2:00 PM"

### Additional Details
- **Appointments:** Duration in minutes
- **Orders:** Total amount in USD
- **Documents:** File size in MB
- **Health:** Specific metric (BP, HR, sleep, weight)

---

## Frontend Functions

### Dashboard Activity Feed
```javascript
loadActivityFeed()           // Fetch and render activity
renderActivityFeed(data)     // Render activity list
renderPaginationControls()   // Show pagination buttons
filterActivityType(type)     // Filter by type
```

### Activity History Page
```javascript
loadActivityHistory()        // Fetch full history
renderActivityHistory(data)  // Render detailed cards
renderStats(counts)          // Display statistics
renderPaginationControls()   // Advanced pagination
filterActivityType(type)     // Filter functionality
```

---

## Security
✓ User-scoped data (only see own activities)
✓ Authentication required
✓ SQL prepared statements
✓ Proper error handling
✓ Pagination prevents data dumps

---

## Performance Optimizations

1. **Pagination** - Limits database queries (default 20 items)
2. **Filtering** - Reduces data processing on client
3. **Lazy Loading** - Activity loads on page load, not pre-rendered
4. **Efficient Queries** - Single query with UNION for all types

---

## Future Enhancements

- Export activity as PDF
- Share activity timeline with coach
- Search and advanced filtering
- Activity notifications/alerts
- Activity commenting system
- Custom date range filtering
- Activity analytics/trends

