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

# Coverage gate for vyre-conform.
# Usage: coverage.sh [standard|legendary|report]
#   standard  - Run cargo llvm-cov with floor: lines>=85%, functions>=90%.
#   legendary - Enforce 100% line+function coverage on conform/src/enforce/ via JSON post-processing.
#   report    - Print top 10 lowest-coverage files from the last standard run.

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

if ! cargo llvm-cov --version &>/dev/null; then
    echo "Fix: cargo-llvm-cov is not installed. Install with: cargo install cargo-llvm-cov --locked"
    exit 1
fi

MODE="${1:-standard}"

JSON_PATH="target/coverage/vyre-conform-coverage.json"

run_json_report() {
    mkdir -p "$(dirname "$JSON_PATH")"
    cargo llvm-cov --package vyre-conform --offline report --json --output-path "$JSON_PATH"
}

case "$MODE" in
  standard)
    echo "[coverage] Running standard floor (lines >=85%, functions >=90%)..."
    set +e
    cargo llvm-cov --package vyre-conform --offline \
        --html --output-dir target/coverage \
        --fail-under-lines 85 --fail-under-functions 90
    gate_exit=$?
    set -e

    run_json_report

    if [ "$gate_exit" -ne 0 ]; then
        echo "Fix: standard coverage gate FAILED (exit $gate_exit)."
        exit "$gate_exit"
    fi
    echo "[coverage] Standard gate passed. HTML report: target/coverage/index.html"
    ;;

  legendary)
    echo "[coverage] Running legendary gate for conform/src/enforce/ (lines >=100%, functions >=100%)..."
    run_json_report

    # Extract enforce/ coverage from JSON.
    line_data="$(jq -r '
      .data[0].files[]
      | select(.filename | contains("conform/src/enforce/"))
      | .summary.lines
      | "\(.count) \(.covered)"
    ' "$JSON_PATH" | awk '{count+=$1; covered+=$2} END {if(count>0) printf "%.4f", covered/count*100; else print "100"}')"

    func_data="$(jq -r '
      .data[0].files[]
      | select(.filename | contains("conform/src/enforce/"))
      | .summary.functions
      | "\(.count) \(.covered)"
    ' "$JSON_PATH" | awk '{count+=$1; covered+=$2} END {if(count>0) printf "%.4f", covered/count*100; else print "100"}')"

    echo "[coverage] enforce/ line coverage:   ${line_data}%"
    echo "[coverage] enforce/ function coverage: ${func_data}%"

    fail=0
    if awk "BEGIN {exit !($line_data < 100)}"; then
        echo "Fix: enforce/ line coverage ($line_data%) is below 100%."
        fail=1
    fi
    if awk "BEGIN {exit !($func_data < 100)}"; then
        echo "Fix: enforce/ function coverage ($func_data%) is below 100%."
        fail=1
    fi

    if [ "$fail" -eq 1 ]; then
        exit 1
    fi
    echo "[coverage] Legendary gate passed."
    ;;

  report)
    if [ ! -f "$JSON_PATH" ]; then
        echo "Fix: no coverage JSON found at $JSON_PATH. Run 'coverage.sh standard' first."
        exit 1
    fi

    echo "[coverage] Top 10 lowest line-coverage files:"
    jq -r '
      .data[0].files[]
      | select(.summary.lines.count > 0)
      | [ (.summary.lines.covered / .summary.lines.count * 100), .summary.lines.covered, .summary.lines.count, .filename ]
      | @tsv
    ' "$JSON_PATH" | sort -t$'\t' -k1 -n | head -n 10 | while IFS=$'\t' read -r pct covered count filename; do
        printf "%6.2f%% (%s/%s) %s\n" "$pct" "$covered" "$count" "$filename"
    done
    ;;

  *)
    echo "Usage: $0 [standard|legendary|report]"
    exit 1
    ;;
esac
