#!/bin/sh
# tests/mock_login.sh - stand-in for scorpiox-antigravity-login machine mode.
#
# Emits exactly the contract shape the real CLI emits (one JSON line last, all
# human output before it), so the plugin's parsers are exercised against the
# production layout rather than an invented one. The exchange is simulated:
# --finish writes a credential file into $ANTIGRAVITY_CREDS_PATH's directory so
# the test can prove the plugin re-reads state after a successful login.
#
# Set MOCK_FAIL=1 to make --finish fail the way Google does when the account
# needs verification.

case "$1" in
  --status)
    if [ -n "$MOCK_STATUS_JSON" ]; then
      printf '%s\n' "$MOCK_STATUS_JSON"
    else
      # Mirror the real CLI: logged_in / has_refresh / identity all come from
      # the credential file the exchange wrote, so a test that runs --finish
      # and then --status sees the same thing a real node would.
      _acct="${ANTIGRAVITY_CREDS_PATH:-/nonexistent}"
      _li=false; _hr=false; _id=""
      if [ -f "$_acct" ] && grep -q 'refresh_token' "$_acct" 2>/dev/null; then
        _li=true; _hr=true
        _id=$(sed -n 's/.*"email"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$_acct" | head -1)
      fi
      printf 'scorpiox-antigravity-login 1.0.0 (SXLOGIN-MACHINE-V1)\n'
      printf '{"ok":true,"provider":"antigravity","name":"Antigravity (Google)","flow":"paste_code",'
      printf '"logged_in":%s,"path":"%s",' "$_li" "$_acct"
      if [ -n "$_id" ]; then printf '"identity":"%s",' "$_id"; fi
      printf '"expires_at":0,"expired":false,"has_refresh":%s,"pending":false,' "$_hr"
      printf '"profile":"antigravity","profile_exists":false}\n'
    fi
    ;;
  --start)
    printf 'generating PKCE challenge\n'
    printf '{"ok":true,"provider":"antigravity","flow":"paste_code",'
    printf '"auth_url":"https://accounts.google.com/o/oauth2/auth?client_id=test&code_challenge=abc123&code_challenge_method=S256&state=st4te",'
    printf '"expires_in":900,"hint":"Sign in with Google and approve, then paste the code."}\n'
    ;;
  --finish)
    if [ "$MOCK_FAIL" = "1" ]; then
      printf '{"ok":false,"provider":"antigravity","error":"exchange","detail":"validation required"}\n'
      exit 1
    fi
    [ -z "$2" ] && { printf '{"ok":false,"error":"no_code"}\n'; exit 1; }
    # simulate the CLI writing the credential + profile
    if [ -n "$MOCK_WRITE_DIR" ]; then
      mkdir -p "$MOCK_WRITE_DIR" "$MOCK_WRITE_DIR/scorpiox-env"
      printf '[{"email":"pico@example.com","refresh_token":"1//mock-refresh-token-value"}]\n' \
        > "$MOCK_WRITE_DIR/antigravity_accounts.json"
      printf 'pico@example.com\n' > "$MOCK_WRITE_DIR/selected_account.txt"
      printf 'PROVIDER=google_gemini\nGOOGLE_ACCOUNT=pico@example.com\nGOOGLE_PROJECT_ID=proj-mock-42\n' \
        > "$MOCK_WRITE_DIR/scorpiox-env/antigravity.txt"
    fi
    printf 'Login Successful!\n'
    printf '{"ok":true,"provider":"antigravity","state":"done","logged_in":true,'
    printf '"path":"%s","identity":"pico@example.com","profile":"antigravity","profile_created":true}\n' \
      "${ANTIGRAVITY_CREDS_PATH:-/nonexistent}"
    ;;
  --cancel)
    printf '{"ok":true,"provider":"antigravity","state":"cancelled"}\n'
    ;;
  *)
    printf '{"ok":false,"error":"usage"}\n'
    exit 1
    ;;
esac
