#!/bin/bash

# Simple Handoff Verification - Uses API endpoint to check handoff state

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}  Collector Handoff Verification${NC}"
echo -e "${BLUE}========================================${NC}\n"

# Send a test message that should trigger handoff update
echo -e "${YELLOW}[Step 1]${NC} Sending message to check handoff state..."

CHAT_RESPONSE=$(curl -s -X POST "${BASE_URL}/api/ai/chat" \
  -H "Authorization: ${TOKEN}" \
  -H "Content-Type: application/json" \
  -d "{
    \"projectId\": \"${PROJECT_ID}\",
    \"message\": \"What do you know about my project so far?\"
  }")

AI_REPLY=$(echo "$CHAT_RESPONSE" | jq -r '.reply // empty')

if [ -z "$AI_REPLY" ]; then
  echo -e "${RED}✗ Failed to get AI response${NC}"
  echo "Response: $CHAT_RESPONSE"
  exit 1
fi

echo -e "${GREEN}✓ AI responded${NC}"
echo -e "\nAI Reply:\n${AI_REPLY}\n"

# Check the conversation for handoff indicators
echo -e "${YELLOW}[Step 2]${NC} Checking conversation history..."

HISTORY=$(curl -s -X GET "${BASE_URL}/api/ai/conversation?projectId=${PROJECT_ID}" \
  -H "Authorization: ${TOKEN}")

MESSAGE_COUNT=$(echo "$HISTORY" | jq '.messages | length')
echo -e "${GREEN}✓ Found ${MESSAGE_COUNT} messages in history${NC}\n"

# Look for handoff indicators in AI responses
echo -e "${YELLOW}[Step 3]${NC} Analyzing AI responses for handoff tracking...\n"

HAS_DOC_MENTION=$(echo "$AI_REPLY" | grep -i "document\|uploaded" && echo "yes" || echo "no")
HAS_GITHUB_MENTION=$(echo "$AI_REPLY" | grep -i "github\|repo" && echo "yes" || echo "no")
HAS_EXTENSION_MENTION=$(echo "$AI_REPLY" | grep -i "extension\|browser" && echo "yes" || echo "no")

if [ "$HAS_DOC_MENTION" = "yes" ]; then
  echo -e "${GREEN}✓ AI is tracking documents${NC}"
else
  echo -e "${YELLOW}⚠ AI doesn't mention documents in response${NC}"
fi

if [ "$HAS_GITHUB_MENTION" = "yes" ]; then
  echo -e "${GREEN}✓ AI is tracking GitHub connection${NC}"
else
  echo -e "${YELLOW}⚠ AI doesn't mention GitHub in response${NC}"
fi

if [ "$HAS_EXTENSION_MENTION" = "yes" ]; then
  echo -e "${GREEN}✓ AI is tracking browser extension${NC}"
else
  echo -e "${YELLOW}⚠ AI doesn't mention extension in response${NC}"
fi

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

echo -e "Project ID: ${PROJECT_ID}"
echo -e "Total Messages: ${MESSAGE_COUNT}"
echo -e "\nTo manually verify handoff data in Firebase Console:"
echo -e "  1. Go to Firestore"
echo -e "  2. Open projects/${PROJECT_ID}"
echo -e "  3. Check phaseData.phaseHandoffs.collector"
echo -e ""
echo -e "Or view in UI:"
echo -e "  ${BASE_URL}/default/project/${PROJECT_ID}/v_ai_chat"
echo -e "  (Look for the checklist in the left sidebar)\n"

