# Stage 8 acceptance: returning-user shell.
#
# Flow:
#   1. Cold-start onboarding flow (mirrors 01_smoke) lands customer on home.
#   2. Seed a completed chat_sessions row so the bestie history list isn't empty.
#   3. Tap "Mulai Curhat" → Bestie Choice Sheet appears.
#   4. Tap "bestie yang udah kenal" → bestie history list appears.
#   5. Verify ONLINE pill renders for the seeded (online) mitra.
#   6. Tap "curhat lagi" on the row → targeted-wait screen appears with 20s
#      countdown overlay, then matches via the running mitra.
#
# Pre-req: client_app debug APK installed, backend reachable, NODE_ENV != 'production'
# so the dev-only /internal/_test routes are registered, AND a mitra is currently
# online in the dev DB (see backend/src/db/seed.js or run mitra_app to sign in).
#
# Run:
#   maestro test client_app/.maestro/flows/08_returning_targeted.yaml
appId: com.halobestie.client.client_app
env:
  TEST_PHONE: "+628155556677"
  BACKEND_INTERNAL_URL: http://localhost:3001
---
# Wipe prior state for TEST_PHONE so the run is hermetic.
- runScript:
    file: ../scripts/reset_phone.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- launchApp:
    clearState: true

# Onboarding → welcome → display name → force-register → OTP → home (matches 01_smoke).
- extendedWaitUntil:
    visible:
      text: "Mulai"
    timeout: 15000
- tapOn:
    text: "Mulai"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "Lanjut sebagai Tamu"
    timeout: 10000
- tapOn:
    text: "Lanjut sebagai Tamu"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "Nama panggilan"
    timeout: 10000
- tapOn:
    text: "Nama panggilan"
- inputText: "Maestro"
- hideKeyboard
- tapOn:
    text: "Lanjut"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "Verifikasi Akun"
    timeout: 15000
- tapOn:
    text: "Nomor HP"
- inputText: ${TEST_PHONE}
- hideKeyboard
- tapOn:
    text: "Kirim OTP"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "Masukkan OTP"
    timeout: 15000
- runScript:
    file: ../scripts/peek_otp.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- inputText: ${output.OTP}
- extendedWaitUntil:
    notVisible:
      text: "Masukkan OTP"
    timeout: 15000
- extendedWaitUntil:
    visible:
      text: "Nama panggilan"
    timeout: 10000
- tapOn:
    text: "Nama panggilan"
- inputText: "Maestro"
- hideKeyboard
- tapOn:
    text: "Lanjut"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "Mulai Curhat"
    timeout: 20000

# Seed a prior session against an online mitra.
- runScript:
    file: ../scripts/seed_history_session.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# Tap "Mulai Curhat" → Bestie Choice Sheet (returning-user variant).
- tapOn:
    text: "Mulai Curhat"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "mau curhat sama siapa?"
    timeout: 5000
- assertVisible: "bestie yang udah kenal"
- assertVisible: "bestie baru"

# Choose the known bestie path → BestieHistoryList picker (mermaid §4).
# Stage 10 renamed the screen + dropped the per-row "curhat lagi" button —
# the row tap itself is now the pick action.
- tapOn: "bestie yang udah kenal"
- extendedWaitUntil:
    visible:
      text: "bestie kamu sebelumnya"
    timeout: 5000
- assertVisible: "ONLINE"

# Tap the seeded bestie row → /payment targeted-payment route. Verify the
# screen title; the targeted-payment flow itself is covered by Stage 5.
- tapOn: "bestie ${output.MITRA_NAME}"
- extendedWaitUntil:
    visible:
      text: "Chat lagi dengan"
    timeout: 10000
