# Reminder Delivery Setup (WhatsApp + Google Calendar)

The `send-reminders` Edge Function is already deployed to your project. It stays
inactive per-channel until you add the credentials below as **Supabase secrets**.
Until then, in-app reminders work, and WhatsApp/Calendar report
"not configured" (the dashboard handles this gracefully).

You do all of this once. Nothing here touches your app code.

---

## A. WhatsApp (Meta WhatsApp Cloud API)

1. Go to https://developers.facebook.com → **My Apps** → **Create App** → choose
   **Business** → finish.
2. On the app dashboard, **Add product → WhatsApp → Set up**.
3. Open **WhatsApp → API Setup**. You'll see:
   - a **test sender** phone number,
   - a **Phone number ID**  → this is `WHATSAPP_PHONE_NUMBER_ID`,
   - a temporary **access token** (valid 24h) → this is `WHATSAPP_TOKEN`,
   - a box to add **recipient** numbers (add your own WhatsApp number to test).
4. **Important about free-text messages:** Meta only allows free-form text within
   24 hours of the recipient messaging your number. For a smooth demo, send a
   message *from* your phone *to* the test sender number first — that opens the
   24h window, after which the function's text messages go through.
5. For a token that doesn't expire in 24h: **Business Settings → Users → System
   users → Add**, assign your app, then **Generate new token** with the
   `whatsapp_business_messaging` permission. Use that as `WHATSAPP_TOKEN`.

You need: `WHATSAPP_TOKEN`, `WHATSAPP_PHONE_NUMBER_ID`.

---

## B. Google Calendar (service account)

1. Go to https://console.cloud.google.com → create a **new project**.
2. **APIs & Services → Library →** search **Google Calendar API → Enable**.
3. **APIs & Services → Credentials → Create credentials → Service account.**
   Name it (e.g. `volunteer-reminders`) → Create → Done.
4. Click the service account → **Keys → Add key → Create new key → JSON →
   Create**. A JSON file downloads. Inside it you'll find:
   - `client_email` → this is `GOOGLE_SA_EMAIL`,
   - `private_key`  → this is `GOOGLE_SA_PRIVATE_KEY` (the whole
     `-----BEGIN PRIVATE KEY----- ... -----END PRIVATE KEY-----` block).
5. Open https://calendar.google.com. Create a calendar (or use one), open its
   **Settings → Share with specific people → Add people**, paste the
   `client_email`, and give it **Make changes to events**.
6. In the same settings page, scroll to **Integrate calendar → Calendar ID**
   (looks like `xxxxx@group.calendar.google.com`, or your email for the primary
   calendar) → this is `GOOGLE_CALENDAR_ID`.

You need: `GOOGLE_SA_EMAIL`, `GOOGLE_SA_PRIVATE_KEY`, `GOOGLE_CALENDAR_ID`.

> Events are created on this shared calendar to demonstrate the integration.
> (Auto-inviting each volunteer as an attendee needs Google Workspace
> domain-wide delegation, which is out of scope for the FYP demo.)

---

## C. Add the secrets to Supabase

Easiest is the dashboard (handles the multi-line private key cleanly):

**Dashboard → Project Settings → Edge Functions → Secrets (Manage secrets)** →
add each of these as a name/value pair:

| Name | Value |
|------|-------|
| `WHATSAPP_TOKEN` | your Meta access token |
| `WHATSAPP_PHONE_NUMBER_ID` | your Meta phone number ID |
| `GOOGLE_SA_EMAIL` | the service account `client_email` |
| `GOOGLE_SA_PRIVATE_KEY` | the full PEM private key (paste with line breaks) |
| `GOOGLE_CALENDAR_ID` | your calendar ID |

Do **not** add `SUPABASE_URL`, `SUPABASE_ANON_KEY`, or
`SUPABASE_SERVICE_ROLE_KEY` — Supabase injects those automatically.

(CLI alternative, run in the project folder:
`supabase secrets set WHATSAPP_TOKEN=... WHATSAPP_PHONE_NUMBER_ID=... GOOGLE_SA_EMAIL=... GOOGLE_CALENDAR_ID=...`
and for the key use an env file: `supabase secrets set --env-file ./supabase/.secrets.env`.)

---

## D. Test it

1. Make sure the volunteer you're sending to has a **phone number** on their
   profile (for WhatsApp). You can set one for testing via SQL:
   ```sql
   update profiles set phone = '0123456789' where email = 'volunteer@example.com';
   ```
2. In the dashboard, open **Reminders**, pick a program, choose channel
   **WhatsApp** (or **Calendar**), and **Send reminder**.
3. The toast tells you how many were delivered, or the first error if a channel
   isn't set up yet.
4. To see detailed logs: Supabase **Dashboard → Edge Functions → send-reminders →
   Logs**.

---

## How it fits together

```
Reminders page  ──insert──>  notifications table  (channel = whatsapp/calendar/in_app)
       │
       └──invoke──>  send-reminders (Edge Function)
                         ├─ whatsapp → Meta WhatsApp Cloud API
                         ├─ calendar → Google Calendar API (service account)
                         └─ marks notifications.sent_at on success
```

The function requires a signed-in **admin** (it verifies the caller's role), and
uses the service role only server-side, so your keys never touch the browser.
