# WP3 — FE unify (drop the smuggled prompt) + send page-context

## Goal
Clean up the frontend so the assistant's knowledge comes from the backend digest (WP1/WP2), not a hardcoded FE constant, AND tell the backend which screen the user is on so answers are contextual. Backend accepts + injects the page-context.

## Context
- FE `/home/moonui/public_html/moon-erp` (Angular 21 standalone, branch `hazemdev`, `ng build` must stay green). BE `/home/moonui/moon-erp-be` branch `hazemdev`.
- Today (verified):
  - `src/app/core/services/ai-assistant.service.ts` has a hardcoded Arabic `SYSTEM_PROMPT` constant (~lines 27-195) that it **smuggles to the BE as fake user/assistant history turns** (see `callBackend` ~:335-347, comment "backend ignores the system_prompt field, so we inject via history"). Request body (~:349-352) = `{ message, history: [...] }`.
  - After WP2 the BE builds the real system prompt itself → this FE smuggling is now redundant and would DOUBLE the prompt. Remove it.
  - The component `src/app/shared/components/ai-assistant/ai-assistant.component.ts` calls `aiService.sendMessage(text)` with only the text. It's mounted globally in `layout/main-layout/main-layout.component.html:29` (present on every screen).
  - Current-screen signals available: `Router.url` / `NavigationEnd` (canonical), `nav-items.config` (route→label/permission), `command-bar.service` (route→bilingual label). No global Title service.
- BE endpoint `POST /api/ai/chat` via `Modules/Core/app/Http/Controllers/AiChatController.php` + `AiChatRequest.php` (validates `message`, `history[]`). WP2 left a `?string $pageContext` param ready in `AiChatService`.

## Interfaces consumed (from WP2)
- `AiChatService::getSystemPrompt(User, locale, ?pageContext)` / `buildMessages(..., ?pageContext)` accept an optional page-context string; the digest+versioned cache already handle it.

## Deliverables
### Frontend
1. **Remove the smuggled prompt:** delete the `SYSTEM_PROMPT` constant + the fake-history injection in `ai-assistant.service.ts`. The request body becomes `{ message, history, page }` where `history` is only the REAL conversation turns (keep the rolling window).
2. **Send page-context:** inject `Router` into `ai-assistant.service.ts` (or pass from the component). Resolve the current screen to a compact hint: the route path + a human label if resolvable from `nav-items.config`/`command-bar.service` (e.g. `{ route: '/lab/requests', label_en: 'Lab Requests', label_ar: 'طلبات المعمل' }` or just the route string if no label). Put it in the payload as `page`.
3. Keep the client-side localStorage cache behavior sane: since answers now depend on `page`, either include `page` in the FE cache key or only cache page-independent questions (it already only caches no-history questions — extend the key with `page` to be safe).

### Backend
4. `AiChatRequest.php`: accept optional `page` (nullable; sub-fields `page.route` string ≤ 200, `page.label_en`/`label_ar` string ≤ 200 — all optional/validated, never trusted raw).
5. `AiChatController` → pass the page hint into `AiChatService::chat(...)`, which forwards it as the `?string $pageContext` (format a short line like `The user is currently on the "<label>" screen (<route>).` bilingual-aware). It already flows into the versioned cache key (WP2).

## Acceptance criteria
- [ ] FE no longer sends the hardcoded system prompt (grep: `SYSTEM_PROMPT` gone from `ai-assistant.service.ts`; no fake-history injection). The assistant still works end-to-end (relies on BE digest).
- [ ] The `/ai/chat` request payload includes a `page` object with the current route (verify by reading the built request / a unit check).
- [ ] BE injects a page-context line into the system prompt when `page` is present; absent `page` still works (optional).
- [ ] `ng build` green. Existing BE AI tests green (extend `AiChatApiTest` for the `page` field validation + injection).

## Tests
- BE: extend `Modules/Core/tests/Feature/AiChatApiTest.php` — `page` accepted+validated, injected into prompt, absent-page still works.
- FE: `ng build` green (no unit runner configured on FE).

## Flags
- No [FIN]. No migration. `page` is validated user input — never interpolate raw into the prompt without the length caps + treat as untrusted text (it's a hint, not a command).

## Out of scope
- Owner knowledge field + refresh button (WP4).
