#!/bin/sh
# tests/mock_login.sh - stand-in for scorpiox-copilot-login in machine mode.
#
# The plugin never speaks TLS: it shells out to the node's login CLI and reads
# only what that CLI writes. This mock reproduces the CLI's documented machine
# contract (SXLOGIN-MACHINE-V1) against a scratch directory so the whole plugin
# can be exercised offline, with no network and no real GitHub account.
#
# State lives in $COPILOT_MOCK_DIR:
#   poll_done     -> the next --poll authorizes and writes credentials
#   poll_denied   -> the next --poll reports access_denied
#   fail_start    -> --start reports a network failure
#   no_cli        -> (handled by the caller: point COPILOT_LOGIN_BIN elsewhere)
#
# Usage mirrors the real CLI: --status | --start | --poll | --cancel [--name N] [--force]

set -u

MOCK="${COPILOT_MOCK_DIR:-/tmp/copilot-mock}"
mkdir -p "$MOCK" 2>/dev/null || true

NAME=""
MODE=""
FORCE=0
while [ $# -gt 0 ]; do
    case "$1" in
        --status|--start|--poll|--cancel) MODE="$1" ;;
        --name) shift; NAME="$1" ;;
        --force) FORCE=1 ;;
        *) : ;;
    esac
    shift
done

[ -n "$NAME" ] || NAME="default"

creds_file() {
    if [ "$1" = "default" ]; then
        printf '%s' "${COPILOT_CREDS_PATH:-$HOME/.copilot/.credentials.json}"
    else
        printf '%s' "${COPILOT_ACCOUNTS_DIR:-$HOME/.copilot/accounts}/$1.json"
    fi
}

pending_file() {
    printf '%s/%s.json' "${COPILOT_PENDING_DIR:-$HOME/.claude/.login-pending}" "copilot-$1"
}

write_creds() {
    f=$(creds_file "$1")
    d=$(dirname "$f")
    mkdir -p "$d" 2>/dev/null || true
    {
        printf '{\n'
        printf '  "github_token": "gho_MOCKTOKENMUSTNOTLEAK000000000000000",\n'
        printf '  "token_type": "bearer",\n'
        printf '  "scope": "%s",\n' "${COPILOT_MOCK_SCOPE:-read:user}"
        printf '  "timestamp": "2026-01-01T00:00:00Z"\n'
        printf '}\n'
    } > "$f"
}

case "$MODE" in
--status)
    if [ -f "$(creds_file "$NAME")" ]; then logged=true; else logged=false; fi
    if [ -f "$(pending_file "$NAME")" ]; then pending=true; else pending=false; fi
    printf '{"ok":true,"provider":"copilot","name":"GitHub Copilot","flow":"device_code",'
    printf '"account":"%s","logged_in":%s,"path":"%s","expires_at":0,"expired":false,'
    printf '"has_refresh":false,"pending":%s,"profile":"copilot","profile_exists":false,'
    printf '"named_accounts":true,"protocol":"SXLOGIN-MACHINE-V1"}\n' \
        "$NAME" "$logged" "$(creds_file "$NAME")" "$pending"
    exit 0
    ;;
--start)
    if [ -f "$MOCK/fail_start" ]; then
        printf 'error: device code request failed\n' >&2
        printf '{"ok":false,"provider":"copilot","error":"network","detail":"device code request failed"}\n'
        exit 1
    fi
    # A node that already holds a token refuses a second login without --force,
    # exactly like the real CLI.
    if [ "$FORCE" = "0" ] && [ -f "$(creds_file "$NAME")" ]; then
        printf '{"ok":false,"provider":"copilot","error":"exists","detail":"credentials already exist; pass --force to replace"}\n'
        exit 1
    fi
    p=$(pending_file "$NAME")
    mkdir -p "$(dirname "$p")" 2>/dev/null || true
    printf '{"device_code":"dc_mock_value","interval":5,"force":%d,"vscode":0}\n' "$FORCE" > "$p"
    printf 'Requesting device code...\n'
    printf '{"ok":true,"provider":"copilot","flow":"device_code",'
    printf '"verify_url":"https://github.com/login/device","user_code":"1234-5678",'
    printf '"interval":5,"expires_in":900,'
    printf '"hint":"Open the link, enter the code, approve. This page checks automatically."}\n'
    exit 0
    ;;
--poll)
    p=$(pending_file "$NAME")
    if [ ! -f "$p" ]; then
        printf '{"ok":false,"provider":"copilot","error":"no_pending","detail":"run --start first"}\n'
        exit 1
    fi
    if [ -f "$MOCK/poll_denied" ]; then
        rm -f "$p"
        printf '{"ok":false,"provider":"copilot","error":"denied","detail":"access_denied"}\n'
        exit 1
    fi
    if [ -f "$MOCK/poll_done" ]; then
        rm -f "$p" "$MOCK/poll_done"
        write_creds "$NAME"
        printf '{"ok":true,"provider":"copilot","state":"done","logged_in":true,'
        printf '"path":"%s","profile":"copilot","profile_created":false}\n' "$(creds_file "$NAME")"
        exit 0
    fi
    printf '{"ok":true,"provider":"copilot","state":"pending","interval":5}\n'
    exit 0
    ;;
--cancel)
    rm -f "$(pending_file "$NAME")"
    printf '{"ok":true,"provider":"copilot","cancelled":true}\n'
    exit 0
    ;;
*)
    printf 'error: mock_login.sh needs --status|--start|--poll|--cancel\n' >&2
    exit 2
    ;;
esac
