# Diabeasy — Build Plan v1.0

## Assumptions I've locked (tell me if any are wrong)

| Decision | Choice | Why |
|---|---|---|
| WhatsApp provider | **Meta Cloud API direct** | No per-message reseller markup. Wrapped in an adapter class so AiSensy/Gupshup/Interakt can be swapped without touching business logic. |
| AI extraction | **Claude API** (Sonnet) | Vision model reads photographed reports directly. Already used at Caresoft in support-mail-assist. |
| Stack | PHP 8.2 + MySQL 8.0, no framework | Matches your team. Thin custom core, PDO, no Composer bloat. |
| File storage | **Outside webroot**, served via authenticated proxy | DPDP requirement. A lab report must never be reachable by guessing a URL. |
| Async work | DB-backed `job_queue` + cron worker | No Redis dependency on shared hosting. |
| Verification | **Human verifies every report before delivery in Phase 1** | Extraction accuracy on Indian lab photos is ~85–90%. Automate only after 2,000+ verified samples. |

---

## Folder structure

```
/diabeasy
├── public_html/                  ← webroot
│   ├── index.php                 landing (CMS-driven)
│   ├── blog/  faq/  about/  privacy/  terms/
│   ├── app/                      patient portal (login required)
│   │   ├── dashboard.php  patients.php  reports.php
│   │   ├── trends.php     care-gaps.php  profile.php
│   ├── admin/                    admin panel (2nd access level)
│   │   ├── verify.php            ← report verification console
│   │   ├── patients.php  reports.php  escalations.php
│   │   ├── cms/  blog/  testimonials/  faqs/  media/
│   │   ├── whatsapp/             templates, message log, broadcast
│   │   └── settings.php  users.php  audit.php
│   ├── api/
│   │   ├── wa-webhook.php        ← Meta webhook (GET verify + POST receive)
│   │   ├── upload.php            portal report upload
│   │   └── file.php              authenticated file proxy
│   ├── assets/  css/ js/ img/
│   ├── sitemap.php  robots.txt  llms.txt   ← AEO
│
├── app/                          ← NOT web accessible
│   ├── config/config.php
│   ├── core/    DB.php Auth.php Session.php Csrf.php Log.php Response.php
│   ├── lib/
│   │   ├── WhatsApp/  CloudApiClient.php  MessageRouter.php  StateMachine.php  MediaDownloader.php
│   │   ├── Reports/   Ingestor.php  Extractor.php  Normaliser.php  FlagEngine.php
│   │   │               SummaryGenerator.php  CardRenderer.php  Comparator.php
│   │   ├── Care/      GapEngine.php  RecallScheduler.php  EscalationEngine.php
│   │   ├── Cms/       PageRepo.php  BlogRepo.php  SeoBuilder.php  SchemaBuilder.php
│   │   └── Ai/        AnthropicClient.php  Prompts.php
│   ├── workers/  queue-worker.php  cron-recalls.php  cron-gaps.php  cron-purge.php
│   └── views/    layouts, partials, email + WA templates
│
└── storage/                      ← NOT web accessible
    ├── patients/{patient_uuid}/reports/{YYYY}/{report_uuid}.{ext}
    ├── patients/{patient_uuid}/cards/
    ├── cache/  logs/  tmp/
```

**Auto-download rule:** every WhatsApp media message is downloaded by the queue worker into
`storage/patients/{patient_uuid}/reports/{YYYY}/` within seconds of arrival. The DB stores only the
relative path. Nothing is ever written into the webroot.

---

## WhatsApp conversation flow (Phase 1)

```
User sends photo/PDF
   │
   ├─ New number? → consent message (DPDP) → name → "Whose report is this?"
   │                                                  ├─ Mine
   │                                                  └─ Father / Mother / Other → create patient
   │
   ├─ Known number, multiple patients? → quick-reply buttons to pick patient
   │
   ▼
"Got it. Reading your report — I'll send the summary in a few minutes." 
   │
   ├─ download media → store in patient folder
   ├─ extract (Claude vision) → report_values
   ├─ flag engine → red flag? → escalation + "please see a doctor today" + human call
   ├─ human verification queue  ← admin/verify.php
   ▼
Deliver: summary + what's good + what to watch + do/don't + missing tests
         + comparison vs last report + shareable card image
   │
   └─ 3 months later → recall template: "Your HbA1c is due"
```

**Language:** detect from user's message; default English, offer Hindi. Store in `users.preferred_lang`.

---

## Phase roadmap

**Phase 1 — Foundation** ✅ this delivery
Schema, seed data (18 lab parameters, 8 care-gap rules), folder structure, architecture decisions.

**Phase 2 — Core PHP** · config, DB, auth, session, CSRF, logging, queue worker
**Phase 3 — WhatsApp** · webhook, signature verification, state machine, media auto-download
**Phase 4 — Extraction** · Claude vision prompt, JSON schema, normaliser, synonym matching, flag engine
**Phase 5 — Summary** · plain-language generator, do/don't rules, red-flag escalation, comparison vs previous
**Phase 6 — Verification console** · side-by-side report image + extracted values, one-click approve
**Phase 7 — Patient portal** · dashboard, report library, graphical trends, care-gap tracker, family switcher
**Phase 8 — Shareable card** · branded PNG report card for WhatsApp forwarding
**Phase 9 — Landing + CMS** · homepage, blog, testimonials, FAQ, admin CMS
**Phase 10 — SEO / AEO / GEO** · JSON-LD, sitemap, `llms.txt`, FAQPage schema, city landing pages
**Phase 11 — Recalls** · 3-month cron, WA template broadcast, snooze handling
**Phase 12 — Phase-2 commerce** · lab test booking on partner rails, then food subscriptions

---

## Guardrails wired into the design

1. **Never prescribe.** Summaries explain, flag and route. No dose advice, no "stop this medicine", no diagnosis. Do/don't lists cover lifestyle and screening only.
2. **Red flags always escalate.** Critical values trigger an immediate "see a doctor today" message plus a human callback logged in `escalations`.
3. **Nothing auto-delivered unverified** until accuracy is proven. `settings.require_human_verification` controls this.
4. **Separate consent for partner sharing.** Signup consent does not cover handing data to a lab or pharmacy in Phase 2.
5. **Every report view is audited.** `audit_log` records who opened which record.

---

## The number that decides whether this works

At month 3: **what % of users send a second report without being chased?**
Under 20% and the value proposition isn't real — stop cheaply. Signups are vanity; the second upload is the product.
