# AGENTS.md — mobile/raito_whatsapp

Flutter employee client for the Raito WhatsApp CRM. Separate package with its own
toolchain — the root `AGENTS.md` (Laravel/PHP) does not apply here. The server is
the source of truth for authorization and role scoping; this app never enforces it.

## Stack
- Dart SDK `^3.7.2`, Flutter 3.29+; app version from `pubspec.yaml` (`version:` field)
- State/navigation: flutter_riverpod ^2.5.1, go_router ^14.2.7
- Networking/storage: dio ^5.7.0, flutter_secure_storage ^9.2.2
- Media: cached_network_image, photo_view, just_audio, record ^6.0.0, image_picker, file_picker, open_filex, flutter_pdfview, url_launcher
- Realtime: web_socket_channel ^3.0.1 (Reverb); Push: firebase_core ^3.6.0 + firebase_messaging ^15.1.3 + flutter_local_notifications
- Misc: intl ^0.19.0, package_info_plus; Lints: flutter_lints ^5.0.0; Icons: flutter_launcher_icons ^0.14.1
- Tests: flutter_test (widget + unit)

## Structure
- `lib/main.dart` — entry point: app bootstrap, router and theme wiring.
- `lib/config.dart` — all build-time configuration (`String.fromEnvironment`); add new tunables here, never hardcode hosts in widgets.
- `lib/providers.dart` — single Riverpod provider graph (token storage → api client → repositories → services); register new dependencies here.
- `lib/models/` — plain data classes with `fromJson`; edit when an API payload changes shape.
- `lib/services/` — `api_client.dart` (Dio + bearer interceptor), `repositories.dart` (endpoint calls), `token_storage.dart`, `realtime_service.dart`, `push_service.dart`, `media_auth.dart`. All network/IO lives here.
- `lib/screens/` — one file per screen (login, conversations, chat, search).
- `lib/widgets/` — reusable presentation widgets (message bubble, audio player, image/PDF viewers).
- `lib/theme.dart`, `lib/utils/` — theming and pure helpers (date/time formatting).
- `test/` — unit + widget tests. `android/`, `ios/` — platform projects (cleartext flags live here). `assets/` — launcher icon sources.
- `build/` — generated output; never edit or commit-review.

## Conventions
- Naming: snake_case filenames, PascalCase classes, `*Repository` for endpoint wrappers, `*Service` for long-lived stateful services, `*Provider` suffix for Riverpod providers.
- Imports: relative imports within `lib/` (`../config.dart`), `package:` imports for third-party and for `raito_whatsapp/...` inside `test/`.
- Layering: widget/screen → Riverpod provider → repository → `ApiClient`. Screens never construct Dio or read storage directly.
- Error handling: `ApiClient` sets `validateStatus: (s) => s < 500`, so 4xx are returned, not thrown — callers inspect `statusCode` themselves. A 401 triggers `onUnauthorized` → `forceLogout()`; do not add ad-hoc 401 handling in screens.
- Models tolerate both snake_case and camelCase keys because different endpoints return different casing — preserve this when editing `fromJson`.
- Non-obvious choices (timeouts, dead defaults, cleartext) are explained in doc comments above the code; keep that style.
- Tests: `flutter_test` with `group(...)`/`test(...)`; `fromJson` parsing is covered per-endpoint-casing. Files named `*_test.dart`.

## Commands
- Install deps: `flutter pub get`
- Run (dev): `flutter run --dart-define=API_BASE=https://raito-ai.com`
- Analyze/lint: `flutter analyze`
- Format: `dart format .`
- Tests: `flutter test`
- Build Android: `flutter build apk --release --dart-define=API_BASE=https://raito-ai.com`
- Build iOS: `flutter build ipa --dart-define=API_BASE=https://raito-ai.com` (needs macOS + Xcode + Apple Developer account)
- Launcher icons: `dart run flutter_launcher_icons`
- CI: غير محدد (no workflow covers this package)

## Rules
- Configuration is injected with `--dart-define`, never committed: `API_BASE`, `REVERB_KEY`, `REVERB_HOST`, `REVERB_PORT`, `REVERB_TLS`, `REALTIME`. Never hardcode a real key or secret in `config.dart`.
- `API_BASE` is the origin **without** `/api`; `AppConfig.apiUrl` appends it. Production is HTTPS with no port.
- Reverb is still plain `ws://` on its own port, so Android `usesCleartextTraffic` and iOS `NSAllowsArbitraryLoads` must stay enabled until Reverb is behind TLS — do not "harden" these without moving Reverb first.
- Auth tokens live only in `flutter_secure_storage` via `TokenStorage`; never in SharedPreferences, logs or plain files.
- Media URLs require the `Authorization` bearer header — go through `media_auth.dart`, do not build bare image URLs.
- Push is optional until `google-services.json` is added; the app must keep working without it (do not make Firebase init fatal).
- Bump `version:` in `pubspec.yaml` for every release build; `pubspec.lock` is committed — do not hand-edit it.

## References
- `README.md` (this folder) — run/build instructions, test credentials, full list of backend endpoints used, push setup.
- `../../AGENTS.md` — backend/API contract, conversation lifecycle and role scoping this client depends on.
- `../../Project_memory/mobile_app.md` — historical notes on the mobile client; background only.
- `../../HTTPS_REVERSE_PROXY_SETUP.md` — why the API is HTTPS while Reverb is not; read before touching TLS/cleartext.
