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

# Measures per-check first-attempt pass rate for the mandated required checks.
# Gate criterion: mandated checks must be <3% first-attempt failure before
# enabling the merge queue (Phase 3 of CI/CD evolution design, PR #1552).
#
# Usage: ./scripts/ci-flake-rate.sh [--limit N]
#   --limit N   Number of recent merged PRs to sample (default: 200)

REPO="pingcap-inc/MetaDrivenPlatform4AI"
LIMIT=200

while [[ $# -gt 0 ]]; do
  case "$1" in
    --limit) LIMIT="$2"; shift 2 ;;
    *) echo "Unknown arg: $1" >&2; exit 1 ;;
  esac
done

echo "Fetching $LIMIT most recent merged PRs..."

PRS_FILE=$(mktemp)
RUNS_FILE=$(mktemp)
trap 'rm -f "$PRS_FILE" "$RUNS_FILE"' EXIT

CURSOR=""
PAGES=$(( (LIMIT + 49) / 50 ))

for (( page=1; page<=PAGES; page++ )); do
  if [[ -z "$CURSOR" ]]; then
    CURSOR_ARG=""
  else
    CURSOR_ARG="-f cursor=$CURSOR"
  fi

  # shellcheck disable=SC2086
  RESULT=$(gh api graphql $CURSOR_ARG -f query='
  query($cursor: String) {
    repository(owner: "pingcap-inc", name: "MetaDrivenPlatform4AI") {
      pullRequests(states: MERGED, first: 50, after: $cursor, orderBy: {field: UPDATED_AT, direction: DESC}) {
        pageInfo { hasNextPage endCursor }
        nodes { number headRefOid }
      }
    }
  }')

  echo "$RESULT" | jq -r '.data.repository.pullRequests.nodes[] | "\(.number)\t\(.headRefOid)"' >> "$PRS_FILE"
  FETCHED=$(wc -l < "$PRS_FILE" | tr -d ' ')

  HAS_NEXT=$(echo "$RESULT" | jq -r '.data.repository.pullRequests.pageInfo.hasNextPage')
  CURSOR=$(echo "$RESULT" | jq -r '.data.repository.pullRequests.pageInfo.endCursor')

  if [[ "$HAS_NEXT" != "true" ]] || (( FETCHED >= LIMIT )); then break; fi
done

head -n "$LIMIT" "$PRS_FILE" > "${PRS_FILE}.trimmed" && mv "${PRS_FILE}.trimmed" "$PRS_FILE"
TOTAL_PRS=$(wc -l < "$PRS_FILE" | tr -d ' ')
echo "Fetched $TOTAL_PRS merged PRs."
echo ""

# Counters: indexed by check (0=Unit, 1=Integration, 2=Frontend, 3=E2E)
CHECK_NAMES=("Unit Tests" "Integration Tests" "Frontend Tests" "E2E API Stable Scenes")
PASS_0=0; PASS_1=0; PASS_2=0; PASS_3=0
FAIL_0=0; FAIL_1=0; FAIL_2=0; FAIL_3=0
SKIP_0=0; SKIP_1=0; SKIP_2=0; SKIP_3=0

incr_pass() { case $1 in 0) PASS_0=$((PASS_0+1));; 1) PASS_1=$((PASS_1+1));; 2) PASS_2=$((PASS_2+1));; 3) PASS_3=$((PASS_3+1));; esac; }
incr_fail() { case $1 in 0) FAIL_0=$((FAIL_0+1));; 1) FAIL_1=$((FAIL_1+1));; 2) FAIL_2=$((FAIL_2+1));; 3) FAIL_3=$((FAIL_3+1));; esac; }
incr_skip() { case $1 in 0) SKIP_0=$((SKIP_0+1));; 1) SKIP_1=$((SKIP_1+1));; 2) SKIP_2=$((SKIP_2+1));; 3) SKIP_3=$((SKIP_3+1));; esac; }

PROCESSED=0

while IFS=$'\t' read -r pr_number sha; do
  PROCESSED=$((PROCESSED + 1))
  if (( PROCESSED % 50 == 0 )); then
    echo "  processed $PROCESSED / $TOTAL_PRS PRs..." >&2
  fi

  RUNS=$(gh api "repos/$REPO/commits/$sha/check-runs" \
    --jq '.check_runs[] | "\(.name)\t\(.conclusion)\t\(.started_at)"' 2>/dev/null || true)

  for i in 0 1 2 3; do
    check="${CHECK_NAMES[$i]}"

    if [[ -z "$RUNS" ]]; then
      incr_skip $i
      continue
    fi

    first_run=$(echo "$RUNS" | grep "^${check}	" | sort -t$'\t' -k3 | head -1 || true)

    if [[ -z "$first_run" ]]; then
      incr_skip $i
      continue
    fi

    conclusion=$(echo "$first_run" | cut -f2)

    case "$conclusion" in
      success|skipped) incr_pass $i ;;
      *) incr_fail $i ;;
    esac
  done
done < "$PRS_FILE"

echo ""
echo "=== CI Flake Rate Report ==="
echo "Sample: $TOTAL_PRS merged PRs"
echo ""
printf "%-25s %8s %8s %8s %10s %s\n" "Check" "Pass" "Fail" "N/A" "Flake %" "Gate"
printf "%-25s %8s %8s %8s %10s %s\n" "-------------------------" "--------" "--------" "--------" "----------" "----"

EXIT_CODE=0
PASS_VALS=($PASS_0 $PASS_1 $PASS_2 $PASS_3)
FAIL_VALS=($FAIL_0 $FAIL_1 $FAIL_2 $FAIL_3)
SKIP_VALS=($SKIP_0 $SKIP_1 $SKIP_2 $SKIP_3)

for i in 0 1 2 3; do
  check="${CHECK_NAMES[$i]}"
  pass=${PASS_VALS[$i]}
  fail=${FAIL_VALS[$i]}
  skip=${SKIP_VALS[$i]}
  total=$((pass + fail))

  if (( total == 0 )); then
    rate="N/A"
    gate="--"
  else
    rate=$(awk "BEGIN { printf \"%.1f\", ($fail / $total) * 100 }")

    if [[ "$check" == "E2E API Stable Scenes" ]]; then
      gate="(info)"
    elif awk "BEGIN { exit !(($fail / $total) * 100 >= 3) }"; then
      gate="FAIL"
      EXIT_CODE=1
    else
      gate="OK"
    fi
  fi

  printf "%-25s %8d %8d %8d %9s%% %s\n" "$check" "$pass" "$fail" "$skip" "$rate" "$gate"
done

echo ""
if (( EXIT_CODE == 0 )); then
  echo "Result: All mandated checks under 3% flake threshold. Phase 3 (merge queue) is unblocked."
else
  echo "Result: BLOCKED — one or more mandated checks exceed 3% flake rate. Remediate before Phase 3."
fi

exit $EXIT_CODE
