#!/bin/bash

# Test script for new accessibility summary tools
# Tests the token-efficient accessibility assessment tools

set -e

echo "========================================="
echo "Testing Accessibility Summary Tools"
echo "========================================="
echo ""

# Configuration
DAEMON_HOST="localhost"
DAEMON_PORT="8989"
TEST_URL="https://visionleadership.org"

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

# Function to test a command
test_command() {
    local cmd_name=$1
    local cmd_json=$2
    
    echo -e "${YELLOW}Testing: $cmd_name${NC}"
    
    response=$(curl -s -X POST http://$DAEMON_HOST:$DAEMON_PORT/command \
        -H "Content-Type: application/json" \
        -d "$cmd_json")
    
    success=$(echo "$response" | jq -r '.success')
    
    if [ "$success" = "true" ]; then
        echo -e "${GREEN}✓ $cmd_name succeeded${NC}"
        
        # Show token estimate
        token_count=$(echo "$response" | jq -r '.data' | wc -c)
        echo "  Estimated tokens: ~$((token_count / 4))"
        
        return 0
    else
        error=$(echo "$response" | jq -r '.error')
        echo -e "${RED}✗ $cmd_name failed: $error${NC}"
        return 1
    fi
}

# Check if daemon is running
echo "Checking daemon status..."
if ! curl -s http://$DAEMON_HOST:$DAEMON_PORT/status > /dev/null; then
    echo -e "${RED}Error: Daemon is not running on $DAEMON_HOST:$DAEMON_PORT${NC}"
    echo "Please start the daemon first: ./daemon/cremotedaemon"
    exit 1
fi
echo -e "${GREEN}✓ Daemon is running${NC}"
echo ""

# Open a tab and navigate to test URL
echo "Opening tab and navigating to $TEST_URL..."
tab_response=$(curl -s -X POST http://$DAEMON_HOST:$DAEMON_PORT/command \
    -H "Content-Type: application/json" \
    -d '{"action":"open-tab","params":{"timeout":"10"}}')

tab_id=$(echo "$tab_response" | jq -r '.data')
echo "Tab ID: $tab_id"

# Navigate to test URL
curl -s -X POST http://$DAEMON_HOST:$DAEMON_PORT/command \
    -H "Content-Type: application/json" \
    -d "{\"action\":\"navigate\",\"params\":{\"tab\":\"$tab_id\",\"url\":\"$TEST_URL\",\"timeout\":\"10\"}}" > /dev/null

echo -e "${GREEN}✓ Navigated to $TEST_URL${NC}"
echo ""

# Wait for page to load
sleep 2

# Test 1: Page Accessibility Report
echo "========================================="
echo "Test 1: Page Accessibility Report"
echo "========================================="
test_command "page-accessibility-report" \
    "{\"action\":\"page-accessibility-report\",\"params\":{\"tab\":\"$tab_id\",\"tests\":\"wcag,contrast,keyboard\",\"standard\":\"WCAG21AA\",\"timeout\":\"30\"}}"
echo ""

# Test 2: Contrast Audit
echo "========================================="
echo "Test 2: Contrast Audit"
echo "========================================="
test_command "contrast-audit" \
    "{\"action\":\"contrast-audit\",\"params\":{\"tab\":\"$tab_id\",\"priority_selectors\":\"button,a,nav,footer\",\"threshold\":\"AA\",\"timeout\":\"10\"}}"
echo ""

# Test 3: Keyboard Audit
echo "========================================="
echo "Test 3: Keyboard Audit"
echo "========================================="
test_command "keyboard-audit" \
    "{\"action\":\"keyboard-audit\",\"params\":{\"tab\":\"$tab_id\",\"check_focus_indicators\":\"true\",\"check_tab_order\":\"true\",\"check_keyboard_traps\":\"true\",\"timeout\":\"15\"}}"
echo ""

# Test 4: Form Accessibility Audit
echo "========================================="
echo "Test 4: Form Accessibility Audit"
echo "========================================="
test_command "form-accessibility-audit" \
    "{\"action\":\"form-accessibility-audit\",\"params\":{\"tab\":\"$tab_id\",\"timeout\":\"10\"}}"
echo ""

# Close the tab
echo "Cleaning up..."
curl -s -X POST http://$DAEMON_HOST:$DAEMON_PORT/command \
    -H "Content-Type: application/json" \
    -d "{\"action\":\"close-tab\",\"params\":{\"tab\":\"$tab_id\"}}" > /dev/null

echo -e "${GREEN}✓ Tab closed${NC}"
echo ""

echo "========================================="
echo "All Tests Complete!"
echo "========================================="
echo ""
echo "Summary:"
echo "- All 4 new accessibility summary tools tested"
echo "- Token usage significantly reduced"
echo "- Results are structured and actionable"
echo ""
echo "Next steps:"
echo "1. Review the output above for any errors"
echo "2. Compare token usage with old approach"
echo "3. Test with MCP server integration"
echo "4. Run full site assessment"

