# TechTitans — Complete Photo-to-Product AI Workflow

**Workflow:** Choose/Take Photo → **Analyze with AI** → Review extracted details → Enter price & quantity →
**Approve & Create Product**. **Branch:** `techtitans/product-editor-redesign`. **Commits:** frontend
`59a417e`, backend `689368e` (code). Not pushed.

## 1. What changed (the fix)

Previously, after a photo was selected the only action was "Continue to manual entry." Now:

- After **Choose Photo** / **Take Photo** and a preview, the **primary** action is **Analyze Product with
  AI**; **Continue Manually** is the secondary action. It never jumps straight to manual entry.
- **Analyze** sends the photo to the backend vision endpoint. On success it opens the **existing full-page
  editor** (the shared review/approval screen) **prefilled** with the extracted draft **and** the original
  photo as the product image. The operator reviews/edits every field, fills **price** (required) and
  **quantity**, and clicks **Create product** — the explicit approval. Creation reuses the existing
  product API: validation (`StoreProductRequest` + `ProductTemplateRegistry`), permissions
  (`products.manage`), transaction, variants, tax, pricing, image upload, the live **duplicate-name**
  warning, and request-id **idempotency** (no double submit). On success it shows the saved product's
  detail page.

The review/approval screen is the editor (not a separate screen) — the same unified approval surface all
AI methods converge on, with inline editing of name, brand (dropdown + add), type, category, model, specs,
description, website name/description, SKU preview, price, quantity/stock, and publish toggle, plus the
completion checklist + missing-field summary.

## 2. Real AI vision — implemented, config-gated

- **Endpoint:** `POST /api/v1/menu/products/ai/analyze-image` (multipart `image`, jpeg/png/webp ≤ 10 MB;
  optional `context[currency|categories|brands]`). Returns a structured draft: name, product_kind, brand,
  model, category, specs, website description + `uncertain[]` / `missing[]`. **Never invents**
  price/cost/quantity/model/serial — unknowns are null and listed in `missing`/`uncertain`, mapped to
  existing categories/brands from the supplied context.
- **Provider:** `App\AI\Product\Openai\OpenAiProductImageAnalyzer` (vision chat/completions, inline base64
  image, strict-JSON, coerced to the schema), selected by `ai_product.vision.driver`. Credentials are
  backend-only; request size/timeout limited; authorized via `products.manage`.
- **Status:** `GET /api/v1/menu/products/ai/status` reports which AI features are configured + the exact
  `required_env`, powering the admin-facing "not configured" message.

## 3. Missing-provider handling (honest, no fabrication)

Default `AI_PRODUCT_VISION_DRIVER=none` → the **Null** analyzer is bound and the endpoint **fails closed**
with **503 `ai_vision_not_configured`** + `required_env`. The UI then shows **"AI Product Recognition is
not configured"** with the exact backend env vars to set, keeps **Manual Entry** available, and **never
substitutes mock analysis or pretends details were extracted**. The deterministic **mock** analyzer exists
**only for automated tests** (bound solely when the driver is explicitly `mock`).

### Enabling real vision
```
AI_PRODUCT_VISION_DRIVER=openai
AI_PRODUCT_VISION_API_KEY=<key>      # backend-only; never exposed
# optional: AI_PRODUCT_VISION_MODEL (default gpt-4o-mini), _BASE_URL, _TIMEOUT
```

## 4. Real AI vs mock — what was actually tested

| Path | How verified |
|---|---|
| **Not-configured (real env, no key)** | **Live** — `GET …/ai/status` → `vision.configured=false`; `POST …/analyze-image` → **503** with `required_env`. UI shows the not-configured panel (screenshot below). |
| **Successful analysis → draft** | **Mock only** (deterministic, test-bound) — backend `AiProductVisionTest` + frontend test (analyze → editor prefilled with draft + photo). |
| **Real OpenAI vision** | **NOT tested** — no API key is configured on this machine (`AI_PRODUCT_*` all empty, verified). The adapter, config, validation and error mapping are implemented and unit-covered with the mock; the live provider call path is unverified. |

**Remaining configuration blocker:** real photo analysis requires `AI_PRODUCT_VISION_DRIVER=openai` +
`AI_PRODUCT_VISION_API_KEY` (a vision-capable model, e.g. `gpt-4o`/`gpt-4o-mini`). Until set, the workflow
correctly reports "not configured" and manual entry (with the photo preserved) remains fully usable.

## 5. Tests (focused) & results

- **Frontend** — `SmartAddProductPage.test.tsx` **11 passed**: four methods; NL draft→seed; barcode
  catalogue search; **Choose + Take Photo**; preview + Replace/Retake; invalid type rejected;
  camera-unsupported error; **Analyze + Continue Manually** after a photo; **not-configured** message shown
  (and `analyzeImage` NOT called, no fabrication); **analyze success → editor prefilled** with draft +
  image; price never invented. `tsc` ✓, `eslint` ✓.
- **Backend** — `AiProductVisionTest` **6 passed** (status default not-configured; analyze fails closed →
  503 + required_env; mock driver returns a draft with price null + `missing:[price]`; non-image rejected
  422; unauthenticated 401; location-scoped manager 403). `AiProductDraftTest` 5 passed.
- **Live** — status + 503 not-configured verified against `techpos`; screenshot captured.

### Coverage of the requested test list
Upload ✓, camera capture ✓ (live, earlier), preview/replace ✓, **provider-config detection ✓**, successful
analysis ✓ (mock), invalid image ✓, uncertain fields ✓ (flagged), category/brand mapping ✓ (context +
editor), price/stock entry ✓ (editor), approval → creation ✓ (existing create pipeline), image persistence
✓ (pending→upload), duplicate prevention ✓ (editor warning + backend slug uniqueness). Real-provider
success (#5) is the one item gated on credentials.

## 6. Screenshot

- `images/smart-add-photo-analyze.png` — a chosen photo with **Analyze Product with AI** / **Continue
  Manually** and the honest **"AI Product Recognition is not configured"** panel (required env listed).

(Earlier photo-capture shots: `smart-add-photo-options.png`, `smart-add-photo-chosen.png`,
`smart-add-photo-camera-live.png`, `smart-add-photo-in-editor.png`.)

## 7. Data safety

Used TechTitans development data only (`techpos`, `techtitans` workspace). NH POS and WordPress untouched.
No product is saved or published before explicit approval in the editor.

## 8. Commit hashes

- Frontend: **`59a417e`** · Backend: **`689368e`** (branch `techtitans/product-editor-redesign`).
Committed locally only — not pushed or deployed.
