#!/bin/bash

# Helper script to set up and run E2E test

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

echo ""
echo "=========================================="
echo "   E2E TEST SETUP HELPER"
echo "=========================================="
echo ""

echo -e "${BLUE}Step 1: Check if server is running...${NC}"
if curl -s -o /dev/null -w "%{http_code}" http://localhost:3000 | grep -q "200\|404"; then
    echo -e "${GREEN}✓ Server is running on http://localhost:3000${NC}"
else
    echo -e "${YELLOW}⚠ Server not running. Starting it now...${NC}"
    npm run dev &
    sleep 5
fi

echo ""
echo -e "${BLUE}Step 2: Get your credentials${NC}"
echo ""
echo -e "${CYAN}Please follow these steps:${NC}"
echo ""
echo "1. Open your browser and go to:"
echo "   ${BLUE}http://localhost:3000${NC}"
echo ""
echo "2. Sign in and create/open a project"
echo ""
echo "3. Go to AI Chat page"
echo ""
echo "4. Open DevTools (F12 or Cmd+Option+I)"
echo ""
echo "5. Go to Network tab"
echo ""
echo "6. Send any message (e.g., 'test')"
echo ""
echo "7. Find the request to '/api/ai/chat'"
echo ""
echo "8. Click it → Headers tab"
echo ""
echo "9. Copy the 'Authorization' header value"
echo "   (It looks like: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6Ij...)"
echo ""
echo "10. Copy your project ID from the URL"
echo "    (Format: /workspace/project/{PROJECT_ID}/v_ai_chat)"
echo ""
echo ""
echo -e "${YELLOW}Ready to input your credentials?${NC}"
read -p "Press Enter when ready..."
echo ""

# Get auth token
echo -e "${CYAN}Paste your Authorization header (including 'Bearer '):${NC}"
read -r AUTH_TOKEN
echo ""

# Get project ID
echo -e "${CYAN}Paste your Project ID:${NC}"
read -r PROJECT_ID
echo ""

# Validate inputs
if [ -z "$AUTH_TOKEN" ]; then
    echo -e "${YELLOW}⚠ Auth token is empty. Exiting.${NC}"
    exit 1
fi

if [ -z "$PROJECT_ID" ]; then
    echo -e "${YELLOW}⚠ Project ID is empty. Exiting.${NC}"
    exit 1
fi

# Save to temporary file for reuse
cat > .e2e-test-env << EOF
export AUTH_TOKEN='$AUTH_TOKEN'
export PROJECT_ID='$PROJECT_ID'
EOF

echo -e "${GREEN}✓ Credentials saved to .e2e-test-env${NC}"
echo ""

# Test connection
echo -e "${BLUE}Step 3: Testing connection...${NC}"
response=$(curl -s -X POST "http://localhost:3000/api/ai/chat" \
    -H "Authorization: $AUTH_TOKEN" \
    -H "Content-Type: application/json" \
    -d "{\"projectId\":\"$PROJECT_ID\",\"message\":\"test\"}")

if echo "$response" | jq -e '.reply' > /dev/null 2>&1; then
    echo -e "${GREEN}✓ Connection successful!${NC}"
    reply=$(echo "$response" | jq -r '.reply' | head -c 100)
    echo -e "${CYAN}AI Response:${NC} $reply..."
    echo ""
elif echo "$response" | jq -e '.error' > /dev/null 2>&1; then
    error=$(echo "$response" | jq -r '.error')
    echo -e "${YELLOW}⚠ API Error: $error${NC}"
    echo "Please check your credentials and try again."
    exit 1
else
    echo -e "${YELLOW}⚠ Unexpected response${NC}"
    echo "$response"
    exit 1
fi

echo ""
echo -e "${GREEN}=========================================="
echo "   READY TO RUN E2E TEST"
echo "==========================================${NC}"
echo ""
echo "Credentials saved! You can now run:"
echo ""
echo -e "${CYAN}  source .e2e-test-env${NC}"
echo -e "${CYAN}  ./test-e2e-collector.sh${NC}"
echo ""
echo "Or simply:"
echo ""
echo -e "${CYAN}  AUTH_TOKEN='$AUTH_TOKEN' PROJECT_ID='$PROJECT_ID' ./test-e2e-collector.sh${NC}"
echo ""

# Offer to run now
echo -e "${YELLOW}Run E2E test now? (y/n)${NC}"
read -r run_now

if [ "$run_now" = "y" ] || [ "$run_now" = "Y" ]; then
    echo ""
    export AUTH_TOKEN
    export PROJECT_ID
    ./test-e2e-collector.sh
fi

