# Firebase setup for Guest Welcome App

This app supports multiple lodges. Each admin is assigned to one lodge, private editing data is stored per lodge, and guest-facing content is published to a public lodge document. A guest link selects a lodge with `?lodge=<lodgeId>`.

## Firebase project

The Firebase project is `guests-app-welcome-2026`. The browser SDK configuration is stored in [`firebase-config.js`](./firebase-config.js). The web app uses Firebase Authentication and Cloud Firestore.

## Data layout

Create one admin assignment per Authentication user:

```text
admins/{userUid}
  enabled: true
  lodgeId: "default"
  role: "owner"
```

Each lodge has separate admin content and a published guest document:

```text
lodges/{lodgeId}/content/main
publicLodges/{lodgeId}
```

The app reads and writes the assigned admin's private content document. Saving changes writes both lodge documents in one batch. Guests read only the `publicLodges` document selected by the URL.

## One-time migration from `site/content`

Do these steps in order so the existing live content stays available until it has been copied.

1. **Back up the existing data.** In Firestore, keep the current `site/content` document unchanged until the new lodge page has been tested.
2. **Publish temporary migration rules.** In **Firestore Database → Rules**, replace the current rules with the contents of [`firestore.migration.rules`](./firestore.migration.rules), then click **Publish**. These temporary rules preserve the old app while enabling lodge-scoped documents. They are not the final isolation rules.
3. **Assign the existing lodge.** In **Authentication → Users**, copy the existing admin user's UID. In Firestore, open `admins/{UID}` and add:
   - `enabled`: boolean `true`
   - `lodgeId`: string `default`
   - `role`: string `owner`

   Use the exact lowercase `default` ID for the existing lodge so its regular app link continues to work.
4. **Deploy the updated Hosting app.** From the project folder, run:

   ```powershell
   npx.cmd --yes firebase-tools deploy --only hosting --project guests-app-welcome-2026
   ```

5. **Move existing lodge content.** Open `https://guests-app-welcome-2026.web.app/?lodge=default#admin`, sign in with the existing admin account, and click **Import existing shared content**. Review the fields, then click **Save changes for all guests**. This writes the old content to `lodges/default/content/main` and `publicLodges/default`.
6. **Add each additional owner.**
   - Enable **Email/Password** in **Authentication → Sign-in method** if it is not already enabled.
   - In **Authentication → Users**, choose **Add user** and create their account. Copy the UID.
   - Create `admins/{UID}` with `enabled` (boolean) `true`, `lodgeId` (string) set to a unique ID such as `seaview`, and `role` (string) `owner`.
   - Give that owner the link `https://guests-app-welcome-2026.web.app/?lodge=seaview#admin`. They sign in, enter their lodge details, and save. This creates their lodge's private and guest documents.
7. **Test isolation before tightening rules.** Sign in as each owner and verify the editor shows only their lodge's content. Save a harmless change in one lodge, then verify the other lodge's editor and guest link are unchanged. Guest links are `https://guests-app-welcome-2026.web.app/?lodge=<lodgeId>`.
8. **Publish final rules.** From the project folder, run:

   ```powershell
   npx.cmd --yes firebase-tools deploy --only firestore:rules --project guests-app-welcome-2026
   ```

   [`firestore.rules`](./firestore.rules) is the final rules file. It removes client access to the legacy `site` collection and only allows an enabled admin to read or write private content for their own `lodgeId`. Public lodge documents remain readable without sign-in so guests can use the app.
9. **Keep `site/content` as a backup** until both admin and guest links have been tested after the final rules are live. It can then be deleted manually from Firestore if no longer needed.

Never create admin assignment documents from the client app. The self-service signup function creates them on the server for verified users; existing assignments can also be managed in the Firebase Console. The rules intentionally deny client writes to `admins`.

## Guest-data privacy

The `publicLodges/{lodgeId}` document is readable by anyone with the app, because guests are not required to sign in. It contains guest-facing lodge data, which means another person could technically read a different lodge's published document if they know its ID. Admin users cannot read or edit another lodge's **private** `lodges/{lodgeId}/content/main` document. To make guest-facing data private between lodges too, guests must authenticate and the public-read rule must be replaced with an access check.

## Current app features

The Admin tab manages lodge details and logo, guest services, Wi-Fi, stay information, house rules, FAQs, Explore recommendations, emergency/reception details, and guest announcements. It also creates a lodge-specific guest QR code with a download action and a shareable guest link. The QR image is generated by QRServer; the data sent to that service is the publicly accessible guest link for the selected lodge. Each admin account is assigned to one lodge through `admins/{UID}.lodgeId`. Announcements and other edits publish only to that lodge. The existing lodge uses the `default` URL ID; use a unique lowercase ID for each additional lodge.

## Self-service subscription offer

The Traderite Consulting landing page is [`traderite.html`](./traderite.html). An existing signed-in lodge owner can continue to admin or explicitly sign out to use another account. Signing out also affects other tabs sharing the Firebase session. Lodge owners sign in or, when `signupEnabled` in [`billing-config.js`](./billing-config.js) is enabled, create an account on that page before continuing to the guest app's Admin page. New accounts request a verification email before continuing; the Admin page handles verification, resending and lodge setup. Existing guest links and direct Admin sign-in continue to work.

New owners verify their email and create their lodge to start a 14-day trial; no payment details are required. The Admin page displays the remaining days rounded up to the next whole day and the trial end date. The server records the trial end when the lodge is created; changing the trial length does not alter existing subscriptions.

Verification emails use the Firebase Authentication email template and include a continuation link to the Admin page. The landing page's domain must be authorized in Firebase Authentication. A successful send request does not guarantee inbox delivery; check the recipient address, spam or quarantine, and the project's email delivery configuration if a message does not arrive.

If Gmail receives verification emails but Outlook or Hotmail does not, the app has already submitted the same Firebase Auth request for both addresses; investigate delivery rather than changing the verification code. In each Firebase project (**`guests-app-welcome-2026`** and **`guests-app-staging-2026`**), open **Authentication → Templates**, edit the verification email, choose **customize domain**, and follow the console's instructions to add and verify the provided DNS records for a domain you control. Apply the custom domain after verification completes. If the domain already has an SPF TXT record, update that record as instructed instead of creating a second SPF record. DNS verification may take up to 24 hours. Then test delivery to Outlook/Hotmail again and check Microsoft quarantine or ask the recipient's mail administrator to inspect rejected messages. See [Firebase's custom authentication email domain guide](https://firebase.google.com/docs/auth/email-custom-domain).

The self-service founding offer is R199 per month per property for 12 months, then R299 per month, with R0 setup and a limit of 10 founding properties. The standard plan is R299 per month. When checkout is enabled, lodge owners can subscribe through PayFast after setup; checkout charges immediately, even during the trial. A subscription activates only after a verified PayFast notification.

Production signup is enabled in `billing-config.js`; online checkout remains disabled. Self-service signup requires the Functions and rules to be deployed and Firebase Email/Password authentication to be enabled. Before enabling checkout, configure the PayFast merchant secrets and required server settings, and test the complete payment flow in the sandbox. Keep merchant credentials and passphrase in Firebase Secret Manager, never in browser code or source control. Firebase Functions requires the Blaze plan.

Subscription visibility is driven by the server-managed `subscriptions/{lodgeId}` document. Trial creation and verified PayFast notifications update this document from trusted server code; Firestore rules deny client writes. Deploy the updated Firestore rules with:

```powershell
npx.cmd --yes firebase-tools deploy --only firestore:rules --project guests-app-welcome-2026
```
