# AdMedia Internal CRM (frontend)

Next.js App Router app for the AdMedia internal CRM. Talks to
[`api.internal-crm`](../api.internal-crm) over `/api/v1`.

## Stack

- Next.js 15 (App Router, Turbopack)
- React 19 + TypeScript (strict, `noUncheckedIndexedAccess`)
- Tailwind CSS 4
- Axios + Zod
- ESLint + Prettier
- PM2 + `deploy/deploy.sh` (same style as the API's `deploy/deploy.sh`)

## Folder layout

```
src/
  app/                 # routes and layouts only
  components/          # shared UI (no feature API calls)
  config/              # env, constants
  features/            # one folder per domain
    <domain>/
      types.ts         # shared DTOs for that domain
      api.client.ts    # browser helpers
      api.server.ts    # Server Component / RSC helpers
  lib/
    api/
      client.ts        # browser Axios (rewritten /api/v1)
      server.ts        # server Axios (direct API_URL, server-only)
      errors.ts
  types/
```

## Client vs server API helpers

| Need                                           | Import                                                 |
| ---------------------------------------------- | ------------------------------------------------------ |
| Client Component, hook, click handler          | `@/features/<domain>/api.client` or `@/lib/api/client` |
| Server Component, Route Handler, Server Action | `@/features/<domain>/api.server` or `@/lib/api/server` |

```ts
// Client Component
'use client';
import { listContacts } from '@/features/contacts/api.client';

// Server Component
import { listContacts } from '@/features/contacts/api.server';
```

- **Client** calls same-origin `/api/v1` (Next rewrite) and sends cookies + local `x-dev-*` headers.
- **Server** calls `API_URL` directly, forwards `crm_session` from `next/headers`, and is guarded by `server-only` so it cannot ship to the browser.

## Local setup

1. Copy env and start the API (port 3340) from `api.internal-crm`.
2. In this folder:

```bash
cp .env.example .env.local
npm install
npm run dev
```

Open [http://127.0.0.1:3000](http://127.0.0.1:3000).

With `NEXT_PUBLIC_DEV_AUTH_ENABLED=true`, both clients send `x-dev-user` /
`x-dev-business` (same as Swagger / Postman local auth).

## Scripts

| Command             | What it does                              |
| ------------------- | ----------------------------------------- |
| `npm run dev`       | Next.js on port 3000                      |
| `npm run check`     | typecheck + lint + format                 |
| `npm run build`     | production build                          |
| `npm run start`     | next start (local)                        |
| `npm run deploy`    | Shell deploy: pull, ci, build, PM2        |
| `npm run pm2:start` | Start PM2 (`deploy/ecosystem.config.cjs`) |

## Deploy (shell script + PM2)

```bash
cp .env.example .env
# edit .env — PORT, PM2_APP_NAME, NODE_ENV, NEXT_PUBLIC_*, API_URL for this environment
npm run deploy
```

Runs `deploy/deploy.sh` (pull → `npm ci` → build → `pm2 startOrReload`). Details: [deploy/README.md](deploy/README.md).

`NEXT_PUBLIC_*` are baked at **build** time. Never put secrets in `NEXT_PUBLIC_*`.

## How to add a page

Follow the same pattern as Companies / Contacts so lists and auth stay consistent.

1. **Route** — add `src/app/(app)/<path>/page.tsx`. Keep it thin: metadata + a feature view.
2. **Feature module** — under `src/features/<domain>/`:
   - `types.ts` — DTOs matching the API (check Swagger at `{API_URL}/docs`)
   - `api.client.ts` / `api.server.ts` — thin wrappers around `@/lib/api/*`
   - `ui/<Name>View.tsx` — Client Component for interactive UI
3. **Auth** — `(app)` layout already gates on `getMe()` and unfinished onboarding. Do not add per-page login checks; 401s redirect via the browser API client.
4. **Lists** — use cursor paging (`nextCursor`), put filters in the URL, and cancel in-flight requests when filters change (`useCursorList` in `src/lib/hooks/useCursorList.ts`). Do not invent a `total` count unless the API returns one.
5. **Writes** — send `updatedAt` on PATCH and show “Someone else changed this, reload” on 409.
6. **Picklists** — load dropdown options from `GET /picklists`, not free text.
7. **Nav / CSS** — wire the link in `AppShell` and add any page CSS under `public/crm/assets/css` + the `(app)` layout `<link>` list.
8. **Check** — `npm run check` before opening a PR.
9. **API types** — with the API running, regenerate OpenAPI types: `npm run types:api` (writes `src/types/api.generated.ts`). Prefer generated paths over hand-copied DTOs when adding new endpoints.

Sign-in is **Google only**. New businesses come from the API (`npm run business:create`); people join by invitation. First-login role onboarding uses `GET /me` → `onboarding` and `POST /me/onboarding`.

## Next

Convert remaining PHP UI assets into App Router pages under `src/app` and
feature modules under `src/features`.
