# Halo Bestie — Project Root

Mental health chat platform connecting clients (users seeking support) with trained mental health professionals (mitra). Paid service with optional trial period.

## Folder Structure

| Folder | Purpose |
|---|---|
| `requirement/` | Requirement documents (phased) |
| `backend/` | Fastify.js API server |
| `client_app/` | Flutter app for clients (iOS + Android) |
| `mitra_app/` | Flutter app for professionals/mitra (iOS + Android) |
| `control_center/` | React + Vite SPA — internal management tool |

## Tech Stack

- **Backend:** Fastify.js — single codebase, two listeners
  - Public (port 3000) → serves `client_app` and `mitra_app`
  - Internal (port 3001) → serves `control_center` only
- **Database:** PostgreSQL (GCP Cloud SQL)
- **Auth:** Firebase Auth — Google/Apple social login + phone OTP (native Flutter UI, no WebView)
- **Payment:** Xendit
- **Mobile:** Flutter (iOS + Android)
- **Control Center:** React + Vite SPA
- **Infra:** GCP — Cloud Run (backend), Cloud SQL (PostgreSQL), VPC private networking

## Key Architectural Decisions

- **One backend** serves both mobile apps — routes namespaced as `/api/client/`, `/api/mitra/`, `/api/shared/`
- **Control center is internal-only** — never expose its API routes to the public internet; protected via Nginx allow/deny + VPN
- **Firebase Auth** tokens are verified on Fastify via JWT — user data lives in PostgreSQL, linked by Firebase UID
- **Horizontal scaling** (Cloud Run) handles load — do not split into microservices prematurely
- **Real-time features** use WebSocket for chat + Valkey pub/sub as backend bus; FCM push for background notifications
- **Pairing** uses blast-to-all-available-mitras with first-come-first-served acceptance

## Current Progress

- **Phase 1 (Authentication)** — fully scaffolded
  - Backend routes, services, DB migration + seed script
  - client_app: all auth screens (welcome, display name, register, OTP, force-register)
  - mitra_app: OTP-only login
  - control_center: email/password login, mitra management, user management, anonymity settings
  - Docs: `requirement/phase1-plan.md`, `requirement/phase1-api-contract.md`, `requirement/client_app_mockup.html`
- **Phase 2 (Mitra Online Status & Pairing)** — fully scaffolded
  - Backend: Valkey pub/sub, mitra online/offline status + heartbeat + auto-offline, pairing service, session management, dashboard stats
  - client_app: "Mulai Curhat" CTA, searching/found/no-bestie/session-active screens, PairingBloc
  - mitra_app: online/offline toggle, heartbeat + lifecycle handling, incoming request notification, active sessions screen
  - control_center: dashboard (auto-refresh), max customers per mitra config, session management + reroute, mitra online logs
  - Docs: `requirement/phase2.md`, `requirement/phase2-plan.md`
- **Phase 3 (Chat Engine)** — fully scaffolded
  - Backend: WebSocket plugin, chat message service, session timer (backend-authoritative), extension/closure services, FCM push notifications, pricing service (mock tiers + free trial)
  - client_app: ChatBloc (WebSocket), ChatOpeningBloc (pricing), SessionClosureBloc, chat screen with message status (sent/delivered/read), typing indicator, extension/goodbye flow, chat history + transcript screens, FCM setup
  - mitra_app: MitraChatBloc (WebSocket), ExtensionBloc, chat screen, extension accept/reject, goodbye flow, chat history + transcript screens, FCM setup
  - control_center: free trial config, extension timeout config, early end toggles (mitra/customer)
  - Docs: `requirement/phase3.md`, `requirement/phase3-plan.md`

## Domain Concepts

- **Client** — end user seeking mental health support
- **Mitra** — trained mental health professional (partner)
- Sessions are paid; Xendit handles payment and subscription/trial logic
