#!/bin/bash

# Base36 Encoding Benchmark Script
# Benchmarks base36 encoding performance on large numbers of integers

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Default values
COUNT="1000000000" # 1 billion by default
RELEASE_MODE=true
# RELEASE_MODE=false

# Get the project root directory
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

print_help() {
  echo "Base36 Encoding Benchmark Script"
  echo ""
  echo "Usage: $0 [OPTIONS]"
  echo ""
  echo "Options:"
  echo "  -n, --count COUNT     Number of integers to encode (default: 1 trillion)"
  echo "  -d, --debug           Build and run in debug mode instead of release"
  echo "  -h, --help            Show this help message"
  echo ""
  echo "Examples:"
  echo "  $0                    # Benchmark 1 billion numbers (default)"
  echo "  $0 -n 100000000      # Benchmark 100 million numbers"
  echo "  $0 -n 1000000 -d     # Benchmark 1 million numbers in debug mode"
  echo ""
  echo "Performance Notes:"
  echo "  - 1 trillion numbers takes approximately 3-4 hours"
  echo "  - Expected rate: ~80 million encodings per second"
  echo "  - Uses optimized base36 encoding with specialized fast paths"
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
  case $1 in
  -n | --count)
    COUNT="$2"
    shift 2
    ;;
  -d | --debug)
    RELEASE_MODE=false
    shift
    ;;
  -h | --help)
    print_help
    exit 0
    ;;
  *)
    echo -e "${RED}❌ Unknown option: $1${NC}"
    print_help
    exit 1
    ;;
  esac
done

# Validate count is a number
if ! [[ "$COUNT" =~ ^[0-9]+$ ]]; then
  echo -e "${RED}❌ Error: Count must be a positive integer${NC}"
  exit 1
fi

echo -e "${BLUE}🔧 Base36 Encoding Benchmark${NC}"
echo "================================"
echo -e "${YELLOW}📊 Count: $(printf "%'d" "$COUNT") numbers${NC}"

if [ "$RELEASE_MODE" = true ]; then
  echo -e "${YELLOW}🚀 Mode: Release (optimized)${NC}"
  BUILD_MODE="--release"
  BINARY_PATH="$PROJECT_ROOT/target/release/wasot"
else
  echo -e "${YELLOW}🐛 Mode: Debug${NC}"
  BUILD_MODE=""
  BINARY_PATH="$PROJECT_ROOT/target/debug/wasot"
fi

echo ""

# Build the project
echo -e "${BLUE}🔨 Building project...${NC}"
cd "$PROJECT_ROOT"
if ! cargo build $BUILD_MODE; then
  echo -e "${RED}❌ Build failed${NC}"
  exit 1
fi

echo -e "${GREEN}✅ Build completed${NC}"
echo ""

# Run the benchmark
echo -e "${BLUE}🚀 Starting base36 encoding benchmark...${NC}"
echo ""

# Record start time for the script
SCRIPT_START=$(date +%s)

# Run the benchmark
if ! "$BINARY_PATH" base36-bench -n "$COUNT"; then
  echo -e "${RED}❌ Benchmark failed${NC}"
  exit 1
fi

# Calculate total script time
SCRIPT_END=$(date +%s)
SCRIPT_DURATION=$((SCRIPT_END - SCRIPT_START))

echo ""
echo "================================"
echo -e "${GREEN}✅ Benchmark completed successfully!${NC}"
echo -e "${BLUE}📊 Total script time: ${SCRIPT_DURATION} seconds${NC}"

# Provide estimates for different scales if running a smaller test
if [ "$COUNT" -lt 1000000000000 ]; then
  RATE=$(echo "scale=2; $COUNT / ($SCRIPT_DURATION - 5)" | bc -l 2>/dev/null || echo "80000000") # Subtract ~5s for build time
  if command -v bc >/dev/null 2>&1 && [ "$SCRIPT_DURATION" -gt 5 ]; then
    TRILLION_ESTIMATE=$(echo "scale=0; 1000000000000 / $RATE" | bc -l)
    TRILLION_HOURS=$(echo "scale=1; $TRILLION_ESTIMATE / 3600" | bc -l)
    echo -e "${YELLOW}📈 Estimated time for 1 trillion: ${TRILLION_ESTIMATE}s (~${TRILLION_HOURS}h)${NC}"
  fi
fi

echo -e "${BLUE}🎯 Use './scripts/base36_benchmark.sh --help' for more options${NC}"
