#!/bin/bash
# Manual Test Script for Anchor Visibility
# Run this script and follow the on-screen instructions

set -e

echo "======================================"
echo "Anchor Visibility Manual Test"
echo "======================================"
echo ""

# Check if server is running
if ! curl -s http://localhost:8081 > /dev/null; then
    echo "❌ ERROR: Site builder not running on http://localhost:8081"
    echo "Start it with: cd /home/jknapp/code/site-builder && docker compose up -d"
    exit 1
fi

echo "✅ Server is running on http://localhost:8081"
echo ""
echo "STEP 1: Test Editor Visibility"
echo "------------------------------"
echo "1. Open http://localhost:8081 in your browser"
echo "2. Find 'Anchor Point' block in the left panel"
echo "3. Click it to add to the canvas"
echo "4. You should see a box with:"
echo "   - Dashed gray border"
echo "   - Light blue background"
echo "   - ⚓ anchor icon"
echo "   - Editable text field: 'anchor-1'"
echo ""
echo "5. Click on the text field and change the name"
echo "6. Verify the anchor's ID updates automatically"
echo ""
read -p "Press Enter when you've verified editor visibility..."

echo ""
echo "STEP 2: Test Preview Mode"
echo "------------------------"
echo "1. Click the 'Preview' button in the top toolbar"
echo "2. A new window/tab should open"
echo "3. The anchor point should be INVISIBLE"
echo "4. (Optional) Open DevTools and verify:"
echo "   - Element exists in DOM: <div data-anchor=\"true\" class=\"editor-anchor\">"
echo "   - But has style: display: none"
echo ""
read -p "Press Enter when you've verified preview mode..."

echo ""
echo "STEP 3: Test Export"
echo "------------------"
echo "We'll export to ZIP and check the HTML"
echo ""

# Create temp directory
TEMP_DIR=$(mktemp -d)
echo "Using temp directory: $TEMP_DIR"

echo "1. Click 'Export' button in the editor"
echo "2. Click 'Export ZIP'"
echo "3. Save the file to: $TEMP_DIR/site.zip"
echo ""
read -p "Press Enter when you've saved the ZIP file..."

if [ ! -f "$TEMP_DIR/site.zip" ]; then
    echo "⚠️  ZIP file not found at $TEMP_DIR/site.zip"
    echo "Skipping export verification..."
else
    echo "✅ ZIP file found"
    
    # Extract and check
    cd "$TEMP_DIR"
    unzip -q site.zip
    
    echo ""
    echo "Checking index.html for anchor elements..."
    
    if grep -q "data-anchor=\"true\"" index.html; then
        echo "❌ FAIL: Found data-anchor=\"true\" in exported HTML"
        exit 1
    fi
    
    if grep -q "editor-anchor" index.html; then
        echo "❌ FAIL: Found 'editor-anchor' in exported HTML"
        exit 1
    fi
    
    echo "✅ PASS: No anchor elements found in exported HTML"
    
    # Cleanup
    cd -
    rm -rf "$TEMP_DIR"
fi

echo ""
echo "======================================"
echo "✅ All manual tests passed!"
echo "======================================"
echo ""
echo "Summary:"
echo "  • Anchors visible in editor (with ⚓ icon)"
echo "  • Anchors hidden in preview mode"
echo "  • Anchors completely removed from export"
echo ""
