#!/bin/bash
# Performance Test Runner for mockupAWS v1.0.0
# Usage: ./run-performance-tests.sh [test-type] [environment]

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

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPORTS_DIR="$SCRIPT_DIR/../reports"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

# Default values
TEST_TYPE="${1:-all}"
ENVIRONMENT="${2:-local}"
BASE_URL="${BASE_URL:-http://localhost:8000}"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  mockupAWS v1.0.0 Performance Tests${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo "Test Type: $TEST_TYPE"
echo "Environment: $ENVIRONMENT"
echo "Base URL: $BASE_URL"
echo "Timestamp: $TIMESTAMP"
echo ""

# Check if k6 is installed
if ! command -v k6 &> /dev/null; then
    echo -e "${RED}Error: k6 is not installed${NC}"
    echo "Please install k6: https://k6.io/docs/get-started/installation/"
    exit 1
fi

# Create reports directory
mkdir -p "$REPORTS_DIR"

# Function to run a test
run_test() {
    local test_name=$1
    local test_script=$2
    local output_name="${TIMESTAMP}_${test_name}"
    
    echo -e "${YELLOW}Running $test_name...${NC}"
    
    k6 run \
        --out json="$REPORTS_DIR/${output_name}.json" \
        --out influxdb=http://localhost:8086/k6 \
        --env BASE_URL="$BASE_URL" \
        --env ENVIRONMENT="$ENVIRONMENT" \
        "$test_script" 2>&1 | tee "$REPORTS_DIR/${output_name}.log"
    
    if [ ${PIPESTATUS[0]} -eq 0 ]; then
        echo -e "${GREEN}✓ $test_name completed successfully${NC}"
    else
        echo -e "${RED}✗ $test_name failed${NC}"
    fi
    echo ""
}

# Health check before tests
echo -e "${YELLOW}Checking API health...${NC}"
if curl -s "$BASE_URL/health" > /dev/null; then
    echo -e "${GREEN}✓ API is healthy${NC}"
else
    echo -e "${RED}✗ API is not responding at $BASE_URL${NC}"
    exit 1
fi
echo ""

# Run tests based on type
case $TEST_TYPE in
    smoke)
        run_test "smoke" "$SCRIPT_DIR/../scripts/load-test.js"
        ;;
    load)
        run_test "load_100" "$SCRIPT_DIR/../scripts/load-test.js"
        ;;
    load-all)
        echo -e "${YELLOW}Running load tests for all user levels...${NC}"
        run_test "load_100" "$SCRIPT_DIR/../scripts/load-test.js"
        run_test "load_500" "$SCRIPT_DIR/../scripts/load-test.js"
        run_test "load_1000" "$SCRIPT_DIR/../scripts/load-test.js"
        ;;
    stress)
        run_test "stress" "$SCRIPT_DIR/../scripts/stress-test.js"
        ;;
    benchmark)
        run_test "benchmark" "$SCRIPT_DIR/../scripts/benchmark-test.js"
        ;;
    all)
        echo -e "${YELLOW}Running all performance tests...${NC}"
        run_test "smoke" "$SCRIPT_DIR/../scripts/smoke-test.js"
        run_test "load" "$SCRIPT_DIR/../scripts/load-test.js"
        run_test "stress" "$SCRIPT_DIR/../scripts/stress-test.js"
        run_test "benchmark" "$SCRIPT_DIR/../scripts/benchmark-test.js"
        ;;
    *)
        echo -e "${RED}Unknown test type: $TEST_TYPE${NC}"
        echo "Usage: $0 [smoke|load|load-all|stress|benchmark|all] [environment]"
        exit 1
        ;;
esac

# Generate summary report
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Generating Summary Report${NC}"
echo -e "${BLUE}========================================${NC}"

cat > "$REPORTS_DIR/${TIMESTAMP}_summary.md" << EOF
# Performance Test Summary

**Date:** $(date)
**Environment:** $ENVIRONMENT
**Base URL:** $BASE_URL

## Test Results

EOF

# Count results
PASSED=0
FAILED=0
for log in "$REPORTS_DIR"/${TIMESTAMP}_*.log; do
    if [ -f "$log" ]; then
        if grep -q "✓" "$log"; then
            ((PASSED++))
        elif grep -q "✗" "$log"; then
            ((FAILED++))
        fi
    fi
done

echo "- Tests Passed: $PASSED" >> "$REPORTS_DIR/${TIMESTAMP}_summary.md"
echo "- Tests Failed: $FAILED" >> "$REPORTS_DIR/${TIMESTAMP}_summary.md"
echo "" >> "$REPORTS_DIR/${TIMESTAMP}_summary.md"
echo "## Report Files" >> "$REPORTS_DIR/${TIMESTAMP}_summary.md"
echo "" >> "$REPORTS_DIR/${TIMESTAMP}_summary.md"

for file in "$REPORTS_DIR"/${TIMESTAMP}_*; do
    filename=$(basename "$file")
    echo "- $filename" >> "$REPORTS_DIR/${TIMESTAMP}_summary.md"
done

echo -e "${GREEN}✓ Summary report generated: $REPORTS_DIR/${TIMESTAMP}_summary.md${NC}"
echo ""
echo -e "${GREEN}All tests completed!${NC}"
echo "Reports saved to: $REPORTS_DIR"
