# VetOne Cloud — Deploy Runbook

How to package VetOne and stand it up on a cloud box so prospects can self-register 48-hour demo tenants. Companion to `VETONE-CLOUD-DEMO-SPEC.md`.

> **Model:** the VetOne cloud box is a **single-project** deploy — its default DB connection **is** the vet demo DB. All provisioning runs on the default connection; no SaaS project switching needed.

---

## 0. Prerequisites (build host)
- PHP 8.1+, Composer, Node 18+, `mysqldump`, `zip`, a **freshly seeded vet DB**.
- MySQL/MariaDB 10.4+ on the server.

## 1. Seed the template DB (once, on the build host)
A fresh install needs the global lookups + one template business (so the medicine catalog can be cloned per tenant).

```bash
php artisan migrate --force
php artisan db:seed --class=Database\\Seeders\\PermissionsTableSeeder   # if not already
php artisan module:seed VetClinic          # SOAP options, medicine categories/medicines, vaccines, etc.
php artisan module:seed GuidedTour          # default "vet-full-tour" walkthrough
php artisan entitlements:seed --project=vet # vet permissions + default roles
```
> SOAP options + medicine categories are seeded **global** (`business_id = NULL`); the medicine catalog is per-business and is **cloned automatically** into each new demo tenant by the provisioner. AI prompts are built-in code defaults — nothing to seed.

## 2. Package the release (build host)
```bash
scripts/package-vetone.sh --build            # builds PWAs, lean code archive, DB dump
#   --with-vendor   include vendor/ for a turnkey (no composer install) deploy
```
Output: `dist/vetone-release-<stamp>.zip` containing `code.tar.gz`, `db/vetone.sql`, `RUNBOOK.md`, `SPEC.md`.

## 3. Server install
```bash
unzip vetone-release-*.zip && cd vetone-release-*
tar xzf code.tar.gz -C /var/www/vetone
cd /var/www/vetone
composer install --no-dev --optimize-autoloader   # skip if packaged --with-vendor
cp .env.example .env && php artisan key:generate
```

## 4. Configure `.env`
```dotenv
APP_URL=https://vetone.app
APP_ENV=production

DB_DATABASE=vet_one_demo
DB_USERNAME=...
DB_PASSWORD=...

# self-service demo registration expects the vet DB as default (single-project box)
VETONE_DEMO_HOURS=48
VETONE_DEMO_PURGE_DAYS=14
VETONE_DEMO_DOCTORS=3
VETONE_DEMO_CURRENCY=EGP
VETONE_DEMO_TZ=Africa/Cairo

# Tour media is stored on the private 'local' disk and streamed (never public).
GUIDEDTOUR_DISK=local

# Optional: DEMO_LOGIN=true enables the passwordless role chooser at /demo
```

## 5. Import DB + finalize
```bash
mysql -u USER -p vet_one_demo < db/vetone.sql        # or run the migrate+seed of step 1
php artisan migrate --force                          # applies VetOneDemo/GuidedTour/ActivityLog new tables
                                                     # (also installs GuidedTour's version property so the ▶ Tour launcher injects)
php artisan config:cache route:cache view:cache
php artisan storage:link
```
Confirm these modules are enabled in `modules_statuses.json` (they ship enabled in this branch): **PortalApi, VetOneDemo, GuidedTour, ActivityLog, VetClinic, VetAdmission, VetAI, VetPortal, VetStaffPortal, VetSite, AiCore, WhatsApp, SaasSwitcher**.

## 6. Cron (drives the 48h purge)
```cron
* * * * * cd /var/www/vetone && php artisan schedule:run >> /dev/null 2>&1
```
`demo:purge` runs hourly and deletes tenants expired past the 14-day retention (keeping an anonymised stub + audit trail for TPS reporting). Dry-run any time: `php artisan demo:purge --dry-run`.

## 7. PWAs
Pre-built assets ship in `public/app` (pet-owner) and `public/staff-app` (staff), both **runtime-branded per tenant** — no per-tenant rebuild. To rebuild manually:
```bash
( cd pwa-owner && npm ci && npm run build )     # → public/app
( cd pwa-staff && npm ci && npm run build )     # → public/staff-app
```

## 8. Smoke test
1. Visit **`/vetone/register`** → fill the form → submit.
2. You land on the **onboarding gate** (WhatsApp + Groq, both skippable). Skip → dashboard.
3. The **▶ Tour** launcher (bottom-right) starts the walkthrough; replay works.
4. Verify **3 doctors** exist with different schedules; nurses/reception/accounting/HR created per counts; sample pets/appointments present.
5. Owner PWA at **`/app/<slug>`** shows the clinic logo + brand colour; staff PWA at **`/staff-app?t=<slug>`** is themed too.
6. TPS view: **`/activity-insights`** shows the heat-map + the new tenant; **`/activity-insights/tenant/<id>`** shows the trial scorecard.
7. Fast-forward expiry to confirm the lock: `UPDATE business SET demo_expires_at = NOW() - INTERVAL 1 HOUR WHERE id = <id>;` → next request logs out → **`/demo-expired`** (contact TPS).

## 9. Rollback / notes
- Everything is additive + module-only; core files are byte-identical. Disabling `VetOneDemo`/`GuidedTour` in `modules_statuses.json` cleanly removes the feature surface.
- `activity_incidents`, `tour_*`, and `business.demo_*` are the only new tables/columns.
