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

# -----------------------------------------------------------------------
# run_stability_test.sh
# 안정성 전용 실행 wrapper
#
# --dry-run: run_test_by_change.sh --route stability --dry-run 출력만 수행한다.
# non-dry-run: stability-quick recommended면 run_stress.sh --quick 실행(hard gate),
#   stability-full nightly-pending 후보는 실행하지 않고 후보 command만 기록한다.
#   hard gate 판정: timeout, nonce/type mismatch, FIFO, pending leak,
#                    queue/gateway backlog가 모두 0이어야 PASS.
# -----------------------------------------------------------------------

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

안정성 전용 테스트 wrapper.

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

dry-run이 아니면:
  - stability-quick recommended → run_stress.sh --quick --profile sustained,parallel,payload 실행 (hard gate)
  - stability-full nightly-pending → 후보 command만 기록 (미실행, PASS 아님)
  hard gate: timeout / nonce mismatch / response type mismatch /
             per-connection FIFO violation / pending leak / queue/gateway backlog leak가 모두 0이어야 한다.
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"
stress_script="$script_dir/run_stress.sh"

# 1. classifier dry-run 출력 (항상 출력)
printf '## Stability route 분류 결과\n\n'
classify_output="$(bash "$classifier" --route stability --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. 추천 상태 파싱
has_stab_quick=0
has_nightly_stab_full=0
has_nightly_perf_full=0

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

# 3. 실행 계획 출력
printf '\n## 실행 계획\n\n'
if [ "$has_stab_quick" -eq 1 ]; then
  printf '%s\n' '- [실행 예정] stability-quick (hard gate): run_stress.sh --quick --profile sustained,parallel,payload'
  printf '%s\n' '  hard gate: timeout / nonce mismatch / response type mismatch / FIFO violation / pending leak / queue/gateway backlog leak 모두 0'
else
  printf '%s\n' '- [skipped] stability-quick: classifier recommended 없음'
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
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

# 4. stability-quick 실행 (hard gate)
run_exit=0
if [ "$has_stab_quick" -eq 1 ]; then
  printf '\n--- 실행: run_stress.sh --quick --profile sustained,parallel,payload ---\n' >&2
  (cd "$repo_root" && bash "$stress_script" --quick --profile sustained,parallel,payload)
  run_exit=$?

  printf '\n## 실행 결과: stability-quick\n\n'
  printf '%s\n' '- 실행 명령: `bash agent-ops/skills/project/run-proto-socket-test-matrix/scripts/run_stress.sh --quick --profile sustained,parallel,payload`'
  printf '%s\n' '- 선택 이유: stability route — stability-quick hard gate 점검'
  printf '%s\n' "- exit code: ${run_exit}"
  printf '%s\n' '- hard gate 판정 기준: timeout / nonce mismatch / response type mismatch / FIFO violation / pending leak / queue/gateway backlog leak 모두 0'
  local_record="$(find "$repo_root/agent-test/runs" -maxdepth 1 -name '*proto-socket-stress-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
  if [ "$run_exit" -ne 0 ]; then
    printf '%s\n' '- 판정: hard gate 위반 — stability PASS 아님'
  else
    printf '%s\n' '- 판정: hard gate 통과'
  fi
fi

# 5. nightly-pending 후보 요약 (미실행, PASS 아님)
if [ "$has_nightly_stab_full" -eq 1 ] || [ "$has_nightly_perf_full" -eq 1 ]; then
  printf '\n## nightly-pending 후보 (미실행, PASS 아님)\n\n'
  [ "$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`'
  [ "$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`'
fi

exit "$run_exit"
