# NGO Toolkit Lab

AI-powered grant writing tools for NGOs. 4 tools: Logframe Generator, Funder Rewriter, SMART Indicators, Theory of Change.

## Setup

```bash
# Install dependencies
bun install

# Copy env and fill in values
cp .env.example .env

# Run database migration
bun run db:migrate

# Start dev server
bun dev
```

## Environment Variables

See `.env.example` for all required variables:
- `DATABASE_URL` — PostgreSQL connection string
- `OPENAI_API_KEY` — OpenAI API key
- `AUTH0_*` — Auth0 configuration for admin routes
- `LEMONSQUEEZY_*` — Payment processing

## Database

Schema is in `lib/db/schema.sql`. Run manually or use the migration script:
```bash
psql $DATABASE_URL < lib/db/schema.sql
# or
bun run db:migrate
```

## Docker

```bash
docker compose up -d
```

## Routes

| Route | Description |
|-------|-------------|
| `/` | Hub — 4 tool cards |
| `/logframe` | Logframe Generator |
| `/funder-rewriter` | Funder Rewriter |
| `/smart-indicators` | SMART Indicators |
| `/theory-of-change` | Theory of Change |
| `/admin` | Dashboard (Auth0 protected) |
| `/admin/config` | Configuration editor |
| `/admin/leads` | Leads table |

## API Routes

- `POST /api/[tool]/generate-draft` — Generate initial draft
- `POST /api/[tool]/improve-section` — Improve a section (paid)
- `POST /api/[tool]/validate` — Quality assessment (paid)
- `POST /api/[tool]/finalize` — Produce final version (paid)
- `POST /api/payment/create-checkout` — Create LemonSqueezy checkout
- `POST /api/payment/webhook` — LemonSqueezy webhook handler
- `GET/POST /api/admin/config` — Admin config management
- `GET /api/admin/leads` — Paginated leads
- `GET /api/admin/metrics` — Dashboard metrics
- `POST /api/export/[format]` — Export (docx/excel/pdf/copy)
