#!/bin/bash
# Quick-start script for running user testing suite

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

cd "$PROJECT_ROOT"

echo "🧪 Kraiken User Testing Suite"
echo "=============================="
echo ""

# Check if stack is running
echo "📊 Checking stack health..."
if ! curl -s http://localhost:8081/api/rpc > /dev/null 2>&1; then
    echo "❌ Stack is not running!"
    echo ""
    echo "Please start the stack first:"
    echo "  ./scripts/dev.sh start"
    echo ""
    exit 1
fi

echo "✅ Stack is running"
echo ""

# Create output directories
echo "📁 Creating output directories..."
mkdir -p tmp/usertest-results
mkdir -p test-results/usertest
echo "✅ Directories ready"
echo ""

# Parse arguments
PERSONA=""
DEBUG_MODE=""
HEADED_MODE=""

while [[ $# -gt 0 ]]; do
    case $1 in
        --persona)
            PERSONA="$2"
            shift 2
            ;;
        --debug)
            DEBUG_MODE="--debug"
            shift
            ;;
        --headed)
            HEADED_MODE="--headed"
            shift
            ;;
        *)
            echo "Unknown option: $1"
            echo "Usage: $0 [--persona <name>] [--debug] [--headed]"
            echo ""
            echo "Personas:"
            echo "  marcus    - Marcus 'Flash' Chen (Degen/MEV Hunter)"
            echo "  sarah     - Sarah Park (Cautious Yield Farmer)"
            echo "  tyler     - Tyler 'Bags' Morrison (Retail Degen)"
            echo "  priya     - Dr. Priya Malhotra (Institutional)"
            echo "  alex      - Alex Rivera (Newcomer)"
            echo ""
            exit 1
            ;;
    esac
done

# Determine which tests to run
if [ -z "$PERSONA" ]; then
    echo "🎭 Running ALL personas..."
    TEST_PATTERN="tests/e2e/usertest/"
else
    case $PERSONA in
        marcus)
            echo "🎭 Running Marcus 'Flash' Chen (Degen)..."
            TEST_PATTERN="tests/e2e/usertest/marcus-degen.spec.ts"
            ;;
        sarah)
            echo "🎭 Running Sarah Park (Yield Farmer)..."
            TEST_PATTERN="tests/e2e/usertest/sarah-yield-farmer.spec.ts"
            ;;
        tyler)
            echo "🎭 Running Tyler 'Bags' Morrison (Retail)..."
            TEST_PATTERN="tests/e2e/usertest/tyler-retail-degen.spec.ts"
            ;;
        priya)
            echo "🎭 Running Dr. Priya Malhotra (Institutional)..."
            TEST_PATTERN="tests/e2e/usertest/priya-institutional.spec.ts"
            ;;
        alex)
            echo "🎭 Running Alex Rivera (Newcomer)..."
            TEST_PATTERN="tests/e2e/usertest/alex-newcomer.spec.ts"
            ;;
        *)
            echo "❌ Unknown persona: $PERSONA"
            echo ""
            echo "Available personas: marcus, sarah, tyler, priya, alex"
            exit 1
            ;;
    esac
fi

echo ""
echo "▶️  Starting tests..."
echo ""

# Run tests with workers=1 to avoid account conflicts
npx playwright test "$TEST_PATTERN" --workers=1 $DEBUG_MODE $HEADED_MODE

echo ""
echo "✅ Tests complete!"
echo ""
echo "📊 Results:"
echo "  - Screenshots: test-results/usertest/"
echo "  - JSON reports: tmp/usertest-results/"
echo ""

# Show report files
if [ -d "tmp/usertest-results" ]; then
    echo "Generated reports:"
    ls -lh tmp/usertest-results/*.json 2>/dev/null | awk '{print "  - " $9 " (" $5 ")"}'
fi

echo ""
echo "🔍 To analyze results:"
echo "  cat tmp/usertest-results/<persona-name>.json | jq"
echo ""
