#!/bin/bash

# WordLift CLI Mode Comparison Test
# This script tests the differences between interactive and -p modes

echo "🧪 WordLift CLI Mode Comparison Test"
echo "====================================="

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

echo -e "${YELLOW}Testing WordLift CLI functionality in different modes...${NC}\n"

# Test 1: Check if WordLift context is loaded
echo "📋 Test 1: Context Loading"
echo "-------------------------"

echo "Testing -p mode context loading..."
wordlift-cli -p "What is your role? Answer in one sentence." > /tmp/test-p-mode.txt 2>&1 &
P_PID=$!

echo "Waiting for -p mode test to complete..."
wait $P_PID

if grep -q "WordLift" /tmp/test-p-mode.txt; then
    echo -e "${GREEN}✅ -p mode: WordLift context appears to be loaded${NC}"
else
    echo -e "${RED}❌ -p mode: WordLift context may not be loaded${NC}"
fi

echo "Response from -p mode:"
cat /tmp/test-p-mode.txt
echo -e "\n"

# Test 2: Check MCP server access
echo "📋 Test 2: MCP Server Access"
echo "----------------------------"

echo "Testing MCP server access in -p mode..."
wordlift-cli -p "Can you access the WordLift knowledge graph? Try to search for 'SEO' using MCP." > /tmp/test-mcp.txt 2>&1 &
MCP_PID=$!

echo "Waiting for MCP test to complete..."
wait $MCP_PID

if grep -q "mcp" /tmp/test-mcp.txt || grep -q "WordLift" /tmp/test-mcp.txt; then
    echo -e "${GREEN}✅ MCP server access appears to work${NC}"
else
    echo -e "${RED}❌ MCP server access may not be working${NC}"
fi

echo "MCP test response:"
cat /tmp/test-mcp.txt
echo -e "\n"

# Test 3: Check YOLO mode (auto-accept)
echo "📋 Test 3: YOLO Mode (Auto-accept)"
echo "----------------------------------"

echo "Testing YOLO mode in -p mode..."
wordlift-cli -p "Create a small test file called 'test-yolo.txt' with the content 'YOLO test successful'" > /tmp/test-yolo.txt 2>&1 &
YOLO_PID=$!

echo "Waiting for YOLO test to complete..."
wait $YOLO_PID

if [ -f "test-yolo.txt" ]; then
    echo -e "${GREEN}✅ YOLO mode: File creation auto-accepted${NC}"
    echo "File content: $(cat test-yolo.txt)"
    rm -f test-yolo.txt
else
    echo -e "${RED}❌ YOLO mode: File creation may not be working${NC}"
fi

echo "YOLO test response:"
cat /tmp/test-yolo.txt
echo -e "\n"

# Test 4: Compare SEO expertise
echo "📋 Test 4: SEO Expertise Comparison"
echo "-----------------------------------"

echo "Testing SEO expertise in -p mode..."
wordlift-cli -p "What are the key elements of semantic SEO? List 3 main points." > /tmp/test-seo.txt 2>&1 &
SEO_PID=$!

echo "Waiting for SEO test to complete..."
wait $SEO_PID

if grep -q -i "semantic\|schema\|entity\|knowledge graph\|structured data" /tmp/test-seo.txt; then
    echo -e "${GREEN}✅ SEO expertise: Advanced SEO concepts detected${NC}"
else
    echo -e "${YELLOW}⚠️  SEO expertise: May be using generic knowledge${NC}"
fi

echo "SEO expertise response:"
cat /tmp/test-seo.txt
echo -e "\n"

# Test 5: Check environment variables
echo "📋 Test 5: Environment Variables"
echo "--------------------------------"

echo "Checking if WordLift environment variables are set..."
if [ -n "$WORDLIFT_PROJECT_DIR" ]; then
    echo -e "${GREEN}✅ WORDLIFT_PROJECT_DIR: $WORDLIFT_PROJECT_DIR${NC}"
else
    echo -e "${RED}❌ WORDLIFT_PROJECT_DIR not set${NC}"
fi

if [ -n "$WORDLIFT_PACKAGE_DIR" ]; then
    echo -e "${GREEN}✅ WORDLIFT_PACKAGE_DIR: $WORDLIFT_PACKAGE_DIR${NC}"
else
    echo -e "${RED}❌ WORDLIFT_PACKAGE_DIR not set${NC}"
fi

if [ -n "$GEMINI_CONFIG_DIR" ]; then
    echo -e "${GREEN}✅ GEMINI_CONFIG_DIR: $GEMINI_CONFIG_DIR${NC}"
else
    echo -e "${RED}❌ GEMINI_CONFIG_DIR not set${NC}"
fi

# Test 6: Check context files
echo -e "\n📋 Test 6: Context Files"
echo "------------------------"

if [ -f "WORDLIFT.md" ]; then
    echo -e "${GREEN}✅ WORDLIFT.md found in current directory${NC}"
    echo "File size: $(wc -c < WORDLIFT.md) bytes"
else
    echo -e "${RED}❌ WORDLIFT.md not found in current directory${NC}"
fi

if [ -f ".gemini/settings.json" ]; then
    echo -e "${GREEN}✅ .gemini/settings.json found${NC}"
    echo "WordLift MCP server configured: $(grep -q 'wordlift' .gemini/settings.json && echo 'Yes' || echo 'No')"
else
    echo -e "${RED}❌ .gemini/settings.json not found${NC}"
fi

echo -e "\n🎯 Summary"
echo "=========="
echo "Run this test after installing WordLift CLI to verify:"
echo "1. Both modes have access to WordLift context"
echo "2. MCP server integration works in -p mode"
echo "3. YOLO mode auto-accepts actions"
echo "4. SEO expertise is consistent between modes"
echo "5. Environment variables are properly set"
echo "6. Context files are correctly deployed"

echo -e "\n${YELLOW}💡 Tip: For a more detailed comparison, manually test:${NC}"
echo "   Interactive: wordlift-cli"
echo "   Prompt mode: wordlift-cli -p 'Your question here'"
echo "   Both should show similar expertise and capabilities"

# Cleanup
rm -f /tmp/test-*.txt

echo -e "\n✅ Test complete!"
