#!/bin/bash
# Captcha Vision Voting System
# Sends same image to multiple AI models and compares answers

IMAGE_PATH="${1:-/tmp/captcha-puzzle.png}"
PROMPT="${2:-Look at this captcha puzzle. On the left is a target icon. On the right is a shadow that can be rotated with arrows. Which direction should I click - LEFT arrow or RIGHT arrow - to make the shadow match the target icon? Answer with just LEFT or RIGHT.}"

if [ ! -f "$IMAGE_PATH" ]; then
    echo "❌ Image not found: $IMAGE_PATH"
    exit 1
fi

echo "🗳️  Captcha Vision Voting System"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📸 Image: $IMAGE_PATH"
echo ""

# Vote 1: Gemini
echo "🤖 Asking Gemini..."
GEMINI_ANSWER=$(gemini -p "$PROMPT" -f "$IMAGE_PATH" 2>/dev/null | tr '[:lower:]' '[:upper:]' | grep -oE '(LEFT|RIGHT)' | head -1)
echo "   Gemini says: ${GEMINI_ANSWER:-UNCLEAR}"

# Vote 2: Gemini again with slightly different prompt (for diversity)
echo "🤖 Asking Gemini (rephrased)..."
PROMPT2="This is a shadow-matching captcha. The left image is the target shape. The right image shows a shadow. Should I click the LEFT arrow or RIGHT arrow to rotate the shadow to match? Just say LEFT or RIGHT."
GEMINI2_ANSWER=$(gemini -p "$PROMPT2" -f "$IMAGE_PATH" 2>/dev/null | tr '[:lower:]' '[:upper:]' | grep -oE '(LEFT|RIGHT)' | head -1)
echo "   Gemini2 says: ${GEMINI2_ANSWER:-UNCLEAR}"

# Vote 3: Gemini with position analysis
echo "🤖 Asking Gemini (analytical)..."
PROMPT3="Analyze this captcha: 1) What object is shown on the left? 2) How is the shadow on the right currently oriented? 3) To match them, should I click LEFT or RIGHT arrow? End with just LEFT or RIGHT."
GEMINI3_ANSWER=$(gemini -p "$PROMPT3" -f "$IMAGE_PATH" 2>/dev/null | tr '[:lower:]' '[:upper:]' | grep -oE '(LEFT|RIGHT)' | tail -1)
echo "   Gemini3 says: ${GEMINI3_ANSWER:-UNCLEAR}"

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Tally votes
LEFT_VOTES=0
RIGHT_VOTES=0

[[ "$GEMINI_ANSWER" == "LEFT" ]] && ((LEFT_VOTES++))
[[ "$GEMINI_ANSWER" == "RIGHT" ]] && ((RIGHT_VOTES++))
[[ "$GEMINI2_ANSWER" == "LEFT" ]] && ((LEFT_VOTES++))
[[ "$GEMINI2_ANSWER" == "RIGHT" ]] && ((RIGHT_VOTES++))
[[ "$GEMINI3_ANSWER" == "LEFT" ]] && ((LEFT_VOTES++))
[[ "$GEMINI3_ANSWER" == "RIGHT" ]] && ((RIGHT_VOTES++))

echo "📊 VOTES: LEFT=$LEFT_VOTES | RIGHT=$RIGHT_VOTES"

if [ $LEFT_VOTES -gt $RIGHT_VOTES ]; then
    echo "✅ CONSENSUS: LEFT"
    echo "LEFT"
elif [ $RIGHT_VOTES -gt $LEFT_VOTES ]; then
    echo "✅ CONSENSUS: RIGHT"
    echo "RIGHT"
else
    echo "⚠️  TIE - needs Claude's tiebreaker"
    echo "TIE"
fi
