# TS-04 — Returning user picks "bestie baru" → blast happy path
# (requirement/phase4-customer-flow.md → Test Scenarios → TS-04).
#
# §4 branch covered: Choice → "bestie baru" → PickMethod → PickDuration →
#   PayMethod → Bayar → WaitPay → paid → PairRoute(baru) → BlastFlow →
#   S10 chat.
#
# What this proves: tapping "bestie baru" explicitly resets the payment
# draft (clearing any stale targetedMitraId — Stage 5.1 Risk #4
# mitigation), so the post-payment route is /chat/searching (blast), NOT
# /chat/waiting-targeted/...
#
# Pre-reqs (HARD):
#   - Backend reachable; NODE_ENV != 'production'.
#   - At least ONE mitra is online to accept the blast.
#   - The customer must qualify as "returning" so the choice sheet renders
#     (not auto-jumps to /payment/entry). We seed a history row to force
#     this. The seeded mitra does NOT need to stay online — they're not
#     the acceptor here (this is the "bestie baru" branch — blast goes to
#     whoever's online).
#   - Dev seed (backend/src/db/seed.js) does NOT auto-create mitras — sign
#     in at least one test mitra via mitra_app + heartbeat.
#
# Run:
#   maestro test client_app/.maestro/flows/ts-04_returning_baru_blast.yaml
appId: com.mybestie
env:
  TEST_PHONE: "+6281234567890"
  BACKEND_INTERNAL_URL: http://localhost:3001
---
# --- Cold-start reset + onboarding prelude ---
- runScript:
    file: ../scripts/reset_phone.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}
- launchApp:
    clearState: true
- runFlow: ../subflows/onboarding_returning_user.yaml

# --- Seed history so the customer qualifies as "returning" and the choice
# sheet (with both options) renders. The seeded mitra is who'll later
# accept the blast — they just need to stay online. ---
- runScript:
    file: ../scripts/seed_history_session.js
    env:
      TEST_PHONE: ${TEST_PHONE}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

- swipe:
    start: "50%, 30%"
    end: "50%, 80%"
- extendedWaitUntil:
    visible:
      text: "(?s).*curhatan sebelumnya.*"
    timeout: 10000

# --- Steps 1-2: tap CTA → choice sheet → "bestie baru" ---
- tapOn:
    text: "(?s).*curhat sama bestie baru.*"
    retryTapIfNoChange: true
- extendedWaitUntil:
    visible:
      text: "(?s).*mau curhat sama siapa.*"
    timeout: 5000

# "bestie baru" branch — onTap path resets the draft + pushes
# /payment/entry. The label "bestie baru" alone is too short and may also
# match the CTA we just tapped on home; use the subtitle text "cari bestie
# baru yang siap dengerin" which is unique to this card.
- tapOn: "(?s).*cari bestie baru yang siap dengerin.*"

# /payment/entry → /payment/method-pick (returning customer, no discount).
- extendedWaitUntil:
    visible:
      text: "(?s).*pilih cara curhat.*"
    timeout: 10000

# --- Step 3: pick chat → duration → tier → cara bayar → bayar ---
- tapOn:
    text: "(?s).*tulis dan baca dengan tenang.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*pilih durasi.*"
    timeout: 10000
- tapOn: "(?s).*5 menit.*"
- tapOn: "(?s).*bayar Rp.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*cara bayar.*"
    timeout: 10000
- tapOn:
    text: "(?s).*bayar Rp.*"
    retryTapIfNoChange: true

# --- Step 4: waiting-payment → force-confirm ---
- extendedWaitUntil:
    visible:
      text: "scan QRIS untuk bayar"
    timeout: 10000
- runScript:
    file: ../scripts/mark_latest_payment_paid.js
    env:
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# --- Step 5: notif-gate → /chat/searching (BLAST, not targeted-wait) ---
- runFlow:
    when:
      visible:
        text: "(?s).*biar nggak ketinggalan.*"
    commands:
      - tapOn:
          text: "(?s).*nanti aja.*"
- extendedWaitUntil:
    visible:
      text: "(?s).*lagi nyari bestie.*"
    timeout: 20000

# --- Step 6: any online mitra accepts the blast ---
# We use the seeded mitra's id as a known-online acceptor. (If they're no
# longer online, the assertion below will time out — re-run after
# refreshing their heartbeat.)
- runScript:
    file: ../scripts/mitra_accept_latest_internal.js
    env:
      MITRA_ID: ${output.MITRA_ID}
      BACKEND_INTERNAL_URL: ${BACKEND_INTERNAL_URL}

# --- Step 7: chat screen renders ---
- extendedWaitUntil:
    visible:
      text: "(?s).*online.*"
    timeout: 20000
- assertVisible: "(?s).*${output.MITRA_NAME_RE}.*"
