# Shared onboarding prelude for Phase 4 §4 "returning user" Maestro flows
# (TS-01 through TS-06 — see requirement/phase4-customer-flow.md).
#
# This subflow drives a clean-slate emulator from cold start to /home as a
# verified customer. The verified+display-name'd state is the precondition
# for every TS scenario in §4, so we extract it here to avoid ~80 lines of
# duplication across the six flows.
#
# Pre-reqs (parent flow's responsibility):
#   - Parent flow has `env:` block defining TEST_PHONE and
#     BACKEND_INTERNAL_URL (Maestro subflows inherit env from caller).
#   - Parent flow runs `reset_phone.js` + `launchApp clearState: true`
#     BEFORE invoking this subflow.
#   - NODE_ENV != 'production' on backend (so /internal/_test routes exist).
#
# Path taken:
#   Welcome carousel ("Mulai") → Home with anon banner →
#   tap "masuk →" → /auth/register → enter +62 subscriber digits →
#   "kirim kode" → OTP screen → peek OTP from stub → auto-submit →
#   /auth/set-name (because AuthNeedsDisplayNameData) → enter "Maestro" →
#   "Lanjut" → /home (returning view: "curhatan sebelumnya" header).
#
# Selector style: Flutter merges sibling Text widgets inside a single
# tappable parent into ONE accessibility blob. Maestro's `text:` selector
# does a FULL-string regex match, so we wrap selectors in `(?s).*…*` for
# anything that lives inside an InkWell with multiple Texts. Empty
# TextFields don't expose their hint to Maestro a11y, so we tap by point
# inside the field's pill before typing.
appId: ${APP_ID_ANDROID}
---
# Welcome carousel — the "Mulai" button is the only Text inside its
# tappable region, so a plain selector works.
- extendedWaitUntil:
    visible:
      text: "Mulai"
    timeout: 15000
- tapOn:
    text: "Mulai"
    retryTapIfNoChange: true

# Home — login-recover banner ("udah pernah pakai HaloBestie? … masuk →")
# is one InkWell, so any token within its merged blob reaches the
# /auth/register handler.
- extendedWaitUntil:
    visible:
      text: "(?s).*udah pernah pakai HaloBestie.*"
    timeout: 30000
- tapOn:
    text: "(?s).*masuk →.*"

# Register screen — personalised title "nomor wa-mu, {name}?" is in a
# merged blob with the subtitle copy.
- extendedWaitUntil:
    visible:
      text: "(?s).*nomor wa-mu.*"
    timeout: 10000

# Phone input — +62 is a static prefix chip; type only subscriber digits
# (no leading 0). +6281234567890 → 81234567890. Tap by point inside the
# pill (well right of the +62 chip, well above the kirim-kode button).
- tapOn:
    point: "60%, 47%"
- inputText: "81234567890"
- hideKeyboard
- tapOn:
    text: "(?s).*kirim kode.*"

# OTP screen — peek the code from the stub endpoint.
- 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}

# After 6th digit OTP auto-submits. The anon customer's phone is attached
# but display_name is still empty → AuthNeedsDisplayNameData → router
# pushes /auth/set-name ("Siapa namamu?").
- extendedWaitUntil:
    visible:
      text: "(?s).*Siapa namamu.*"
    timeout: 20000
# Same TextField-hint-invisible-to-Maestro issue as the phone field — tap
# by point inside the "Nama panggilan" pill, then type a display name.
- tapOn:
    point: "50%, 30%"
- inputText: "Maestro"
- hideKeyboard
- tapOn: "Lanjut"

# Now home renders the returning view ("curhatan sebelumnya" section
# header is the deterministic landmark — appears regardless of history).
- extendedWaitUntil:
    visible:
      text: "(?s).*curhatan sebelumnya.*"
    timeout: 30000
