# Notifications & Recommendations Implementation

## Status: ✓ COMPLETED

All hard-coded notifications and recommendations have been replaced with real, data-driven implementations.

---

## Notifications System

### API Endpoint
**File:** `/api/notifications/dashboard.php`
**Method:** GET
**Authentication:** Required (client role)

### Data Sources
The notifications system combines multiple data sources:

1. **Upcoming Appointments** - Session reminders for appointments in next 3 days
2. **Health Recommendations** - Auto-generated based on intake form data
3. **Coach Notes** - From coach_notes table (if available)

### Response Format
```json
{
  "success": true,
  "notifications": [
    {
      "id": "appt_123",
      "type": "appointment",
      "title": "Session Today!",
      "message": "Your session with Coach Name is today at 2:00 PM",
      "icon": "fas fa-calendar",
      "color": "#3b82f6",
      "is_read": false,
      "related_id": 123,
      "created_at": "2026-01-27T14:00:00"
    },
    {
      "id": "rec_stress",
      "type": "recommendation",
      "title": "Stress Management",
      "message": "Your stress level is high. Try meditation or lavender aromatherapy.",
      "icon": "fas fa-spa",
      "color": "#a855f7",
      "is_read": false,
      "created_at": "2026-01-27T10:30:00"
    }
  ],
  "unread_count": 2
}
```

### Notification Types & Colors

| Type | Title | Icon | Color |
|------|-------|------|-------|
| appointment | Session Reminder | fas fa-calendar | #3b82f6 |
| recommendation | Stress/Sleep/Exercise Tips | fas fa-spa | #a855f7 |
| coach_recommendation | Coach Notes | fas fa-leaf | #16a34a |

### Database Table
Automatically created on first use:

```sql
CREATE TABLE IF NOT EXISTS notifications (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    type VARCHAR(50) NOT NULL,
    title VARCHAR(255) NOT NULL,
    message LONGTEXT,
    icon VARCHAR(100),
    color VARCHAR(50),
    is_read BOOLEAN DEFAULT FALSE,
    related_id INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    KEY (user_id, created_at),
    KEY (user_id, is_read)
)
```

---

## Recommendations System

### AI-Generated Recommendations (Intake-Based)
Based on user's intake form responses:

- **High Stress Level (>7):** Stress Management
- **Low Sleep (<7 hours):** Better Sleep
- **No Exercise Logged:** Stay Active  
- **Vegetarian Diet:** Plant-Based Nutrition

### Coach Recommendations API
**File:** `/api/recommendations/coach.php`
**Method:** GET
**Authentication:** Required (client role)

Creates `coach_notes` table automatically:

```sql
CREATE TABLE IF NOT EXISTS coach_notes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    appointment_id INT NOT NULL,
    coach_id INT NOT NULL,
    user_id INT NOT NULL,
    note_type VARCHAR(50) DEFAULT 'general',
    note LONGTEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (appointment_id) REFERENCES appointments(id) ON DELETE CASCADE,
    FOREIGN KEY (coach_id) REFERENCES coaches(id) ON DELETE CASCADE,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    KEY (user_id, created_at),
    KEY (note_type)
)
```

---

## Dashboard Integration

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

Features:
- Real-time notification badge with unread count
- Color-coded by type and importance
- Auto-refreshes every 30 seconds
- Displays icon, title, and message
- Loading state while fetching

### Recommendations Section
**File:** `/dashboard/user/index.php` (lines 210-265)

Two-part system:
1. **System Recommendations** - Based on intake form (always shown if intake exists)
2. **Coach Recommendations** - Loaded dynamically from coach_notes

#### JavaScript Functions
```javascript
loadNotifications() // Fetches and renders notifications (30s refresh)
loadCoachRecommendations() // Fetches and renders coach notes (60s refresh)
```

Initialization on DOMContentLoaded:
- Calls both functions immediately on page load
- Sets intervals for auto-refresh (notifications: 30s, recommendations: 60s)

---

## Real-Time Features

### Notification Updates
- **Upcoming Appointments:** Automatically generates "today/tomorrow/upcoming" messages based on appointment time
- **Health Insights:** Pulled from intake form in real-time
- **Unread Count Badge:** Updated with each fetch, shows active notifications

### Coaching Notes Display
Shows up to 5 most recent coach recommendations with:
- Coach name and display name
- Recommendation date
- Full note text
- Styled with accent border

---

## Use Cases

### For Clients
✓ See upcoming appointments as reminders
✓ Get personalized health tips based on intake responses
✓ Receive coaching guidance and recommendations
✓ Track unread notification count

### For Coaches
(Future feature) Add recommendations/notes to client profiles via admin dashboard

---

## Extensibility

Both endpoints are designed for easy enhancement:

**Add New Notification Types:**
1. Add logic to `/api/notifications/dashboard.php`
2. Define icon, color, and message format
3. Notification automatically appears on dashboard

**Add New Coach Notes:**
1. Coach adds note via admin dashboard (future)
2. Note type: 'recommendation' or 'insight'
3. Automatically fetched and displayed on client dashboard

---

## Security
✓ Authentication required on both endpoints
✓ User-scoped data (user can only see their own notifications/recommendations)
✓ SQL prepared statements
✓ Proper error handling and logging

