# 06 — Sisu Replacement (Analytics, Coaching, Leaderboards)

## Context
Fogbreak replaces Sisu for all analytics, KPI tracking, coaching, and team management. Sisu charges $99 base + $15/agent/month. Fogbreak includes it free, with AI coaching Sisu doesn't have.

## Full Feature Set to Build

### 1. Real-Time KPI Dashboard
Create `app/api/analytics.php`:
- 20+ pre-built reports covering performance, conversion, effort, profitability
- Real-time metric updates (no stale data)
- Personal dashboards per agent
- Team dashboards with roll-ups
- TV-mode: optimized for 60-inch office displays (large fonts, auto-rotate metrics)
- Custom date range filters (today, this week, MTD, QTD, YTD, custom)

Key metrics to track:
- Calls made, texts sent, emails sent
- Appointments set / held
- Offers written / accepted
- Listings taken
- Closings
- GCI (Gross Commission Income)
- Volume (total sales $)
- Average sale price
- Days on market
- List-to-close ratio
- Lead conversion rate (by source)
- Speed-to-lead (average response time)
- Pipeline value (open deals)
- Showing-to-offer ratio
- Feedback response rate

### 2. Agent Performance Tracking
- `agent_metrics_daily` table aggregates all activity per agent per day
- Cron job runs nightly to calculate metrics from raw data (emails, showings, deals, calls)
- Historical trend analysis (compare this month to last month, this year to last)
- Performance vs. goals (pacing dashboard)
- Custom metrics via JSONB column (each brokerage can add their own)

### 3. Leaderboards & Gamification
Create `app/api/leaderboards.php`:
- Real-time leaderboards by any metric
- Multiple simultaneous leaderboards (closings, GCI, calls, listings)
- Team vs. team leaderboards
- Period-based: daily, weekly, monthly, quarterly, yearly
- Sales challenges with start/end dates and prizes
- Point systems (assign point values to activities)
- Podium animation for TV display

### 4. Goal Setting & Pacing
- Agents set personal goals (monthly closings, GCI, calls)
- Managers set team goals
- Pacing dashboard: "You're 73% to your monthly goal with 8 days remaining"
- AI pacing alerts: "At current pace, you'll close 4 of your 6-deal goal. Increase calls by 20% to get on track."
- Visual progress bars (green = ahead, yellow = on pace, red = behind)

### 5. AI Coaching Engine
Create `app/api/coaching.php`:
- AI analyzes agent metrics and generates coaching recommendations
- Examples: "Agent X has high showing count but low offer count — coach on buyer consultation"
- Coaching session scheduler
- Session notes with AI-generated action items
- Follow-up tracking on action items
- Praise/constructive feedback messaging system
- AI coaching for ISAs, TCs, team leads (not just agents)

### 6. Pipeline Visualization
- Funnel view: leads → contacts → appointments → showings → offers → closings
- Conversion rates between each stage
- Pipeline value by stage
- Stalled deal detection (deals stuck in a stage too long)
- Forecasted closings for next 30/60/90 days

### 7. ROI Reporting by Lead Source
- Track every dollar spent on lead generation
- Attribution: which source generated which leads → which closed
- Cost-per-lead by source
- Cost-per-closing by source
- ROI per source (revenue / spend)
- Recommendation: "Zillow leads cost $85/each but close at 2.1%. Realtor.com leads cost $45 and close at 3.4%. Shift $2K/month from Zillow to Realtor.com."

### 8. Financial Analytics
Extend `commissions.php`:
- GCI forecasting from pipeline (weighted by probability per stage)
- Net income after splits, fees, expenses
- Income by agent, by team, by office
- Expense tracking with categorization
- Profitability per agent (revenue minus cost to service)
- Year-over-year comparison

### 9. Team Communication
- Built-in messaging center with @mentions
- Context-specific messaging (discuss within a deal, a listing, a coaching session)
- Daily task digests via email
- Discussion threads
- Notification preferences

## Frontend: New Analytics Tab
Add to `fogbreak.html` (or build in Next.js):
- Dashboard with configurable widget grid
- Drag-and-drop widgets (charts, metrics, leaderboards)
- Full-screen TV mode (auto-rotate every 30 seconds)
- Agent detail drilldown
- Date range picker
- Export to PDF/CSV

## Cron Jobs
- `calculate_daily_metrics`: Nightly, aggregate raw data into agent_metrics_daily
- `update_leaderboards`: Every 15 minutes, recalculate rankings
- `pacing_alerts`: Daily, check goals vs. actuals, send alerts
- `ai_coaching_digest`: Weekly, AI generates coaching insights per agent
- `pipeline_forecast`: Daily, recalculate pipeline weighted value

## Acceptance Criteria
- [ ] 20+ KPI metrics tracked in real-time
- [ ] Personal and team dashboards
- [ ] TV-mode display optimized for office screens
- [ ] Leaderboards with multi-metric, multi-period support
- [ ] Sales challenges with gamification
- [ ] Goal setting with pacing dashboards and AI alerts
- [ ] AI coaching engine with recommendations and session tracking
- [ ] Pipeline funnel visualization with conversion rates
- [ ] ROI reporting by lead source with AI recommendations
- [ ] GCI forecasting and financial analytics
- [ ] All features tenant-aware and market-aware
