# e360 — Supabase Database Setup Guide

This guide walks you through connecting your e360 system to a free Supabase
database so that all staff share the same live data across any device.

---

## What You Need

- A free account at https://supabase.com (no credit card required)
- About 10 minutes

---

## Step 1 — Create a Supabase Account & Project

1. Go to **https://supabase.com** and click **Start your project**
2. Sign up with GitHub or email
3. Click **New project**
4. Fill in:
   - **Name:** `e360-prod` (or anything you like)
   - **Database Password:** choose a strong password and save it somewhere safe
   - **Region:** choose the closest to Uganda → `ap-southeast-1 (Singapore)` is usually fastest
5. Click **Create new project** — it takes about 2 minutes to provision

---

## Step 2 — Create the Database Table

Once your project is ready:

1. In the left sidebar, click **SQL Editor**
2. Click **New query**
3. Paste the following SQL and click **Run** (▶):

```sql
-- e360 key-value store
-- One row per data category (records, tickets, logs, etc.)
CREATE TABLE IF NOT EXISTS e360_kv (
  key        TEXT        PRIMARY KEY,
  value      TEXT        NOT NULL DEFAULT '[]',
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Enable Row Level Security
ALTER TABLE e360_kv ENABLE ROW LEVEL SECURITY;

-- Allow the app (using the anon key) to read and write all rows
CREATE POLICY "e360 app access" ON e360_kv
  FOR ALL
  TO anon, authenticated
  USING (true)
  WITH CHECK (true);
```

4. You should see **Success. No rows returned.**

---

## Step 3 — Get Your API Keys

1. In the left sidebar, click **Settings** (gear icon at the bottom)
2. Click **API**
3. You need two values — copy them:

| Value | Where to find it |
|---|---|
| **Project URL** | Under "Project URL" — looks like `https://abcdefgh.supabase.co` |
| **anon public key** | Under "Project API keys" → `anon public` row |

---

## Step 4 — Add Your Keys to the App

Open **`supabase.js`** in a text editor and replace the two placeholder values near the top:

```javascript
// BEFORE (placeholders)
const SUPABASE_URL      = 'https://YOUR_PROJECT_ID.supabase.co';
const SUPABASE_ANON_KEY = 'YOUR_ANON_PUBLIC_KEY_HERE';

// AFTER (your real values — example format)
const SUPABASE_URL      = 'https://abcdefghijklmnop.supabase.co';
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
```

Save the file.

---

## Step 5 — Test It

1. Open `index.html` in your browser
2. You should see the **"Connecting to Database"** loading screen for a moment
3. It disappears and the PIN screen appears — you're connected ✓

If you see an orange warning banner, check:
- Your internet connection
- That the URL and key in `supabase.js` are correct (no extra spaces)
- That the SQL from Step 2 ran successfully

---

## Step 6 — Migrate Existing Data (Optional)

If you already have data stored in the old localStorage version, you can migrate it:

1. Open the old version of the app in the browser that has the data
2. Click **⬇ Backup CSV** — save the CSV files
3. Open the new Supabase version
4. Click **📥 Import** — import the CSV files
5. Your data is now in Supabase and shared with all users ✓

---

## How It Works After Setup

| What happens | How it works |
|---|---|
| User opens the app | App loads all data from Supabase into memory |
| User submits a ticket | Ticket saved to memory instantly + pushed to Supabase |
| Another user opens the app | They load the same data — sees the ticket |
| Background sync | Every 30 seconds, app checks Supabase for changes |
| Internet goes down | App keeps working from memory; data syncs when reconnected |
| Browser cache cleared | No data lost — everything is in Supabase |

---

## Data Stored in Supabase

| Key | Contents |
|---|---|
| `e360_records` | All device assignments, returns, and registrations |
| `e360_logs` | Activity audit log |
| `e360_maint` | Maintenance records |
| `e360_devreg` | Device register (God Mode) |
| `e360_tickets` | IT support tickets |
| `e360_mrt_logs` | Monthly report rows |

**Still stored locally (per-device, intentionally):**
- PIN code (`e360_user_pin`) — security credential stays on device
- Country/entity setting (`e360_country`) — per-device UI preference
- GDPR consent (`e360_gdpr_accepted`) — per-device consent record

---

## Free Tier Limits (Supabase)

The free tier is more than enough for e360:

| Limit | Free tier | Typical e360 usage |
|---|---|---|
| Database size | 500 MB | ~5 MB for years of records |
| API requests | 500,000 / month | ~1,000 / month typical |
| Bandwidth | 5 GB / month | ~50 MB / month |
| Projects | 2 | 1 needed |

---

## Security Notes

- The `anon public` key is safe to include in client-side code — it only has the
  permissions defined by your Row Level Security policies
- Never share your `service_role` key (it bypasses RLS)
- The e360 PIN system remains the user-facing access control layer
- For production, consider tightening the RLS policy to restrict by IP or add
  Supabase Auth for proper user authentication

---

## Need Help?

- Supabase docs: https://supabase.com/docs
- Supabase status: https://status.supabase.com
- e360 IT Administrator: contact your internal IT team
