# ShopDB - guide for an AI agent

ShopDB is a plugin-based asset-management system (PCs, printers, machines,
network devices, applications, knowledge base, guides, notifications, slides,
warranties, backups, endpoint telemetry) for manufacturing sites. Every site
runs its own instance.

This file is the machine entry point, served at `/api/docs/llms.txt`. It covers
the API, which is what most callers need, and routes everything else.

## Routing - read this before opening any document

`docs/INDEX.md` is the routing table. Rows are TASKS, each row gives the
smallest set of pages that answers it, and each row says how many lines that
costs. Find the row, read those pages, skip the rest. The corpus is far larger
than any one context window and almost none of it applies to a given question.

Every page opens with an HTML comment naming `purpose`
(tutorial/howto/reference/explanation), `audience`, `status` and a one-sentence
`summary`, plus `loadwith` for the pages it is not complete without. Filter on
that metadata before opening anything: `status: legacy` means true but
superseded (do not follow it for new work) and `status: historical` means a
finished migration or a proposal kept as a record, so neither is an answer to
"how do I do this today".

`docs/START-HERE.md` is the same routing for a human, and shorter.

Not an API question? Still start from the index rather than guessing at a
filename: its rows cover installing and running a site, loading its data, the
shop-floor PCs, and building on the platform. For "why is it built this way",
`docs/adr/README.md` says which decision governs the question and whether it
still holds. For writing a plugin, `docs/PLUGIN-HOOKS.md` is the contract and
everything else defers to it.

## Answering "how do I install or fix this server"

Docker is the topology this line ships, and the only one. `docs/DEPLOY.md` is a
new site at a connected facility, `docs/DEPLOY-AIRGAP.md` is the same stack
where nothing can be pulled, and `docs/UPGRADE.md` moves a running site to a
newer version. There is no Windows installer here.

Before diagnosing anything, ask for `docker compose ps` and
`curl -s http://127.0.0.1:5002/api/dashboard/health`. Comparing that bypass with
the site's real URL separates "the app is broken" from "nginx or the certificate
is broken". `docs/DEPLOY.md` has the rest of the health surface, and
`docs/CONFIG.md` is where every environment variable and Setting key is defined.

## Bulk-loading a site's data

Two routes, and the right one depends on what the site has:

- SPREADSHEET, no developer (the common case): `flask csv templates --out <dir>`
  generates templates FROM THE LIVE SCHEMA, then `flask csv import --dir <dir>`
  checks and `--commit` applies. Do NOT hand-write the templates - generate
  them. `docs/CSV-IMPORT.md`.
- A SOURCE DATABASE to script against: the HTTP import API,
  `docs/IMPORT-API.md`, with `docs/IMPORT-ADOPTION.md` for the loader around it.

User accounts are deliberately not CSV-importable.

## Base URL

Each site runs its own instance (ADR-004), so there is no single production URL:
it is whatever that site serves, e.g. `https://shopdb.example.net/shopdb`.
All API paths are under `/api` (e.g. `<base>/api/assets`). Dev: `http://localhost:5001`.

## Auth

Three schemes:

- **Bearer JWT** - most endpoints. Get one by logging in, or use a managed
  Personal Access Token (PAT). Send `Authorization: Bearer <token>`.
  - Login: `POST /api/auth/login` `{ "username": "...", "password": "..." }`
    -> `data.access_token`. Refresh: `POST /api/auth/refresh`.
  - PATs are minted in the UI (Settings > API Tokens); a *scoped* PAT is limited
    to named permissions and suspends the admin bypass.
- **X-API-Key** - unattended/service endpoints (collector ingest, and any
  plugin's own unattended routes). Send `X-API-Key: <managed-token>`. How a shop-floor PC gets its own
  credential is `docs/COLLECTOR-AUTH.md`.
- **Public** - some read endpoints (e.g. printer install-list, the slide feed,
  display dashboards) need no auth.

Auth level per endpoint is in the OpenAPI `security` field: `bearerAuth`,
`apiKeyAuth`, or none. Admin-only and permission-gated routes both use bearer.

## Response envelope

JSON endpoints return `{ "status": "success", "data": <payload>, "meta": {...} }`.
Errors nest under `data`, NOT at the top level:
`{ "status": "error", "data": { "error": { "code": "...", "message": "...", "details": {...} } } }`
with an HTTP 4xx/5xx. Reading `message` or `code` off the root gives `undefined`.
Lists carry `meta.pagination.{page, perpage, total, pages}` - again nested, not
`meta.total`. A few feed endpoints (screensaver, some installer text formats)
return raw text/JSON without the envelope - noted per route.

## Common recipes

- Search everything: `GET /api/search?q=<term>` (multi-word = AND across words).
- List assets on the map: `GET /api/assets/map`.
- List a type: `GET /api/printers`, `/api/computers`, `/api/machines`,
  `/api/network` (paginated: `?page=&perpage=`). A plugin that is disabled at
  that site serves none of its routes.
- Get one: `GET /api/printers/<id>` etc.
- Create (bearer): `POST /api/printers` `{assetnumber, windowsname, vendorid, ...}`.
- Reports: `GET /api/reports` (list), `GET /api/reports/pc-relationships` (PC<->machine).
- Printer installer data: `GET /api/printers/install-list` (public; add
  `?format=text` for a pipe-delimited variant); `GET /api/printers/pc-default?machine=<n>`.
- Collector ingest (X-API-Key): `POST /api/collector/computers`,
  `/api/collector/printers`, `/api/collector/endpoints`. How a PC gets its own
  credential is `docs/COLLECTOR-AUTH.md`.
- Import (admin PAT, preserves timestamps with `X-Import-Mode`): `docs/IMPORT-API.md`.

## Conventions

- DB-mirrored params/fields use lowercase concatenated names (no underscores):
  `locationid`, `vendorid`, `windowsname` - match them exactly.
- IDs in paths are integers.
- Plugin endpoints live under the plugin's prefix (`/api/<plugin>/...`).

## Full reference

- Machine spec: `GET /api/docs/openapi.json` (OpenAPI 3.1). Operation and
  plugin counts live in `docs/PROJECT-MAP.md`, which is generated.
- Interactive: `GET /api/docs` (Redoc).
- Human reference: the same spec at `GET /api/docs`, and `docs/INDEX.md` for everything else.
