# Fogbreak — Master Build Plan

## What Fogbreak Replaces (Full Scope)

Fogbreak is a single, AI-native platform that replaces ALL of these:

| Platform | What It Does | Fogbreak Replacement |
|----------|-------------|----------------------|
| **Follow Up Boss** | CRM, lead management, contact routing | Native CRM with AI lead scoring, routing, nurture |
| **ShowingTime** | Showing scheduling, feedback, route optimization | Native showing engine with AI scheduling |
| **Sisu** | Analytics, KPIs, coaching, leaderboards, goal tracking | Native analytics with AI coaching |
| **Paperless Pipeline** | Transaction coordination, task management, compliance | Native TC engine with jurisdiction templates |
| **DocuSign** | eSignature, envelopes, audit trail | Native eSign (esign.php — needs fixes + features) |
| **RealScout** | Property alerts, saved searches, MLS matching | Native alert engine with AI-powered matching |
| **Luxury Presence** | Contact enrichment, home valuation, private listings, collaborative boards, voice notes, ad management, blog engine, AI lead nurture | Native enrichment, valuation, private listings hub, boards, voice transcription, ad manager, blog + SEO, AI nurture |

**Plus capabilities NONE of them offer:**
- Self-hosted AI agent/chatbot (free, no API costs)
- AI auto-marketing: copy generation, social posting, virtual staging, video/reels, photo enhancement
- White-label multi-tenant (any brokerage can brand it as their own)
- Zero geographic hardcoding (scales city → state → nationwide)
- Native iOS + Android apps
- Autonomous AI agents with human approval gates (LP auto-publishes; Fogbreak queues for review)

## Existing Codebase (43,742 lines)

Already built:
- `app/api/admin.php` — Core CRUD, phase tracking (1,674 lines)
- `app/api/email.php` — Email hub, inbox, SMTP, Fair Housing audit (1,175 lines)
- `app/api/drips.php` — Drip campaigns, enrollments, execution (1,542 lines)
- `app/api/properties.php` — Listings, saved searches, alerts (1,528 lines)
- `app/api/transactions.php` — Deals, 9 jurisdiction checklists (1,071 lines)
- `app/api/deadlines.php` — Relative due dates, dependencies (1,200 lines)
- `app/api/commissions.php` — Splits, tiers, forecasting (1,373 lines)
- `app/api/documents.php` — Upload, versioning, sharing (1,604 lines)
- `app/api/calendar.php` — Events, Google Calendar sync (937 lines)
- `app/api/tenants.php` — Multi-tenant white-label (954 lines)
- `app/api/esign.php` — eSignature engine, 70% complete (2,793 lines)
- `app/api/cron.php` — 8 automated jobs (607 lines)
- `app/fogbreak.html` — 8-tab SPA dashboard (2,622 lines)
- `app/admin.html` — Admin dashboard with AI panel stub
- Plus portal.html, sign.html, 4 JS modules, CSS, service worker

**New LP Feature Parity modules:**
- `app/api/enrichment.php` — Contact data enrichment + deal probability scoring (1,100 lines)
- `app/api/valuation.php` — Home valuation widget + CMA builder (1,100 lines)
- `app/api/private_listings.php` — Off-market listings + buyer matching (1,050 lines)
- `app/api/boards.php` — Collaborative buyer/seller boards (1,100 lines)
- `app/api/voice_notes.php` — Voice transcription + CRM extraction (823 lines)
- `app/api/ads.php` — Paid ad campaign manager (1,247 lines)
- `app/api/blog.php` — AI blog engine + SEO optimization (1,250 lines)
- `app/api/ai_nurture.php` — AI lead nurture engine (1,200 lines)

Known issues:
- 232 hardcoded Peninsula geographic references across 20 files
- esign.php: SQL injection line 758, hardcoded tenant_id=1 in 8 places, missing tenant filters, unvalidated PIN, no deal ownership check
- No AI layer connected (admin.html has UI stub only)
- Social post generation is template-string, not AI-powered
- No showing management system
- No analytics/KPI/coaching dashboards

---

## Run Order

Paste each file into Claude Code as a single instruction. Run in order. Parallel groups noted.

### Phase 0: Foundation (sequential, run first)
1. `01-GEOGRAPHIC-EXTRACTION.md` — Extract 232 hardcoded references into database templates
2. `02-DATABASE-REDESIGN.md` — PostgreSQL migration, row-level security, market config tables

### Phase 1: Platform Replacements (parallel after Phase 0)
3. `03-SELF-HOSTED-AI.md` — Ollama + Llama/Mistral, FastAPI proxy, chatbot backend
4. `04-FOLLOW-UP-BOSS-REPLACEMENT.md` — Upgrade CRM: AI lead scoring, smart routing, pipeline automation
5. `05-SHOWINGTIME-REPLACEMENT.md` — Showing scheduling, feedback, routes, seller portal
6. `06-SISU-REPLACEMENT.md` — KPI dashboards, leaderboards, coaching, pacing, TV-mode
7. `07-PAPERLESS-PIPELINE-REPLACEMENT.md` — TC engine: task automation, compliance, workflows
8. `08-DOCUSIGN-REPLACEMENT.md` — Fix esign.php security, add entity signers, signing groups, anchors
9. `09-REALSCOUT-REPLACEMENT.md` — AI property matching, smart alerts, market-aware recommendations

### Phase 2: AI Marketing Suite (parallel after 03)
10. `10-AI-COPY-ENGINE.md` — AI listing descriptions, social copy, email campaigns, Fair Housing
11. `11-SOCIAL-MEDIA-AUTOMATION.md` — Meta/TikTok/LinkedIn APIs, content calendar, auto-post
12. `12-VIRTUAL-STAGING.md` — Collov/RoomX integration, staging credits, before/after
13. `13-VIDEO-REEL-GENERATION.md` — Runway + ElevenLabs, property videos, auto-post
14. `14-PHOTO-ENHANCEMENT.md` — Sky replacement, HDR, color correction

### Phase 3: Client Apps (after Phase 1 stable)
15. `15-NEXTJS-WEB-APP.md` — Next.js 16 rebuild, SSR listings, white-label themes
16. `16-REACT-NATIVE-MOBILE.md` — iOS + Android, push notifications, offline sync
17. `17-CLIENT-PORTAL.md` — Transaction roadmap, signing, countdown, vendor ordering

### Phase 4: LP Feature Parity (parallel after Phase 1)
18. `24-CONTACT-ENRICHMENT.md` — Auto-enrich contacts, life events, deal probability scoring
19. `25-HOME-VALUATION-CMA.md` — Instant home valuation widget + CMA presentation builder
20. `26-AD-CAMPAIGN-MANAGER.md` — Google/Meta/TikTok/LinkedIn ad management with AI optimization
21. `27-PRIVATE-LISTINGS-HUB.md` — Off-market listings, visibility controls, buyer matching
22. `28-GOOGLE-ONE-TAP-SSO.md` — Google One Tap + Apple Sign-In + progressive lead capture
23. `29-VOICE-NOTES-CRM.md` — Voice recording, Whisper transcription, AI entity extraction
24. `30-COLLABORATIVE-BOARDS.md` — Shared agent-client workspaces (Pinterest meets deal management)
25. `31-AUTONOMOUS-AI-AGENTS.md` — 5 autonomous AI agents (SEO, Blog, Ads, Nurture, Social)
26. `32-BLOG-ENGINE.md` — Full blog CMS with AI content generation and SEO scoring
27. `33-AI-LEAD-NURTURE.md` — 24/7 AI lead engagement, conversation intelligence, human handoff
28. `34-ENHANCED-IDX-SEARCH.md` — Split-screen map IDX, neighborhood pages, advanced filters
29. `35-WEBSITE-TEMPLATE-ENGINE.md` — Public-facing website builder, templates, visual editor, custom domains, analytics

### Phase 5: Scale (ongoing)
29. `18-MLS-INTEGRATION.md` — RETS/RESO feeds, auto-update, price reduction alerts
30. `19-SMS-TWILIO.md` — Two-way SMS, SMS drips, Twilio
31. `20-ADVANCED-ANALYTICS.md` — ROI by source, GCI forecast, agent profitability
32. `21-SELF-HOST-MIGRATION.md` — vLLM on Kubernetes, fine-tuning, production inference

---

## Architecture

```
Fogbreak/
├── app/                          # PHP backend (existing, refactored)
│   ├── api/                      # API modules
│   │   ├── config.php            # DB config (gitignored)
│   │   ├── admin.php             # Core CRUD → upgraded with AI endpoints
│   │   ├── auth.php              # Login, sessions, magic links
│   │   ├── email.php             # Email hub + Fair Housing
│   │   ├── drips.php             # Drip campaigns → AI-powered content
│   │   ├── properties.php        # Listings → AI matching (RealScout replacement)
│   │   ├── transactions.php      # Deals → database-driven compliance
│   │   ├── deadlines.php         # Deadlines → template-based
│   │   ├── commissions.php       # Commissions + forecasting
│   │   ├── documents.php         # Document management
│   │   ├── calendar.php          # Calendar + Google sync
│   │   ├── tenants.php           # Multi-tenant
│   │   ├── esign.php             # eSign → security fixes + entity signers
│   │   ├── cron.php              # Automated jobs
│   │   ├── showings.php          # NEW: ShowingTime replacement
│   │   ├── analytics.php         # NEW: Sisu replacement
│   │   ├── coaching.php          # NEW: AI coaching engine
│   │   ├── marketing.php         # NEW: AI marketing orchestrator
│   │   ├── social.php            # NEW: Social media posting
│   │   ├── staging.php           # NEW: Virtual staging proxy
│   │   ├── video.php             # NEW: Video generation proxy
│   │   ├── sms.php               # NEW: Twilio SMS
│   │   ├── markets.php           # NEW: Market template CRUD
│   │   ├── enrichment.php        # NEW: Contact enrichment + deal detection
│   │   ├── valuation.php         # NEW: Home valuation + CMA
│   │   ├── private_listings.php  # NEW: Off-market listings hub
│   │   ├── boards.php            # NEW: Collaborative boards
│   │   ├── voice_notes.php       # NEW: Voice transcription + CRM
│   │   ├── ads.php               # NEW: Ad campaign manager
│   │   ├── blog.php              # NEW: Blog engine + SEO
│   │   ├── ai_nurture.php        # NEW: AI lead nurture
│   │   ├── website_templates.php # NEW: Website template engine
│   ├── fogbreak.html            # Legacy PWA (kept until Next.js ready)
│   └── ...
├── ai/                           # NEW: AI service layer
│   ├── server.py                 # FastAPI inference proxy
│   ├── agents/                   # Agent definitions
│   │   ├── chatbot.py            # Client-facing chatbot
│   │   ├── lead_scorer.py        # AI lead scoring
│   │   ├── copywriter.py         # Listing/social/email copy
│   │   ├── coach.py              # Agent coaching AI
│   │   ├── market_analyst.py     # CMA/market reports
│   │   └── fair_housing.py       # Compliance checker
│   ├── prompts/                  # System prompts (market-aware)
│   └── models/                   # Ollama model configs
├── web/                          # NEW: Next.js 16 frontend
│   ├── src/app/                  # App Router pages
│   ├── src/components/           # UI components
│   ├── src/lib/                  # API client, auth
│   └── src/themes/               # White-label themes
├── mobile/                       # NEW: React Native
│   ├── src/screens/
│   ├── src/components/
│   └── src/services/
├── db/                           # NEW: Database migrations
│   ├── migrations/
│   ├── seeds/
│   └── market-templates/         # YAML/JSON market configs
├── claude-code-instructions/     # These files
├── CLAUDE.md                     # Product memory
└── Fogbreak_Research_Report.docx
```

## Key Decisions
- **Self-hosted AI (free):** Ollama locally → vLLM on Kubernetes at scale
- **Models:** Llama 3.3 70B (reasoning), Mistral 7B (chatbot), both free/open-source
- **Database:** PostgreSQL with row-level security (migrate from MySQL)
- **Frontend:** Next.js 16 (web) + React Native (iOS/Android)
- **Backend:** PHP stays, Python FastAPI sidecar for AI
- **Zero geography in code:** Database-driven market templates
- **White-label:** Per-tenant branding, domains, feature flags
- **Replaces 7 platforms:** FUB, ShowingTime, Sisu, Paperless Pipeline, DocuSign, RealScout, Luxury Presence
- **LP Feature Parity:** Contact enrichment, home valuation, private listings, collaborative boards, voice notes, ad management, blog engine, AI lead nurture — matching/exceeding Luxury Presence capabilities
- **Autonomous AI with approval gates:** Unlike LP's auto-publish, Fogbreak queues AI output for human approval (compliance advantage)
