# WebNexa

Our own multi-site CMS, like WordPress: log in, pick a theme, build pages from blocks, edit
content, upload media and publish, for many websites from one place.

- **One app, many sites.** Each request is matched to a site by its domain.
- **100% our own.** No outside services, no API keys, no client data leaving our servers.
  Open-source libraries are fine when they're installed in our code and don't call out.

## Stack

Next.js 16 (App Router) · React 19 · TypeScript · PostgreSQL 17 · Drizzle ORM ·
Tailwind CSS 4 · shadcn (copied into our code) · lucide icons · Vitest

## Run it locally (Windows, macOS or Linux, no Docker)

**You need:** Node.js 22.13 or newer, and PostgreSQL 16 or newer running on your machine.

1. **Create the database** (once). In pgAdmin or `psql`, as the `postgres` user:

   ```sql
   CREATE ROLE webnexa LOGIN PASSWORD 'choose-a-strong-password';
   CREATE DATABASE webnexa_dev  OWNER webnexa ENCODING 'UTF8';
   CREATE DATABASE webnexa_test OWNER webnexa ENCODING 'UTF8';
   ```

2. **Set up environment variables.** Copy `.env.example` to `.env.local` and put your
   password in `DATABASE_URL`. `.env.local` is never committed.

3. **Install, build the database and start:**

   ```bash
   npm install
   npm run db:migrate   # create the tables
   npm run db:seed      # super admin + demo site on localhost:3000
   npm run dev
   ```

   `db:seed` prints the super admin's email and a generated password **once**. Save it.
   To choose them yourself, set `SEED_ADMIN_EMAIL` / `SEED_ADMIN_PASSWORD` in `.env.local` first.

4. **Check it works:**
   - Admin: http://localhost:3000/admin (sends you to the sign-in page; use the seeded admin)
   - Public site: http://localhost:3000
   - Health check: http://localhost:3000/api/health should return `"database": "up"`

5. **Database tests (optional):** copy `.env.local` to `.env.test.local`, change the database
   name in `DATABASE_URL` to `webnexa_test`, then run `npm run test:db`. These tests wipe the
   test database, so it must end in `_test`.

Turn off Next.js telemetry once per machine: `npx next telemetry disable`.

## Sign-in and roles

| Role          | Can do                                              |
| ------------- | --------------------------------------------------- |
| Super admin   | Everything, including managing users                |
| Agency staff  | Every site: create sites, edit settings and content |
| Client editor | Only the sites they are a member of                 |

- 5 wrong passwords in a row lock the account for 15 minutes.
- A normal sign-in ends when the browser closes, after 8 hours without activity, or 24 hours
  after signing in. "Keep me signed in" lasts 30 days.
- In production, set `TRUST_PROXY=true` (behind Caddy/Nginx) so per-IP limits and IP addresses
  in the activity log work.

## Scripts

| Command               | What it does                                           |
| --------------------- | ------------------------------------------------------ |
| `npm run dev`         | Start the development server                           |
| `npm run build`       | Production build                                       |
| `npm start`           | Run the production build                               |
| `npm run lint`        | ESLint                                                 |
| `npm run format`      | Format all files with Prettier                         |
| `npm run typecheck`   | TypeScript check (generates route types first)         |
| `npm test`            | Unit tests (Vitest)                                    |
| `npm run check`       | Everything above except build. Run it before every PR. |
| `npm run test:db`     | Database rule tests on `webnexa_test` (wipes it)       |
| `npm run db:generate` | Create a migration after changing `src/db/schema`      |
| `npm run db:migrate`  | Apply pending migrations                               |
| `npm run db:seed`     | Add the super admin and demo site (safe to re-run)     |
| `npm run db:reset`    | Dev only: empty the database (asks you to confirm)     |

**Changing the database:** edit `src/db/schema/*.ts`, run `npm run db:generate`, read the new
SQL file in `drizzle/`, then `npm run db:migrate`. Commit the schema change and the migration
together. Never edit a migration that has already been applied on a server. Use pgAdmin to look
at data (not `drizzle-kit studio`, which opens an outside website).

## Folder layout

```
src/
  app/
    (admin)/admin/   admin panel (logins, sites, pages, media…)
    (site)/          public sites, rendered from blocks
    api/health/      health check
  core/              WebNexa Core: env, auth, sites, block format
  blocks/            shared block library
  components/ui/     shadcn components (our copy)
  db/                database client, schema (tables) and health check
  styles/            shared CSS
drizzle/             migration files (SQL), generated from src/db/schema
scripts/db/          migrate, seed and reset scripts
```

## Working rules

- `main` always works. Work on a branch (`feat/…`, `fix/…`), open a pull request, merge when
  `npm run check` and `npm run build` pass and the day's "done when" is tested.
- Small commits with clear messages: `feat: add sessions table`, `fix: …`, `chore: …`.
- Never commit `.env.local`, passwords, database dumps or uploaded media.
- New libraries must be open source, installed in our code, and not call anyone's servers.

Product plan: `cms-plan.md`, `week-1-plan.md` and the template docs (kept outside this repo for now).
