#!/bin/bash

# Test script simulating actual user conversation flow
# Project: Dr Dave EMR (Rcj5OY2xpQFHAzqUyMim)
# Context: GitHub connected, no documents uploaded

set -e

PROJECT_ID="Rcj5OY2xpQFHAzqUyMim"
BASE_URL="http://localhost:3000"

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

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Testing Actual User Conversation Flow${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Get Firebase token
echo -e "${YELLOW}Step 1: Get Firebase Auth Token${NC}"

if [ -z "$FIREBASE_TOKEN" ]; then
  echo "Please provide your Firebase ID token:"
  echo "(or set FIREBASE_TOKEN environment variable)"
  read -r FIREBASE_TOKEN
fi

if [ -z "$FIREBASE_TOKEN" ]; then
  echo -e "${RED}Error: Firebase token is required${NC}"
  exit 1
fi

echo -e "${GREEN}✓ Token received${NC}"
echo ""

# Function to send chat message
send_message() {
  local message="$1"
  local step_name="$2"
  
  echo -e "${YELLOW}${step_name}${NC}"
  echo -e "User: ${message}"
  
  local response=$(curl -s -X POST "${BASE_URL}/api/ai/chat" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${FIREBASE_TOKEN}" \
    -d "{
      \"message\": \"${message}\",
      \"projectId\": \"${PROJECT_ID}\"
    }")
  
  local ai_reply=$(echo "$response" | jq -r '.reply // .error // "No reply"')
  echo -e "AI: ${ai_reply}"
  echo ""
  
  # Return the full response for checking
  echo "$response"
}

# Function to check project phase
check_project_phase() {
  local step_name="$1"
  
  echo -e "${YELLOW}${step_name}${NC}"
  
  # Get project data from Firestore via API
  local project_data=$(curl -s "${BASE_URL}/api/projects/${PROJECT_ID}" \
    -H "Authorization: Bearer ${FIREBASE_TOKEN}")
  
  local current_phase=$(echo "$project_data" | jq -r '.currentPhase // "unknown"')
  local handoff_ready=$(echo "$project_data" | jq -r '.phaseData.phaseHandoffs.collector.readyForNextPhase // false')
  local has_extraction=$(echo "$project_data" | jq -r '.phaseData.phaseHandoffs.extraction // "null"')
  
  echo "Current Phase: ${current_phase}"
  echo "Collector Ready: ${handoff_ready}"
  echo "Has Extraction Handoff: $([ "$has_extraction" != "null" ] && echo "YES" || echo "NO")"
  echo ""
  
  echo "$project_data"
}

# Simulate the actual user conversation
echo -e "${BLUE}Starting conversation simulation...${NC}"
echo ""

# Message 1: Initial greeting (AI should welcome and ask what they have)
response1=$(send_message "Hello" "Message 1: Initial greeting")
sleep 2

# Message 2: User mentions GitHub repo
response2=$(send_message "I have a GitHub repo connected" "Message 2: User mentions GitHub")
sleep 2

# Message 3: User confirms they have everything
response3=$(send_message "that's everything" "Message 3: User confirms ready")
sleep 2

# Check if handoff was triggered
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Checking Handoff Contract${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

project_state=$(check_project_phase "Step 1: Check project state immediately after 'that's everything'")

# Wait a bit for async extraction to run
echo -e "${YELLOW}Waiting 5 seconds for backend extraction to complete...${NC}"
sleep 5

project_state_after=$(check_project_phase "Step 2: Check project state after backend extraction")

# Extract key values
current_phase=$(echo "$project_state_after" | jq -r '.currentPhase // "unknown"')
ready_for_next=$(echo "$project_state_after" | jq -r '.phaseData.phaseHandoffs.collector.readyForNextPhase // false')
has_extraction=$(echo "$project_state_after" | jq -r '.phaseData.phaseHandoffs.extraction // "null"')

# Message 4: Send another message to see if AI is in extraction review mode
response4=$(send_message "what did you find?" "Message 4: Ask about findings (should be in extraction_review mode)")

# Verify results
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Test Results${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

if [ "$ready_for_next" = "true" ]; then
  echo -e "${GREEN}✓ Collector handoff.readyForNextPhase = true${NC}"
else
  echo -e "${RED}✗ Collector handoff.readyForNextPhase = false (expected true)${NC}"
fi

if [ "$has_extraction" != "null" ]; then
  echo -e "${GREEN}✓ Extraction handoff exists${NC}"
else
  echo -e "${RED}✗ Extraction handoff missing${NC}"
fi

if [ "$current_phase" = "extraction_review" ]; then
  echo -e "${GREEN}✓ Phase transitioned to extraction_review${NC}"
else
  echo -e "${RED}✗ Phase is '${current_phase}' (expected 'extraction_review')${NC}"
fi

# Check if AI's final response mentions "processing" or "analyzing"
if echo "$response4" | jq -r '.reply' | grep -qi "processing\|analyzing\|let me analyze"; then
  echo -e "${RED}✗ AI is still saying it's processing/analyzing (should present results)${NC}"
else
  echo -e "${GREEN}✓ AI is not hallucinating processing state${NC}"
fi

echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Full Project State${NC}"
echo -e "${BLUE}========================================${NC}"
echo "$project_state_after" | jq '.'

