# MileMath PWA guide

MileMath by Cendariq is a standalone mobile web app at `/milemath/` on the Synergystic Systems parent website. Chesapeake Splash has separate routes and branding. The app works without an account; manual calculations and the first manifest import are free. There is no repeat-import cap in this release.

## Local setup

The app uses the repository's Express server, PostgreSQL database, and raw SQL migration runner. Use a disposable local or test database for development and migration checks.

```sh
npm install
npm run build
DATABASE_URL=postgres://localhost/milemath_dev npm run migrate
DATABASE_URL=postgres://localhost/milemath_dev npm start
```

The server also runs migrations at startup. Run `npm run migrate` a second time against the same disposable database to confirm the migration runner is safe to repeat. Do not use production data for this check.

`npm run build` copies the pinned Tesseract.js browser worker, the LSTM WebAssembly core, and the English model into `public/milemath/vendor/`. Recognition and model loading use same-origin files. The app does not upload images or call a cloud OCR service.

## Environment

- `DATABASE_URL` — PostgreSQL connection string. Required by the application.
- `POLSIA_API_BASE_URL` — Polsia payments API base URL. Required only to offer the optional checkout.
- `POLSIA_API_KEY` — Polsia payments credential. Required only to offer the optional checkout; supply the real company key through the platform secret store.
- `MILEMATH_BASE_URL` — public base URL used for checkout return links. Defaults to `https://synergysticsystems.com`.
- `MILEMATH_ALLOWED_HOSTS` — optional comma-separated hostname allowlist. Defaults to `synergysticsystems.com,www.synergysticsystems.com`; localhost is also allowed for local development. Chesapeake Splash and Cendariq product hosts are always denied MileMath routes.

The source serves the current site root from `public/parent.html`; that source code does not prove public DNS or custom-domain routing. Verify that `https://synergysticsystems.com/` reaches this app and that `/milemath/` and `/milemath/app` are available on that host before announcing the beta.

## Beta and store links

Edit `public/milemath/config.js` independently for each platform. Until a platform is public, leave `public: false` and its `storeUrl` empty; visitors see its email beta signup form. When the iPhone build is public, set only `ios.public` to `true` and set `ios.storeUrl` to its App Store URL. Do the same separately for Android. The other platform keeps its own beta or store action.

## Install the PWA

- **iPhone:** Open `/milemath/app` in Safari, tap Share, then Add to Home Screen.
- **Android:** Open `/milemath/app` in Chrome and use Install app or Add to Home screen from the browser menu.

The service worker caches the MileMath shell and same-origin OCR worker, WebAssembly core, and English model for repeat/offline use. It does not cache APIs, OCR drafts, or user-selected photos.

## Route JSON contract

Saved routes and JSON exports use the same versioned structure. `public/milemath/calculator.js` is the pure calculation module; consumers should reject unsupported versions rather than silently changing their meaning.

```json
{
  "schemaVersion": 1,
  "calculationVersion": 1,
  "name": "Harbor Loop · Sample",
  "manifest": { "payout": 150, "payoutConfirmed": true, "source": "manifest" },
  "review": { "confirmed": true },
  "miles": {
    "paid": 120,
    "unpaidToPickup": 0,
    "unpaidBetweenJobs": 0,
    "unpaidAfterRoute": 0,
    "includesUnpaid": false
  },
  "time": {
    "drivingHours": 2.8,
    "serviceHours": 0.8,
    "waitingHours": 0.2,
    "unpaidTravelHours": 0.2
  },
  "expenses": {
    "vehicleMode": "per_mile_inclusive",
    "vehicleRate": 0.35,
    "fuelCost": 0,
    "tolls": 8,
    "parking": 0,
    "other": 0
  },
  "stops": []
}
```

The serializer includes only confirmed calculator fields and stops. Saved routes and purchase references use IndexedDB. The route serializer does not include source images, image blobs, temporary canvas content, or raw OCR text. In the sample above, expenses are $50, estimated pre-tax profit is $100, profit is $25 per hour, and profit is $0.83 per mile.

## Future Cendariq transfer

A future Cendariq integration can accept only user-selected, confirmed route JSON, validate both version numbers, and run the shared calculation module to preserve the same calculation meaning. A transfer should require explicit user action and should not include images or recognition text. Keep the verified payment session reference separate from route JSON. When Cendariq offers the same unlock, verify the reference server-to-server against the MileMath purchase record before granting access; a browser flag or copied local file is not proof of payment. The current release has no route-transfer endpoint.
