#!/bin/bash

# End-to-End Collector Flow Test
# Simulates a real user journey from welcome → document upload → GitHub → extension → handoff

# set -e  # Don't exit on error - show all test results

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

# Configuration
BASE_URL="http://localhost:3000"
API_BASE="$BASE_URL/api"

# You MUST set these from a real logged-in session
echo ""
echo "=========================================="
echo "   E2E COLLECTOR FLOW TEST"
echo "=========================================="
echo ""
echo -e "${YELLOW}SETUP REQUIRED:${NC}"
echo "1. Log into http://localhost:3000"
echo "2. Open DevTools → Network tab"
echo "3. Send a test message in AI Chat"
echo "4. Copy the 'Authorization: Bearer XXX' header"
echo "5. Create a project and copy the projectId"
echo ""
echo -e "${CYAN}Then run:${NC}"
echo "  export AUTH_TOKEN='your-token-here'"
echo "  export PROJECT_ID='your-project-id-here'"
echo "  ./test-e2e-collector.sh"
echo ""

if [ -z "$AUTH_TOKEN" ] || [ -z "$PROJECT_ID" ]; then
    echo -e "${RED}ERROR: AUTH_TOKEN and PROJECT_ID must be set${NC}"
    echo ""
    exit 1
fi

# Test results
TESTS_PASSED=0
TESTS_FAILED=0
FAILED_TESTS=()

# Helper functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[PASS]${NC} $1"
    ((TESTS_PASSED++))
}

log_error() {
    echo -e "${RED}[FAIL]${NC} $1"
    ((TESTS_FAILED++))
    FAILED_TESTS+=("$1")
}

log_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_response() {
    echo -e "${CYAN}[RESPONSE]${NC} $1"
}

# Send a chat message and check response
send_chat_message() {
    local message="$1"
    local expected_keywords="$2"
    
    log_info "Sending: \"$message\""
    
    response=$(curl -s -X POST "$API_BASE/ai/chat" \
        -H "Authorization: Bearer $AUTH_TOKEN" \
        -H "Content-Type: application/json" \
        -d "{\"projectId\":\"$PROJECT_ID\",\"message\":\"$message\"}")
    
    http_code=$?
    
    if [ $http_code -ne 0 ]; then
        log_error "Failed to send message: $message"
        return 1
    fi
    
    # Check for error in response
    if echo "$response" | jq -e '.error' > /dev/null 2>&1; then
        error_msg=$(echo "$response" | jq -r '.error')
        log_error "API Error: $error_msg"
        echo "$response" | jq '.' 2>/dev/null || echo "$response"
        return 1
    fi
    
    # Extract reply
    reply=$(echo "$response" | jq -r '.reply' 2>/dev/null)
    
    if [ -z "$reply" ] || [ "$reply" = "null" ]; then
        log_error "No reply received"
        echo "$response" | jq '.' 2>/dev/null || echo "$response"
        return 1
    fi
    
    log_response "$(echo "$reply" | head -c 200)..."
    
    # Check for expected keywords
    if [ -n "$expected_keywords" ]; then
        all_found=true
        IFS='|' read -ra KEYWORDS <<< "$expected_keywords"
        for keyword in "${KEYWORDS[@]}"; do
            if echo "$reply" | grep -qi "$keyword"; then
                log_success "Response contains: '$keyword'"
            else
                log_error "Response missing expected keyword: '$keyword'"
                all_found=false
            fi
        done
        
        if $all_found; then
            return 0
        else
            return 1
        fi
    fi
    
    return 0
}

# Check collector handoff state
check_handoff_state() {
    local expected_docs="$1"
    local expected_github="$2"
    local expected_extension="$3"
    
    log_info "Checking collector handoff state..."
    
    # This would require Firestore access or an API endpoint
    # For now, we'll just log what we expect
    log_info "Expected state:"
    echo "  - hasDocuments: $expected_docs"
    echo "  - githubConnected: $expected_github"
    echo "  - extensionLinked: $expected_extension"
    
    # TODO: Add actual Firestore check or API endpoint
    log_warning "Handoff state check not implemented (would need Firestore access)"
}

# Simulate document upload
simulate_document_upload() {
    local filename="$1"
    
    log_info "Simulating upload: $filename"
    
    # Create a temporary test file
    temp_file=$(mktemp)
    echo "This is a test document for QA purposes.

Project Overview:
- We're building a SaaS platform
- Target users: Small businesses
- Key features: User management, billing, analytics

Technical Stack:
- Frontend: React, Next.js
- Backend: Node.js, PostgreSQL
- Infrastructure: AWS

This is test content for $filename" > "$temp_file"
    
    # Upload via API
    response=$(curl -s -X POST "$API_BASE/projects/$PROJECT_ID/knowledge/upload-document" \
        -H "Authorization: Bearer $AUTH_TOKEN" \
        -F "file=@$temp_file;filename=$filename")
    
    rm "$temp_file"
    
    if echo "$response" | jq -e '.success' > /dev/null 2>&1; then
        knowledge_id=$(echo "$response" | jq -r '.knowledgeItemId')
        log_success "Uploaded: $filename (ID: $knowledge_id)"
        return 0
    else
        error_msg=$(echo "$response" | jq -r '.error' 2>/dev/null || echo "Unknown error")
        log_error "Upload failed for $filename: $error_msg"
        return 1
    fi
}

# Main test flow
main() {
    echo ""
    echo "=========================================="
    echo "   RUNNING E2E COLLECTOR TESTS"
    echo "=========================================="
    echo ""
    echo "Project ID: $PROJECT_ID"
    echo ""
    
    # Step 1: Initial greeting (auto-sent "Hello")
    log_info "=== STEP 1: Welcome Message ==="
    send_chat_message "Hello" "Welcome|Step 1|Step 2|Step 3|documents|GitHub|extension"
    sleep 2
    
    # Step 2: Upload documents
    log_info ""
    log_info "=== STEP 2: Upload Documents ==="
    
    docs=(
        "project-overview.md"
        "user-stories.md"
        "technical-requirements.md"
        "api-specification.md"
        "database-schema.md"
        "ui-mockups.md"
        "business-requirements.md"
        "deployment-plan.md"
    )
    
    for doc in "${docs[@]}"; do
        simulate_document_upload "$doc"
        sleep 1
    done
    
    # Step 3: Tell AI about documents
    log_info ""
    log_info "=== STEP 3: Inform AI About Documents ==="
    send_chat_message "I just uploaded 8 documents about my project" "uploaded|document"
    sleep 2
    
    # Step 4: Connect GitHub
    log_info ""
    log_info "=== STEP 4: GitHub Connection ==="
    send_chat_message "Yes, I have a GitHub repo. It's called myuser/my-saas-app" "GitHub|repo|connected"
    sleep 2
    
    # Note: Actual GitHub connection requires OAuth flow
    log_warning "GitHub OAuth flow requires manual browser interaction"
    log_info "In real testing, user would click 'Connect GitHub' button"
    
    # Step 5: Extension
    log_info ""
    log_info "=== STEP 5: Extension Installation ==="
    send_chat_message "I want to install the browser extension" "extension|install|capture"
    sleep 2
    
    # Step 6: Confirm ready
    log_info ""
    log_info "=== STEP 6: Confirm Everything ==="
    send_chat_message "Yes, that's everything I have for now" "everything|analyze|dig"
    sleep 2
    
    # Step 7: Check for auto-transition
    log_info ""
    log_info "=== STEP 7: Verify Auto-Transition ==="
    send_chat_message "What do you need from me?" "extraction|review|important|V1"
    sleep 2
    
    # Step 8: Check handoff state
    log_info ""
    log_info "=== STEP 8: Verify Handoff State ==="
    check_handoff_state "true" "true" "false"
    
    # Results
    echo ""
    echo "=========================================="
    echo "              TEST RESULTS"
    echo "=========================================="
    echo -e "${GREEN}Passed:${NC} $TESTS_PASSED"
    echo -e "${RED}Failed:${NC} $TESTS_FAILED"
    echo ""
    
    if [ $TESTS_FAILED -gt 0 ]; then
        echo -e "${RED}Failed checks:${NC}"
        for test in "${FAILED_TESTS[@]}"; do
            echo "  - $test"
        done
        echo ""
        exit 1
    else
        echo -e "${GREEN}✅ E2E COLLECTOR FLOW COMPLETE!${NC}"
        echo ""
        echo "Next steps:"
        echo "1. Open http://localhost:3000 in browser"
        echo "2. Navigate to the project"
        echo "3. Check AI Chat page - verify checklist shows:"
        echo "   ✅ Documents uploaded (8)"
        echo "   ✅ GitHub connected"
        echo "   ⭕ Extension linked"
        echo "4. Verify mode switched to 'Extraction Review'"
        echo ""
        exit 0
    fi
}

# Run tests
cd "$(dirname "$0")"
main

