#!/usr/bin/env bash
set -uo pipefail

# -----------------------------------------------------------------------
# run_speed_test.sh
# 속도(성능) 전용 실행 wrapper
#
# --dry-run: run_test_by_change.sh --route speed --dry-run 출력만 수행한다.
# non-dry-run: performance-quick recommended면 run_performance.sh --quick 실행,
#   performance-full / stability-full nightly-pending 후보는 실행하지 않고
#   후보 command만 기록한다.
# -----------------------------------------------------------------------

usage() {
  cat <<'USAGE'
Usage: run_speed_test.sh [--dry-run]

속도(성능) 전용 테스트 wrapper.

  --dry-run    run_test_by_change.sh --route speed --dry-run 출력만 수행
  -h, --help   이 도움말 출력

dry-run이 아니면:
  - performance-quick recommended → run_performance.sh --quick 실행
  - performance-full / stability-full nightly-pending → 후보 command만 기록
USAGE
}

dry_run=0

while [ "$#" -gt 0 ]; do
  case "$1" in
    --dry-run) dry_run=1 ;;
    -h|--help) usage; exit 0 ;;
    *)
      printf 'Error: unknown option "%s"\n' "$1" >&2
      usage >&2
      exit 2
      ;;
  esac
  shift
done

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(cd "$script_dir/../../../../.." && pwd)"
classifier="$script_dir/run_test_by_change.sh"
perf_script="$script_dir/run_performance.sh"

# 1. classifier dry-run 출력 (항상 출력)
printf '## Speed route 분류 결과\n\n'
classify_output="$(bash "$classifier" --route speed --dry-run 2>&1)"
classify_exit=$?
printf '%s\n' "$classify_output"

if [ "$classify_exit" -ne 0 ]; then
  printf '\nERROR: classifier 실패 (exit %d)\n' "$classify_exit" >&2
  exit "$classify_exit"
fi

# dry-run이면 여기서 종료
[ "$dry_run" -eq 1 ] && exit 0

# 2. 추천 상태 파싱: recommended에 performance-quick이 있는지 확인
has_perf_quick=0
has_nightly_perf_full=0
has_nightly_stab_full=0

while IFS= read -r line; do
  case "$line" in
    '| recommended | performance-quick |'*)
      has_perf_quick=1
      ;;
    '| nightly-pending | performance-full |'*)
      has_nightly_perf_full=1
      ;;
    '| nightly-pending | stability-full |'*)
      has_nightly_stab_full=1
      ;;
  esac
done <<< "$classify_output"

# 3. 실행 계획 출력
printf '\n## 실행 계획\n\n'
if [ "$has_perf_quick" -eq 1 ]; then
  printf '%s\n' '- [실행 예정] performance-quick: run_performance.sh --quick'
else
  printf '%s\n' '- [skipped] performance-quick: classifier recommended 없음'
fi
if [ "$has_nightly_perf_full" -eq 1 ]; then
  printf '%s\n' '- [nightly-pending] performance-full: 20:00 이후 야간 후보. 후보 command: bash agent-ops/skills/project/run-proto-socket-test-matrix/scripts/run_performance.sh --full'
fi
if [ "$has_nightly_stab_full" -eq 1 ]; then
  printf '%s\n' '- [nightly-pending] stability-full: 20:00 이후 야간 후보. 후보 command: bash agent-ops/skills/project/run-proto-socket-test-matrix/scripts/run_stress.sh --full --profile sustained,parallel,payload'
fi

# 4. performance-quick 실행
run_exit=0
if [ "$has_perf_quick" -eq 1 ]; then
  printf '\n--- 실행: run_performance.sh --quick ---\n' >&2
  (cd "$repo_root" && bash "$perf_script" --quick)
  run_exit=$?

  printf '\n## 실행 결과: performance-quick\n\n'
  printf '%s\n' '- 실행 명령: `bash agent-ops/skills/project/run-proto-socket-test-matrix/scripts/run_performance.sh --quick`'
  printf '%s\n' '- 선택 이유: speed route — performance-quick recommended'
  printf '%s\n' "- exit code: ${run_exit}"
  local_record="$(find "$repo_root/agent-test/runs" -maxdepth 1 -name '*proto-socket-performance-quick*' -type f 2>/dev/null | sort -r | head -n 1)"
  if [ -n "$local_record" ]; then
    rel="$(realpath --relative-to="$repo_root" "$local_record" 2>/dev/null || printf '%s' "$(basename "$local_record")")"
    printf '%s\n' "- 결과 기록: ${rel}"
  fi
fi

# 5. nightly-pending 후보 요약 (미실행, PASS 아님)
if [ "$has_nightly_perf_full" -eq 1 ] || [ "$has_nightly_stab_full" -eq 1 ]; then
  printf '\n## nightly-pending 후보 (미실행, PASS 아님)\n\n'
  [ "$has_nightly_perf_full" -eq 1 ] && \
    printf '%s\n' '- performance-full: 20:00 이후 야간 후보. 후보 command: `bash agent-ops/skills/project/run-proto-socket-test-matrix/scripts/run_performance.sh --full`'
  [ "$has_nightly_stab_full" -eq 1 ] && \
    printf '%s\n' '- stability-full: 20:00 이후 야간 후보. 후보 command: `bash agent-ops/skills/project/run-proto-socket-test-matrix/scripts/run_stress.sh --full --profile sustained,parallel,payload`'
fi

exit "$run_exit"
