# Fielddesk Management Hub

A static, mobile-friendly management dashboard for four workspaces: Work, Personal, Scouts and Business. It uses Supabase Auth + Postgres for shared data and row-level security, and can be hosted on Cloudflare Pages free tier.

## What's in this first version

- Overview dashboard and workspace switcher
- Tasks, projects, contacts, documents, events, recurring checklists and reusable templates
- Create, edit, delete and search records; mark tasks complete and run checklists
- Supabase email/password sign-in and account creation
- Owner, admin, editor and viewer workspace roles enforced in Postgres RLS
- Static frontend: no server secrets, build step or paid runtime required

## Set up Supabase

1. Create a Supabase project on the free plan.
2. In **SQL Editor**, run [`supabase/schema.sql`](supabase/schema.sql) once.
3. In **Authentication → URL Configuration**, add your eventual Cloudflare Pages URL to the allowed redirect URLs. If email confirmation is enabled, verify the signup email before signing in.
4. Open the dashboard, paste the Supabase **Project URL** and **publishable anon key** (legacy `anon` key also works). Never use the `service_role` key in this browser app.
5. Create an account. On first sign-in the app creates the four default workspaces and makes your account owner of each.

## Invite another member

For a small private team, first create the person's auth user in Supabase **Authentication → Users** (or let them sign up). Then run this in SQL Editor, replacing IDs with actual values from your project. Workspace IDs appear in `public.workspaces`; user IDs are in Authentication → Users.

```sql
insert into public.workspace_members (workspace_id, user_id, role)
values ('WORKSPACE_UUID', 'USER_UUID', 'editor');
```

Roles: `owner` and `admin` can manage membership and workspace settings; `editor` can create and change workspace records; `viewer` can read. Do not expose a service role key to the website.

## Deploy to Cloudflare Pages (free)

1. Put this folder in a GitHub repository.
2. In Cloudflare, choose **Workers & Pages → Create → Pages → Connect to Git** and select that repository.
3. Set the framework preset to **None**, leave the build command empty, and set the build output directory to `/` (the repository root). Alternatively, upload the root folder directly as a static asset.
4. Deploy, then add the resulting `*.pages.dev` origin in Supabase Authentication → URL Configuration.
5. For your own stable address, add a custom domain in Cloudflare Pages.

The included `wrangler.toml` also declares the project name and compatibility date for a future Cloudflare Pages workflow; this first version needs no Worker or build command.

## Data model

`profiles` belong to Supabase Auth users. `workspaces` are the four top-level spaces, with `workspace_members` connecting users to spaces and their role. Tasks, projects, contacts, documents, events, checklists and templates each belong to one workspace. Tasks, events and documents may reference a project. Checklists contain ordered checklist items and execution history (`checklist_runs` + `checklist_run_items`). The schema enables RLS for every table and provides helper functions for membership checks.

## Navigation structure

Workspace selector → Overview, Tasks, Projects, Contacts, Documents, Events, Checklists, Templates. The workspace selector changes the data scope; the records remain in one shared Supabase database and RLS filters every query by the signed-in user's membership.

## Free-tier notes

Supabase and Cloudflare plan limits change over time. This app has no paid dependency or backend server requirement, but you should check the current provider limits for your expected storage, traffic and backup needs. Document records currently store a URL and metadata; file upload storage can be added later using a private Supabase Storage bucket with matching policies.
