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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
RESULTS_DIR="$PROJECT_ROOT/tests/test-results"

cleanup() {
  if [ -d "$RESULTS_DIR" ]; then
    echo "Results saved to $RESULTS_DIR"
  fi
}
trap cleanup EXIT

mkdir -p "$RESULTS_DIR"

echo "════════════════════════════════════"
echo " Performance Tests"
echo "════════════════════════════════════"
echo ""
echo "Thresholds (from test spec):"
echo "  PT-AUG-01  Augmentation 10 images   ≤ 60s"
echo "  PT-AUG-02  Parallel speedup          ≥ 1.5×"
echo "  PT-DSF-01  Dataset formation 100 img  ≤ 30s"
echo "  PT-ENC-01  Encrypt/decrypt 10MB       ≤ 5s"
echo "  PT-INF-01  ONNX inference single      ≤ 10s"
echo ""

cd "$PROJECT_ROOT"

FAILED=0

if python -m pytest tests/performance/ \
  --tb=short \
  --junitxml="$RESULTS_DIR/performance-results.xml" \
  -v; then
  echo ""
  echo "✓ All performance thresholds met"
else
  echo ""
  echo "✗ Some performance thresholds exceeded"
  FAILED=1
fi

echo ""
echo "════════════════════════════════════"
echo " Summary"
echo "════════════════════════════════════"

if [ -f "$RESULTS_DIR/performance-results.xml" ]; then
  python -c "
import xml.etree.ElementTree as ET
t = ET.parse('$RESULTS_DIR/performance-results.xml').getroot()
print(f\"Tests: {t.get('tests',0)}  Failures: {t.get('failures',0)}  Errors: {t.get('errors',0)}\")
" 2>/dev/null || echo "Could not parse performance results XML"
fi

if [ $FAILED -ne 0 ]; then
  echo "EXIT: 1 (thresholds exceeded)"
  exit 1
fi

echo "EXIT: 0 (all thresholds met)"
exit 0
