# MediBook AI — Milestones 1 + 2 (Foundation + Clinic Core)

Dental Clinic SaaS · Laravel 12 · PHP 8.3 · MySQL 8
**Web-only MVP** — mobile-first responsive UI, PWA-ready (manifest + service
worker, installable). No Flutter app and no standalone API in this release.

**M1 delivers:** multi-tenant core (subdomain resolution), clinic signup with
14-day trial, authentication + TOTP 2FA (Fortify), role-based permissions
(Spatie), subscription gate middleware (trial/active/grace/suspended),
ar/en + RTL + dark-mode landing page, installer command, seeders
(roles, single $35 / 50,000 IQD plan), login history, tenancy isolation tests.

**M2 adds:** dentists + weekly schedules + exceptions, patients CRM,
booking engine with two-layer double-booking prevention, availability engine,
public booking page (with clinic WhatsApp button), FullCalendar day/week/month
with drag-&-drop, status state machine, dashboard KPIs + chart, 18 feature
tests. See docs/M2-NOTES.md.

**M3 adds:** the FDI dental chart centerpiece (interactive 32-tooth, surfaces,
per-tooth history), procedures catalog (ar/en, seeded), treatment plans with
automatic chart updates + mPDF Arabic PDFs, before/after comparison slider,
X-ray viewer with zoom/pan, clinical timeline, mobile bottom nav + PWA
scaffolding. New dependency: `composer require mpdf/mpdf`.
See docs/M3-NOTES.md.

**M4 adds:** invoices (plan→invoice + custom, per-tenant numbering, Arabic
PDFs), installment schedules with 250-IQD rounding and a collections screen,
append-only payment ledger with one-click quick pay (cash / Zain Cash / card /
transfer), outstanding-balance dashboard widgets, revenue analytics by
category/procedure + top services, and patient debt indicators.
See docs/M4-NOTES.md.

**M5 adds:** the WhatsApp automation engine — pluggable drivers (Meta Cloud /
Twilio / log), conversation log with delivery status + failure reasons, a
retry queue with backoff, confirmation/24h/1h reminders, recall (next-visit)
reminders, installment-due reminders, a review gateway (Google vs internal),
editable bilingual clinic templates, and PDF attachments via signed links.
See docs/M5-NOTES.md.

**M6 adds:** the SaaS billing layer — 14-day no-card trial, demo clinic,
soft usage limits (WhatsApp/storage/staff/dentists) with warning banners,
multi-channel expiry notices (in-app + email + WhatsApp), a fully audited
manual payment workflow (proof upload → super-admin approve/reject/info),
the super-admin financial dashboard (MRR/ARR, active/trial/expiring/suspended
counts, churn, conversion funnel, revenue by gateway & method), MRR tracking,
and a unified billing activity log. See docs/M6-NOTES.md and docs/DEPLOYMENT.md.

## Install (this repo is an overlay on a fresh Laravel 12 app)

```bash
composer create-project laravel/laravel medibook "^12.0"
cd medibook
composer require laravel/fortify spatie/laravel-permission
# copy this overlay into the project root (it adds/overwrites files):
cp -R ../medibook-m1/* .
cp .env.example .env && php artisan key:generate
# edit .env: DB_*, MEDIBOOK_CENTRAL_DOMAIN=medibookai.test
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan medibook:install        # checks env, migrates, seeds, creates super admin
php artisan serve
```

Local subdomains: add to /etc/hosts → `127.0.0.1 medibookai.test demo.medibookai.test`

## Domains
- `medibookai.test`              → landing, signup, super-admin (`/admin`)
- `{clinic}.medibookai.test`     → tenant login + dashboard

## Key paths
- Tenancy: `app/Support/Tenancy.php`, `app/Http/Middleware/ResolveTenant.php`,
  `app/Models/Concerns/BelongsToTenant.php`
- Subscription gate: `app/Http/Middleware/EnsureSubscriptionIsActive.php`
- Signup → tenant + owner + trial: `app/Http/Controllers/Auth/RegisterClinicController.php`
- Tenant-scoped login + 2FA: `app/Providers/FortifyServiceProvider.php`

## Production (summary; full guides ship in M7)
- cPanel: docroot → `/public`, wildcard subdomain `*` → same docroot,
  cron `* * * * * php /home/USER/medibook/artisan schedule:run`
- VPS: nginx server_name `medibookai.com *.medibookai.com`, php-fpm 8.3,
  supervisor `queue:work` (M3+ uses queues heavily)
