#!/bin/bash

# Test Collector Handoff Persistence
# Sends messages and checks if handoff data is being persisted

GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

PROJECT_ID="lyOZxelSkjAB6XisIzup"
BASE_URL="http://localhost:3000"
TOKEN="Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjM4MDI5MzRmZTBlZWM0NmE1ZWQwMDA2ZDE0YTFiYWIwMWUzNDUwODMiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoiTWFyayBIZW5kZXJzb24iLCJwaWN0dXJlIjoiaHR0cHM6Ly9hdmF0YXJzLmdpdGh1YnVzZXJjb250ZW50LmNvbS91LzUzOTU0MjEzP3Y9NCIsImlzcyI6Imh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlLmNvbS9nZW4tbGFuZy1jbGllbnQtMDk4MDA3OTQxMCIsImF1ZCI6Imdlbi1sYW5nLWNsaWVudC0wOTgwMDc5NDEwIiwiYXV0aF90aW1lIjoxNzYzMzI1MDEyLCJ1c2VyX2lkIjoiMmhDdmdXQzJaV2RJMGVlTm5SQVM3SWVKcmg1MiIsInN1YiI6IjJoQ3ZnV0MyWldkSTBlZU5uUkFTN0llSnJoNTIiLCJpYXQiOjE3NjM0MjI1NDUsImV4cCI6MTc2MzQyNjE0NSwiZW1haWwiOiJtYXJrQGdldGFjcXVpcmVkLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJnaXRodWIuY29tIjpbIjUzOTU0MjEzIl0sImVtYWlsIjpbIm1hcmtAZ2V0YWNxdWlyZWQuY29tIl19LCJzaWduX2luX3Byb3ZpZGVyIjoiZ2l0aHViLmNvbSJ9fQ.TpMOORDnPUKkbLlg-KtYBmbarEjAijJ3W4vN8tWT6OslOfwaeDJAtPXIahyQk38UvKY4ZGognQG6t-laSATB8yIC8IdkYbD699axfPSGQqC8Lbux1P6YrFKOPLGDD2XemBtJ-Gb5Ql-nK_DbXKAmygLxIwz019XpLJEucGkBPAN_Rj2xC7125DVexkDSIb6ZnbLiDgCpR_IkImyQb08tqlOoBiHVUa-4VGDhraoBPACJfQXwPToJ1W3nhBiVtMvSq7s_Ekd8Otn8AB_1teu5lxC-rhLdgJuNrmlxO-H6xIMBFZ72bwq7wrvdWd_EijqFQCU99oEhphTNoISoJ3wK-g"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Testing Handoff Persistence${NC}"
echo -e "${BLUE}========================================${NC}\n"

# Send a message and capture full response with verbose logging
echo -e "${YELLOW}[Test]${NC} Sending message to AI with verbose logging enabled...\n"

# Enable curl verbose output and check server response
curl -v -X POST "${BASE_URL}/api/ai/chat" \
  -H "Authorization: ${TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{"projectId": "'"${PROJECT_ID}"'", "message": "I have uploaded 1 document. I do not have GitHub or the extension yet."}' \
  2>&1 | tee /tmp/curl-output.txt

echo -e "\n\n${BLUE}========================================${NC}"
echo -e "${BLUE}Analysis${NC}"
echo -e "${BLUE}========================================${NC}\n"

# Check if the response contains expected fields
if grep -q '"mode":"collector_mode"' /tmp/curl-output.txt; then
  echo -e "${GREEN}✓ Mode is collector_mode${NC}"
else
  echo -e "${RED}✗ Mode not detected${NC}"
fi

if grep -q '"projectPhase"' /tmp/curl-output.txt; then
  echo -e "${GREEN}✓ Project phase returned${NC}"
else
  echo -e "${RED}✗ Project phase not returned${NC}"
fi

# Look for any indication of handoff data in server logs
echo -e "\n${YELLOW}Note:${NC} The collectorHandoff data is persisted to Firestore,"
echo -e "but not returned in the API response. This is expected."
echo -e "\nTo verify persistence, check:"
echo -e "  1. Server console logs for '[AI Chat] Collector handoff persisted'"
echo -e "  2. Firebase Console: projects/${PROJECT_ID}/phaseData/phaseHandoffs/collector"
echo -e "  3. UI checklist: ${BASE_URL}/default/project/${PROJECT_ID}/v_ai_chat\n"

rm -f /tmp/curl-output.txt

