#!/usr/bin/env bash
# bash harness for logs-read.sh single-shape resolution.
#
# Covers the three cases the new contract demands:
#   1. sessionKey-named file present → bytes returned, exit 0.
#   2. No file → exit 1 + missing-on-resolve appended to server.log.
#   3. Ambiguous prefix (multiple matches) → exit 1, never picks silently.
#
# MIRROR: see platform/ui/app/lib/logs-read-resolve.ts for the canonical
# single-shape contract and its TS unit tests.
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOGS_READ="$SCRIPT_DIR/logs-read.sh"
if [[ ! -x "$LOGS_READ" ]]; then
  echo "FATAL: $LOGS_READ not executable" >&2
  exit 1
fi

SESSION_KEY="8c264cfb-441f-48bf-9811-9fa3ad3b51dc"
FILE_NAME="claude-agent-stream-${SESSION_KEY}.log"

PASS=0
FAIL=0

setup_install_tree() {
  local root="$1"
  mkdir -p "$root/platform/scripts"
  mkdir -p "$root/data/accounts/acct-test/logs"
  mkdir -p "$HOME/.$(basename "$root")/logs"
  ln -sf "$LOGS_READ" "$root/platform/scripts/logs-read.sh"
  echo "$root/platform/scripts/logs-read.sh"
}

cleanup_install_tree() {
  local root="$1"
  rm -rf "$root"
  rm -rf "$HOME/.$(basename "$root")"
}

run_case() {
  local name="$1"
  shift
  echo ""
  echo "=== CASE: $name ==="
  if "$@"; then
    PASS=$((PASS + 1))
    echo "PASS: $name"
  else
    FAIL=$((FAIL + 1))
    echo "FAIL: $name"
  fi
}

# --- Case 1: sessionKey-named file present → exit 0 ---
case_file_present() {
  local root="/tmp/maxy-logs-read-test-1-$$"
  local script
  script=$(setup_install_tree "$root")
  local logdir="$root/data/accounts/acct-test/logs"
  echo "sessionkey-content-sentinel" > "$logdir/$FILE_NAME"

  local stdout stderr rc
  stdout=$("$script" "$SESSION_KEY" system 2>/tmp/maxy-logs-read-stderr-1-$$) || rc=$?
  rc=${rc:-0}
  stderr=$(cat /tmp/maxy-logs-read-stderr-1-$$)
  rm -f /tmp/maxy-logs-read-stderr-1-$$

  cleanup_install_tree "$root"

  if [[ $rc -ne 0 ]]; then
    echo "  expected exit 0, got $rc"
    return 1
  fi
  if [[ "$stdout" != *"sessionkey-content-sentinel"* ]]; then
    echo "  stdout missing sentinel"
    return 1
  fi
  if [[ "$stderr" != *"matched=1"* ]]; then
    echo "  stderr missing matched=1: $stderr"
    return 1
  fi
  return 0
}

# --- Case 2: no file → exit 1 + missing-on-resolve in server.log ---
case_missing() {
  local root="/tmp/maxy-logs-read-test-2-$$"
  local script
  script=$(setup_install_tree "$root")
  local server_log="$HOME/.$(basename "$root")/logs/server.log"
  : > "$server_log"

  local rc=0
  "$script" "$SESSION_KEY" system >/dev/null 2>/tmp/maxy-logs-read-stderr-2-$$ || rc=$?
  local stderr
  stderr=$(cat /tmp/maxy-logs-read-stderr-2-$$)
  rm -f /tmp/maxy-logs-read-stderr-2-$$
  local server_log_contents
  server_log_contents=$(cat "$server_log" 2>/dev/null || echo "")

  cleanup_install_tree "$root"

  if [[ $rc -ne 1 ]]; then
    echo "  expected exit 1, got $rc"
    return 1
  fi
  if [[ "$stderr" != *"reason=file-not-found"* ]]; then
    echo "  stderr missing reason=file-not-found: $stderr"
    return 1
  fi
  if [[ "$server_log_contents" != *"missing-on-resolve"* ]]; then
    echo "  server.log missing missing-on-resolve emission: $server_log_contents"
    return 1
  fi
  if [[ "$server_log_contents" != *"surface=logs-read-sh"* ]]; then
    echo "  server.log emission missing surface tag"
    return 1
  fi
  return 0
}

# --- Case 3: ambiguous prefix → exit 1 without picking ---
case_ambiguous() {
  local root="/tmp/maxy-logs-read-test-3-$$"
  local script
  script=$(setup_install_tree "$root")
  local logdir="$root/data/accounts/acct-test/logs"
  echo "a" > "$logdir/claude-agent-stream-deadbeef-aaaa.log"
  echo "b" > "$logdir/claude-agent-stream-deadbeef-bbbb.log"

  local rc=0
  "$script" "deadbeef" system >/dev/null 2>/tmp/maxy-logs-read-stderr-3-$$ || rc=$?
  local stderr
  stderr=$(cat /tmp/maxy-logs-read-stderr-3-$$)
  rm -f /tmp/maxy-logs-read-stderr-3-$$

  cleanup_install_tree "$root"

  if [[ $rc -ne 1 ]]; then
    echo "  expected exit 1, got $rc"
    return 1
  fi
  if [[ "$stderr" != *"reason=ambiguous-prefix"* ]]; then
    echo "  stderr missing ambiguous-prefix trailer: $stderr"
    return 1
  fi
  return 0
}

run_case "sessionKey file present → exit 0" case_file_present
run_case "no file → exit 1 + missing-on-resolve" case_missing
run_case "ambiguous prefix → exit 1, refuses to pick" case_ambiguous

echo ""
echo "================================================"
echo " Passed: $PASS / Failed: $FAIL"
echo "================================================"
[[ $FAIL -eq 0 ]]
