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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT_DIR"

ARTIFACTS_DIR="${ARTIFACTS_DIR:-$ROOT_DIR/artifacts}"
REPORT_PREFIX="${REPORT_PREFIX:-tuning_sweep}"
MATRIX_SCRIPT="${MATRIX_SCRIPT:-$ROOT_DIR/scripts/mixed_workload_matrix.sh}"
mkdir -p "$ARTIFACTS_DIR"

REPORT_JSON="$ARTIFACTS_DIR/${REPORT_PREFIX}_report.json"
REPORT_MD="$ARTIFACTS_DIR/${REPORT_PREFIX}_report.md"
TIMESTAMP_UTC="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
COMMIT_SHA="$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")"

extract_value() {
  local file="$1"
  local profile="$2"
  local key="$3"
  grep -E "\"$profile\"[[:space:]]*:[[:space:]]*\\{" "$file" \
    | sed -nE "s/.*\"$key\"[[:space:]]*:[[:space:]]*([^,}]+).*/\\1/p"
}

extract_overall_pass() {
  local file="$1"
  grep -E "\"overall_pass\"[[:space:]]*:" "$file" | sed -nE 's/.*: (true|false).*/\1/p' | tail -n1
}

echo "Running tuning sweep configs..."

CONFIGS=(
  "default|256|0|64"
  "small_morsel_parallel|128|4|64"
  "wide_morsel_parallel|512|2|64"
  "wider_scan|256|0|96"
)

rows_json=""
rows_md=""
best_name=""
best_burst_p99=""

for cfg in "${CONFIGS[@]}"; do
  IFS='|' read -r name morsel workers scan_mult <<<"$cfg"
  cfg_prefix="${REPORT_PREFIX}_${name}"
  cfg_json="$ARTIFACTS_DIR/${cfg_prefix}_report.json"

  IR_QUERY_MORSEL_SIZE="$morsel" \
  IR_QUERY_PARALLEL_WORKERS="$workers" \
  IR_QUERY_SCAN_LIMIT_MULTIPLIER="$scan_mult" \
  REPORT_PREFIX="$cfg_prefix" \
    bash "$MATRIX_SCRIPT" >/dev/null

  overall_pass="$(extract_overall_pass "$cfg_json")"
  light_p99="$(extract_value "$cfg_json" "light" "query_p99")"
  balanced_p99="$(extract_value "$cfg_json" "balanced" "query_p99")"
  burst_p99="$(extract_value "$cfg_json" "burst" "query_p99")"
  burst_ingest_eps="$(extract_value "$cfg_json" "burst" "ingest_events_per_sec")"

  if [[ -n "$rows_json" ]]; then
    rows_json+=","
  fi
  rows_json+=$'\n    '
  rows_json+="\"$name\": {\"morsel_size\": $morsel, \"parallel_workers\": $workers, \"scan_limit_multiplier\": $scan_mult, \"overall_pass\": $overall_pass, \"light_query_p99\": $light_p99, \"balanced_query_p99\": $balanced_p99, \"burst_query_p99\": $burst_p99, \"burst_ingest_events_per_sec\": $burst_ingest_eps, \"matrix_report\": \"$(basename "$cfg_json")\"}"

  rows_md+="- ${name}: morsel=${morsel}, workers=${workers}, scan_mult=${scan_mult}, burst_p99=${burst_p99}, burst_ingest_eps=${burst_ingest_eps}, overall_pass=${overall_pass}"$'\n'

  if [[ "$overall_pass" == "true" ]]; then
    if [[ -z "$best_name" ]]; then
      best_name="$name"
      best_burst_p99="$burst_p99"
    else
      if awk -v cur="$burst_p99" -v best="$best_burst_p99" 'BEGIN { exit !(cur < best) }'; then
        best_name="$name"
        best_burst_p99="$burst_p99"
      fi
    fi
  fi
done

if [[ -z "$best_name" ]]; then
  best_name="none"
  best_burst_p99=0
fi

cat >"$REPORT_JSON" <<EOF
{
  "timestamp_utc": "$TIMESTAMP_UTC",
  "commit_sha": "$COMMIT_SHA",
  "configs": {${rows_json}
  },
  "recommendation": {
    "best_config": "$best_name",
    "selection_metric": "minimum burst_query_p99 among overall_pass=true",
    "best_burst_query_p99": $best_burst_p99
  }
}
EOF

cat >"$REPORT_MD" <<EOF
# Tuning Sweep Report

- Timestamp (UTC): $TIMESTAMP_UTC
- Commit: $COMMIT_SHA
- Selection metric: minimum burst query p99 among passing configs

## Config Results
$rows_md
## Recommendation
- best_config: $best_name
- best_burst_query_p99: $best_burst_p99
EOF

echo "Wrote:"
echo "  $REPORT_JSON"
echo "  $REPORT_MD"
