#!/bin/sh
# tests/mock_login.sh - stand-in for scorpiox-claudecode-login machine mode.
#
# Prints exactly the contract shape the real CLI prints (human progress lines
# first, one JSON line last), so the plugin's parsers are exercised against the
# production layout rather than an invented one. `--finish` writes a credential
# file into $CLAUDE_CREDS_PATH so a test can prove the plugin re-reads state.
#
# MOCK_FAIL=1 makes --finish fail the way Claude does for a used code.

CREDS="${CLAUDE_CREDS_PATH:-/nonexistent}"
PEND="${CLAUDE_PENDING_DIR:-/nonexistent}/claudecode-default.json"

case "$1" in
  --status)
    _li=false; _hr=false
    if [ -f "$CREDS" ] && grep -q 'refreshToken' "$CREDS" 2>/dev/null; then
      _li=true; _hr=true
    fi
    _exp=0
    if [ -f "$CREDS" ]; then
      _exp=$(sed -n 's/.*"expiresAt"[[:space:]]*:[[:space:]]*\([0-9]*\).*/\1/p' "$CREDS" | head -1)
      [ -z "$_exp" ] && _exp=0
    fi
    printf 'scorpiox-claudecode-login 1.2.3 (SXLOGIN-MACHINE-V1)\n'
    printf '{"ok":true,"provider":"claudecode","name":"Claude Code","flow":"paste_code",'
    printf '"named_accounts":true,"protocol":"SXLOGIN-MACHINE-V1","account":null,'
    printf '"logged_in":%s,"path":"%s","expires_at":%s,'
    printf '"expired":false,"has_refresh":%s,"pending":%s,'
    printf '"profile":"claude_code","profile_exists":false}\n' \
      "$_li" "$CREDS" "$_exp" "$_hr" \
      "$([ -f "$PEND" ] && echo true || echo false)"
    ;;
  --start)
    printf 'scorpiox-claudecode-login 1.2.3\n'
    printf 'generating PKCE challenge\n'
    mkdir -p "$(dirname "$PEND")" 2>/dev/null
    printf '{"code_verifier":"verifier-value","state":"st4te-value","created":1}\n' > "$PEND"
    printf '{"ok":true,"provider":"claudecode","flow":"paste_code",'
    printf '"auth_url":"https://claude.com/cai/oauth/authorize?code=true&client_id=test&state=st4te-value",'
    printf '"expires_in":600,"hint":"Sign in, then paste the code shown on the callback page."}\n'
    ;;
  --finish)
    if [ "$MOCK_FAIL" = "1" ]; then
      printf '{"ok":false,"provider":"claudecode","error":"exchange","detail":"code already used"}\n'
      exit 1
    fi
    [ -z "$2" ] && { printf '{"ok":false,"error":"no_code"}\n'; exit 1; }
    mkdir -p "$(dirname "$CREDS")" 2>/dev/null
    _now=$(date +%s)
    _exp=$(( (_now + 28800) * 1000 ))
    {
        printf '{\n  "claudeAiOauth": {\n'
        printf '    "accessToken": "sk-ant-oat01-MOCK-ACCESS-TOKEN-MUST-NOT-LEAK",\n'
        printf '    "refreshToken": "sk-ant-ort01-MOCK-REFRESH-TOKEN-MUST-NOT-LEAK",\n'
        printf '    "expiresAt": %s,\n' "$_exp"
        printf '    "scopes": ["user:profile","user:inference"],\n'
        printf '    "subscriptionType": "pro"\n  }\n}\n'
    } > "$CREDS"
    rm -f "$PEND"
    printf 'Login successful\n'
    printf '{"ok":true,"provider":"claudecode","logged_in":true,"path":"%s",'
    printf '"expires_in":28800,"profile":"claude_code","profile_created":false}\n' "$CREDS"
    ;;
  --cancel)
    rm -f "$PEND"
    printf '{"ok":true,"provider":"claudecode","cancelled":true}\n'
    ;;
  *)
    printf '{"ok":false,"error":"usage"}\n'
    exit 1
    ;;
esac
