#!/usr/bin/env bash
set -e

echo ""

PLUGIN_PATH="$(pwd)/build/nix-ir-plugin.so"
BENCH_DIR="$(pwd)/tests/benchmark"
IRC_BIN="$(pwd)/build/nix-irc"

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

run_benchmark() {
	local name="$1"
	local file="$2"

	echo -e "${BLUE}Benchmark: $name${NC}"
	echo "----------------------------------------"

	# 1. Parse + Compile Time (nix-irc)
	echo -n "  Parse + Compile: "
	local compile_output=$( (time "$IRC_BIN" "$file" /tmp/bench.nixir 2>&1) 2>&1)
	local compile_time=$(echo "$compile_output" | grep "real" | awk '{print $2}')
	echo -e "${GREEN}$compile_time${NC}"

	# 2. IR Size
	if [ -f /tmp/bench.nixir ]; then
		local ir_size=$(stat -f%z /tmp/bench.nixir 2>/dev/null || stat -c%s /tmp/bench.nixir 2>/dev/null)
		echo -e "  IR Bundle Size:  ${GREEN}${ir_size} bytes${NC}"
	fi

	# 3. Native Nix evaluation time
	echo -n "  Native Eval:     "
	local native_time=$( (time nix-instantiate --eval --strict "$file" >/dev/null 2>&1) 2>&1 | grep "real" | awk '{print $2}')
	echo -e "${GREEN}$native_time${NC}"

	# 4. With Plugin evaluation time
	echo -n "  Plugin Eval:     "
	local plugin_time=$( (time nix-instantiate --plugin-files "$PLUGIN_PATH" --eval --strict "$file" >/dev/null 2>&1) 2>&1 | grep "real" | awk '{print $2}')
	echo -e "${GREEN}$plugin_time${NC}"

	echo ""
}

echo "# Running benchmarks..."
echo ""

run_benchmark "Simple Expressions" "$BENCH_DIR/simple.nix"
run_benchmark "Medium Complexity" "$BENCH_DIR/medium.nix"
run_benchmark "Large/Complex" "$BENCH_DIR/large.nix"

# File size comparison
echo -e "${BLUE}File Size Comparison${NC}"
echo "----------------------------------------"
testdir=$(mktemp -d)

for f in "$BENCH_DIR"/*.nix; do
	nixsize=$(stat -c%s "$f" 2>/dev/null || stat -f%z "$f" 2>/dev/null)
	base=$(basename "$f" .nix)
	irfile="${testdir}/${base}.nixir"
	$IRC_BIN "$f" "$irfile" >/dev/null 2>&1
	if [ -f "$irfile" ]; then
		irsize=$(stat -c%s "$irfile" 2>/dev/null || stat -f%z "$irfile" 2>/dev/null)
		ratio=$((irsize * 100 / nixsize))
		echo "  $base: ${nixsize}B => ${irsize}B (${ratio}% of source)"
	fi
done
