#!/bin/bash

# Real Log Injector - Called by collect-multiple-pages.sh
# This script injects actual browser logs into placeholder files

set -e

if [ $# -lt 1 ]; then
    echo "Usage: $0 <log-directory>"
    exit 1
fi

LOG_DIR="$1"

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

echo -e "${BLUE}🤖 Injecting real browser logs via Claude Code MCP tools${NC}"

# Find all log files with injection markers
injection_files=($(grep -l "CLAUDE_MCP_INJECT_START" "$LOG_DIR"/*.log 2>/dev/null || true))

if [ ${#injection_files[@]} -eq 0 ]; then
    echo -e "${YELLOW}⚠️  No files found with injection markers${NC}"
    exit 0
fi

echo -e "${BLUE}📋 Found ${#injection_files[@]} files ready for injection${NC}"

# This function would be called by Claude Code to inject real MCP data
# For now, it creates a signal that Claude Code can detect and replace
inject_mcp_data() {
    local log_file="$1"
    local page_name=$(basename "$log_file" .log)

    echo -e "${BLUE}   🔍 Processing $(basename "$log_file")...${NC}"

    # Create a more specific injection request
    local temp_file=$(mktemp)

    # Replace the injection marker with a request for real data
    cat "$log_file" | sed '
    /CLAUDE_MCP_INJECT_START/,/CLAUDE_MCP_INJECT_END/{
        /CLAUDE_MCP_INJECT_START/c\
=== REAL BROWSER LOGS (Auto-collected via MCP) ===\
\
# CLAUDE CODE: Please replace this section with actual MCP tool results:\
# 1. Run: mcp__browser-tools__getConsoleLogs\
# 2. Run: mcp__browser-tools__getConsoleErrors\
# 3. Run: mcp__browser-tools__getNetworkErrors\
# 4. Format results as shown in existing examples\
\
=== CONSOLE LOGS ===\
[Waiting for Claude Code MCP injection...]\
\
=== CONSOLE ERRORS ===\
[Waiting for Claude Code MCP injection...]\
\
=== NETWORK ERRORS ===\
[Waiting for Claude Code MCP injection...]
        /CLAUDE_MCP_INJECT_END/d
    }
    ' > "$temp_file"

    mv "$temp_file" "$log_file"
    echo -e "${GREEN}   ✅ Injection request created for $(basename "$log_file")${NC}"
}

# Process each file
for log_file in "${injection_files[@]}"; do
    inject_mcp_data "$log_file"
done

echo -e "${GREEN}✅ Injection requests created for ${#injection_files[@]} files${NC}"
echo -e "${YELLOW}💡 Claude Code will now replace these requests with real MCP data${NC}"

# Auto-process if system MCP processor is available
system_processor="$(dirname "${BASH_SOURCE[0]}")/system-mcp-processor.sh"
if [ -f "$system_processor" ]; then
    echo -e "${BLUE}🤖 Attempting automatic system MCP processing...${NC}"

    # Extract pages from log files
    pages=()
    for log_file in "$LOG_DIR"/*.log; do
        if [ -f "$log_file" ]; then
            basename=$(basename "$log_file" .log)
            if [ "$basename" = "root" ]; then
                pages+=("/")
            else
                pages+=("/$basename")
            fi
        fi
    done

    if [ ${#pages[@]} -gt 0 ]; then
        echo -e "${BLUE}📋 Auto-processing pages: ${pages[*]}${NC}"
        "$system_processor" "$LOG_DIR" "${pages[@]}" || echo -e "${YELLOW}⚠️  Auto-processing failed, manual MCP injection needed${NC}"
    fi
fi
