#!/usr/bin/env bash

# Artifact Management Script
# Manage local benchmark artifacts

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
LOCAL_ARTIFACTS_DIR="$PROJECT_ROOT/.local-artifacts"
ARCHIVE_DIR="$LOCAL_ARTIFACTS_DIR/archive"

# Create artifacts directory if it doesn't exist
mkdir -p "$LOCAL_ARTIFACTS_DIR"

case "${1:-help}" in
"list")
	echo "=== Benchmark Runs ==="

	# Fast empty-state: no run directories.
	if ! compgen -G "$LOCAL_ARTIFACTS_DIR/run_*" >/dev/null; then
		echo "(no runs found)"
		exit 0
	fi

	# List newest-first without parsing `ls -l` output.
	for run_dir in $(glob -1dt "$LOCAL_ARTIFACTS_DIR"/run_* 2>/dev/null); do
		if [ ! -d "$run_dir" ]; then
			continue
		fi

		timestamp=$(basename "$run_dir" | sed 's/run_//')
		echo "Run: $timestamp"
		echo "  Path: $run_dir"
		if [ -f "$run_dir/report.html" ]; then
			echo "  Report: $run_dir/report.html"
		fi
		if [ -f "$run_dir/prometheus_metrics.txt" ]; then
			metrics_count=$(grep -c "benchmark_" "$run_dir/prometheus_metrics.txt" 2>/dev/null || echo "0")
			echo "  Metrics: $metrics_count benchmarks"
		fi
		echo ""
	done
	;;

"clean")
	echo "=== Cleaning Artifacts ==="
	case "${2:-all}" in
	"all")
		echo "Archiving all artifacts..."
		stamp=$(date -u +%Y%m%d-%H%M%S)
		mkdir -p "$ARCHIVE_DIR/$stamp"
		mv "$LOCAL_ARTIFACTS_DIR"/run_* "$ARCHIVE_DIR/$stamp"/ 2>/dev/null || true
		echo "All artifacts archived"
		;;
	"old")
		keep_count="${3:-10}"
		echo "Keeping last $keep_count runs, archiving older ones..."
		stamp=$(date -u +%Y%m%d-%H%M%S)
		mkdir -p "$ARCHIVE_DIR/$stamp"
		cd "$LOCAL_ARTIFACTS_DIR"
		ls -1t run_* 2>/dev/null | tail -n +$((keep_count + 1)) | while read -r run; do
			echo "Archiving: $run"
			mv "$run" "$ARCHIVE_DIR/$stamp/" 2>/dev/null || true
		done
		;;
	"run")
		run_id="${3:-}"
		if [ -z "$run_id" ]; then
			echo "Usage: $0 clean run <run_id>"
			echo "Available runs:"
			ls -1 "$LOCAL_ARTIFACTS_DIR"/run_* 2>/dev/null | sed 's/.*run_//' || echo "No runs found"
			exit 1
		fi
		run_dir="$LOCAL_ARTIFACTS_DIR/run_$run_id"
		if [ -d "$run_dir" ]; then
			echo "Archiving run: $run_id"
			stamp=$(date -u +%Y%m%d-%H%M%S)
			mkdir -p "$ARCHIVE_DIR/$stamp"
			mv "$run_dir" "$ARCHIVE_DIR/$stamp/" 2>/dev/null || true
		else
			echo "Run not found: $run_id"
		fi
		;;
	*)
		echo "Usage: $0 clean [all|old|run <run_id>]"
		exit 1
		;;
	esac
	;;

"compare")
	run1="${2:-}"
	run2="${3:-}"
	if [ -z "$run1" ] || [ -z "$run2" ]; then
		echo "Usage: $0 compare <run_id1> <run_id2>"
		echo "Available runs:"
		ls -1 "$LOCAL_ARTIFACTS_DIR"/run_* 2>/dev/null | sed 's/.*run_//' || echo "No runs found"
		exit 1
	fi

	echo "=== Comparing Runs ==="
	echo "Run 1: $run1"
	echo "Run 2: $run2"
	echo ""

	metrics1="$LOCAL_ARTIFACTS_DIR/run_$run1/prometheus_metrics.txt"
	metrics2="$LOCAL_ARTIFACTS_DIR/run_$run2/prometheus_metrics.txt"

	if [ ! -f "$metrics1" ] || [ ! -f "$metrics2" ]; then
		echo "One or both runs not found"
		exit 1
	fi

	echo "Benchmark Comparison:"
	printf "%-40s %-15s %-15s %-10s\n" "Benchmark" "Run 1 (ns)" "Run 2 (ns)" "Change"
	printf "%-40s %-15s %-15s %-10s\n" "--------" "----------" "----------" "------"

	grep "benchmark_time_per_op" "$metrics1" | while read -r line1; do
		benchmark=$(echo "$line1" | sed 's/.*benchmark="\([^"]*\)".*/\1/')
		value1=$(echo "$line1" | awk '{print $2}')

		line2=$(grep "benchmark_time_per_op.*benchmark=\"$benchmark\"" "$metrics2" || true)
		if [ -n "$line2" ]; then
			value2=$(echo "$line2" | awk '{print $2}')

			# Calculate percentage change
			if [ "$value1" != "0" ]; then
				change=$(echo "scale=2; (($value2 - $value1) / $value1) * 100" | bc 2>/dev/null || echo "N/A")
				printf "%-40s %-15s %-15s %-10s\n" "$benchmark" "$value1" "$value2" "${change}%"
			else
				printf "%-40s %-15s %-15s %-10s\n" "$benchmark" "$value1" "$value2" "N/A"
			fi
		fi
	done
	;;

"export")
	run_id="${2:-}"
	format="${3:-json}"
	if [ -z "$run_id" ]; then
		echo "Usage: $0 export <run_id> [json|csv]"
		echo "Available runs:"
		ls -1 "$LOCAL_ARTIFACTS_DIR"/run_* 2>/dev/null | sed 's/.*run_//' || echo "No runs found"
		exit 1
	fi

	run_dir="$LOCAL_ARTIFACTS_DIR/run_$run_id"
	if [ ! -d "$run_dir" ]; then
		echo "Run not found: $run_id"
		exit 1
	fi

	output_file="$LOCAL_ARTIFACTS_DIR/export_${run_id}.$format"

	case "$format" in
	"json")
		echo "Exporting run $run_id to JSON..."
		python3 -c "
import json
import sys

metrics = []
with open('$run_dir/prometheus_metrics.txt', 'r') as f:
    for line in f:
        if line.startswith('benchmark_time_per_op'):
            parts = line.strip().split()
            benchmark = parts[0].split('benchmark=\"')[1].rstrip('\"')
            value = parts[1]
            metrics.append({
                'benchmark': benchmark,
                'time_per_op_ns': int(value),
                'run_id': '$run_id'
            })

export_data = {
    'run_id': '$run_id',
    'timestamp': '$run_id',
    'metrics': metrics
}

with open('$output_file', 'w') as f:
    json.dump(export_data, f, indent=2)
"
		;;
	"csv")
		echo "Exporting run $run_id to CSV..."
		echo "benchmark,time_per_op_ns,run_id" >"$output_file"
		grep "benchmark_time_per_op" "$run_dir/prometheus_metrics.txt" | while read -r line; do
			benchmark=$(echo "$line" | sed 's/.*benchmark="\([^"]*\)".*/\1/')
			value=$(echo "$line" | awk '{print $2}')
			echo "$benchmark,$value,$run_id" >>"$output_file"
		done
		;;
	*)
		echo "Unsupported format: $format"
		exit 1
		;;
	esac

	echo "Exported to: $output_file"
	;;

"help" | *)
	echo "Artifact Management Tool"
	echo ""
	echo "Usage: $0 <command> [args]"
	echo ""
	echo "Commands:"
	echo "  list                    List all benchmark runs"
	echo "  clean [all|old|run]    Archive artifacts"
	echo "    all                   Archive all artifacts"
	echo "    old [count]           Keep last N runs (default: 10)"
	echo "    run <run_id>          Remove specific run"
	echo "  compare <run1> <run2>   Compare two benchmark runs"
	echo "  export <run_id> [format] Export run data (json|csv)"
	echo "  help                    Show this help"
	echo ""
	echo "Examples:"
	echo "  $0 list"
	echo "  $0 clean old 5"
	echo "  $0 compare 20241204_220000 20241204_230000"
	echo "  $0 export 20241204_220000 json"
	;;
esac
