# Profiles, Notifications and AI chat

## Install the update

Back up the current application and database. Copy updated source files while preserving live environment settings, private uploads and any existing `config/openrouter.local.php`. Run `database/migrations/20261016_profiles.sql` against your SLPS database, then Ctrl+F5. Previous feature migrations remain required if they have not been applied. The full schema includes profile details for fresh installations.

## Profile pages

All roles now have a My Profile workspace. Users can update their full/preferred name, email address, phone, country, city and short bio. They can replace their profile photo (JPG/PNG/WebP, up to 5 MB and 4096 pixels per side) and change their password. Photos are served only to their owner through an authenticated endpoint.

Role, username, student/staff number, department, campus and academic status are displayed as university-managed information and cannot be changed through the self-service endpoint. Profile saves check a revision counter, so a stale form cannot silently overwrite newer details. Email changes require the current password and check for another account’s email/username. A student email change clears the old email-verification timestamp. Current login username/student number is unchanged. Password changes require the current password and matching 12–128 character replacement, rotate the session ID and invalidate other sessions through the existing password fingerprint.

## Notifications naming

The visible Notices label is now Notifications across the workspace heading, publishing form, navigation and unread badges. Existing API routes, database tables and read records retain their identities.

## Configure OpenRouter — required for live AI replies

The supplied source contains no API key. Chat does not produce live replies until you configure your own OpenRouter key. The browser never receives the key and calls only your PHP endpoint.

Option A — server environment variables:

```text
OPENROUTER_API_KEY=YOUR_OPENROUTER_KEY
OPENROUTER_MODEL=openrouter/free
OPENROUTER_SITE_URL=https://YOUR-UNIVERSITY-SLPS-ADDRESS
OPENROUTER_HOURLY_LIMIT=300
```

Set these in your PHP/web-server or hosting environment, not in browser JavaScript. `OPENROUTER_MODEL` may be any text-chat model available to your OpenRouter account. The default free-model router depends on provider availability; paid models require appropriate account credit. Site URL is optional app attribution. The hourly limit is a local shared limit and does not configure an OpenRouter billing limit.

Option B — PHP configuration for hosting that does not expose environment settings:

1. Copy `config/openrouter.example.php` to `config/openrouter.local.php` on your server.
2. Open that local file in your server editor and enter the key in `api_key`, the model in `model` and your optional site address in `site_url`.
3. Keep the file private. It has a direct-request guard, and the included config/.htaccess denies direct access to the local configuration on Apache. Never put the key in screenshots, messages, browser code or source control.

Environment variables override local configuration. PHP cURL must be enabled, HTTPS certificate verification must work and outbound access to `https://openrouter.ai/api/v1/chat/completions` must be allowed. Restart/reload PHP if your environment requires it after settings change. Open Assistant and send a short test question to verify the real provider connection.

Official references: https://openrouter.ai/docs/api/api-reference/chat/send-chat-completion-request and https://openrouter.ai/docs/app-attribution.

## Assistant and login popup

The signed-in Assistant is a dedicated chat workspace with conversation history for the current session, suggested starter questions, an input area, typing status, New conversation and safe code blocks with Copy code. User-facing AI disclosure explains that messages go to OpenRouter and responses may be inaccurate. It cannot access university records or perform account actions.

The login page keeps its existing layout. Its bottom-right Chat with us button opens a compact white panel with the blue Message Us header, grey reply bubbles, clear and minimise controls, starter chips and bottom composer, following the supplied popup reference. It is explicitly AI support, not a human operator. Minimise/reopen keeps the conversation. Public support and signed-in Assistant use separate server-session histories. Only the most recent 16 messages are retained as context, with 4,000-byte incoming-message and 1,200-token answer limits. Replies are non-streamed and the interface displays a typing state while waiting.

Public chat is limited to 20 requests per source IP per hour; signed-in chat to 60 per user per hour; both share the configurable global hourly limit. CSRF validation, a fixed server-side system prompt, per-session in-flight protection, provider timeouts and bounded response size are included. Keys and provider response bodies are not returned to users. No user profile or other university record is automatically sent to the provider.

## Validation and remaining deployment checks

Passed: PHP syntax checks, profile form updates and email confirmation UI, safe chat/code rendering, popup minimise/reopen, request payload/header/response tests using a stub transport, real local HTTP tests for guest sessions, CSRF rejection, private authentication and missing-key behavior, plus previous module UI regressions.

A live OpenRouter completion has NOT been tested because no API key was provided. Live MySQL profile updates/photo uploads, hosting-specific PHP cURL/connectivity and visual browser validation still need checking on your server.

Tests: `node tests/profile_ui.cjs`, `node tests/assistant_ui.cjs`, `php -n tests/openrouter_request.php` (the last uses an intentionally stubbed transport and is not a live-network test).
