#!/bin/bash

# Test Vision Flow - Automated testing of 3-question flow
# Usage: ./test-vision-flow.sh <projectId>

set -e

if [ -z "$1" ]; then
  echo "❌ Error: Please provide a projectId"
  echo "Usage: ./test-vision-flow.sh <projectId>"
  exit 1
fi

PROJECT_ID="$1"
WORKSPACE="marks-account"
API_URL="http://localhost:3000/api/ai/chat"

# Load test questions
Q1=$(grep "^q1=" .test-questions | cut -d'=' -f2-)
Q2=$(grep "^q2=" .test-questions | cut -d'=' -f2-)
Q3=$(grep "^q3=" .test-questions | cut -d'=' -f2-)

echo "🧪 Testing Vision Flow"
echo "Project ID: $PROJECT_ID"
echo ""
echo "Questions loaded:"
echo "  Q1: ${Q1:0:50}..."
echo "  Q2: ${Q2:0:50}..."
echo "  Q3: ${Q3:0:50}..."
echo ""

# Function to send chat message
send_message() {
  local message="$1"
  local step="$2"
  
  echo "[$step] Sending: ${message:0:60}..."
  
  response=$(curl -s -X POST "$API_URL" \
    -H "Content-Type: application/json" \
    -d "{
      \"message\": \"$message\",
      \"projectId\": \"$PROJECT_ID\",
      \"workspaceId\": \"$WORKSPACE\"
    }")
  
  # Check for error
  if echo "$response" | grep -q '"error"'; then
    echo "❌ Error response:"
    echo "$response" | jq '.'
    exit 1
  fi
  
  # Extract reply
  reply=$(echo "$response" | jq -r '.reply // .message // "No reply"')
  echo "[$step] AI Reply: ${reply:0:80}..."
  echo ""
  
  # Small delay between messages
  sleep 2
}

echo "🚀 Starting test..."
echo ""

# Send Q1
send_message "$Q1" "Q1"

# Send Q2
send_message "$Q2" "Q2"

# Send Q3
send_message "$Q3" "Q3"

echo "✅ All 3 questions sent!"
echo ""
echo "🔍 Checking logs for success markers..."
sleep 2

# Check terminal logs for success
TERMINAL_LOG="$HOME/.cursor/projects/Users-markhenderson-ai-proxy/terminals/13.txt"

if tail -100 "$TERMINAL_LOG" | grep -q "All 3 vision answers complete"; then
  echo "✅ SUCCESS: Found 'All 3 vision answers complete' in logs"
else
  echo "❌ FAILED: Did not find success marker in logs"
  echo ""
  echo "Last 20 lines of log:"
  tail -20 "$TERMINAL_LOG"
  exit 1
fi

if tail -100 "$TERMINAL_LOG" | grep -q "readyForMVP.*true\|readyForExtraction.*true"; then
  echo "✅ SUCCESS: MVP generation flag set"
else
  echo "⚠️  WARNING: MVP generation flag not found in logs"
fi

echo ""
echo "🎉 Vision flow test completed!"
echo ""
echo "📊 Check Firestore to verify data was saved:"
echo "   Project: $PROJECT_ID"
echo "   Expected fields: visionAnswers.q1, q2, q3, allAnswered, readyForMVP"

