#!/bin/bash
# Reonomy Demo - Quick single contact extraction
# Usage: ./reonomy-demo.sh
# Shows: Login → Search → Property → Owner → Contact modal with phones/emails

set -e

echo "🎯 Reonomy Contact Extraction Demo"
echo "=================================="
echo ""

# Config
EMAIL="${REONOMY_EMAIL:-henry@realestateenhanced.com}"
PASSWORD="${REONOMY_PASSWORD:-9082166532}"
SEARCH_ID="${REONOMY_SEARCH_ID:-bacfd104-fed5-4cc4-aba1-933f899de3f8}"

# Random delay helper
delay() {
  local min=$1
  local max=$2
  local ms=$(( (RANDOM % (max - min + 1)) + min ))
  sleep $(echo "scale=2; $ms/1000" | bc)
}

echo "📍 Step 1: Opening Reonomy..."
agent-browser open "https://app.reonomy.com/#!/login" --headed
delay 2000 4000

echo "📍 Step 2: Logging in..."
agent-browser snapshot -i > /dev/null
agent-browser fill @e2 "$EMAIL"
delay 500 1000
agent-browser fill @e3 "$PASSWORD"
delay 500 1000
agent-browser click @e5
echo "   ⏳ Waiting for login..."
sleep 12

# Check login
URL=$(agent-browser eval "window.location.href" 2>/dev/null)
if [[ "$URL" == *"auth.reonomy"* ]]; then
  echo "   ❌ Login failed"
  exit 1
fi
echo "   ✅ Logged in!"

echo ""
echo "📍 Step 3: Loading search with filters..."
agent-browser open "https://app.reonomy.com/#!/search/$SEARCH_ID"
delay 6000 8000

echo "📍 Step 4: Clicking first property..."
# Get fresh snapshot and click first property-looking button
agent-browser snapshot -i > /tmp/reonomy-demo-snap.txt
# Find a property (address pattern)
PROP_REF=$(grep -oE 'button "[0-9]+ [^"]+" \[ref=e[0-9]+\]' /tmp/reonomy-demo-snap.txt | head -1 | grep -oE 'e[0-9]+')
if [ -z "$PROP_REF" ]; then
  # Try another pattern
  PROP_REF="e8"
fi
agent-browser click @$PROP_REF
delay 5000 7000

echo "📍 Step 5: Clicking Owner tab..."
agent-browser find role tab click --name "Owner"
delay 4000 6000

echo "📍 Step 6: Clicking View Contacts..."
agent-browser snapshot -i > /tmp/reonomy-demo-snap.txt
VC_REF=$(grep -oE 'button "View Contacts[^"]*" \[ref=e[0-9]+\]' /tmp/reonomy-demo-snap.txt | grep -oE 'e[0-9]+')
if [ -z "$VC_REF" ]; then
  echo "   ⚠️ No View Contacts button found"
  agent-browser screenshot /tmp/reonomy-demo-nocontacts.png
  echo "   Screenshot: /tmp/reonomy-demo-nocontacts.png"
  exit 1
fi
agent-browser click @$VC_REF
delay 3000 5000

echo "📍 Step 7: Finding contact person..."
agent-browser snapshot > /tmp/reonomy-demo-snap.txt
# Look for person link
PERSON_URL=$(grep -oE '/!/person/[a-f0-9-]+' /tmp/reonomy-demo-snap.txt | head -1)
if [ -z "$PERSON_URL" ]; then
  echo "   ⚠️ No person link found, checking for direct contacts..."
  agent-browser snapshot -i
  exit 0
fi

echo "   Found: $PERSON_URL"
agent-browser open "https://app.reonomy.com$PERSON_URL"
delay 5000 7000

echo "📍 Step 8: Opening contact modal..."
agent-browser snapshot -i > /tmp/reonomy-demo-snap.txt
CONTACT_REF=$(grep -oE 'button "Contact" \[ref=e[0-9]+\]' /tmp/reonomy-demo-snap.txt | grep -oE 'e[0-9]+')
if [ -z "$CONTACT_REF" ]; then
  echo "   ⚠️ No Contact button found"
  exit 1
fi
agent-browser click @$CONTACT_REF
delay 2000 3000

echo ""
echo "🎉 CONTACT INFO EXTRACTED!"
echo "=========================="
agent-browser snapshot -i | grep -E '(button "[0-9]{3}-[0-9]{3}-[0-9]{4}|button "[a-zA-Z0-9._%+-]+@)' | while read line; do
  if [[ "$line" == *"@"* ]]; then
    EMAIL=$(echo "$line" | grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}')
    echo "   📧 $EMAIL"
  else
    PHONE=$(echo "$line" | grep -oE '[0-9]{3}-[0-9]{3}-[0-9]{4}')
    echo "   📞 $PHONE"
  fi
done

echo ""
echo "📸 Taking screenshot..."
agent-browser screenshot /tmp/reonomy-demo-result.png
echo "   Saved: /tmp/reonomy-demo-result.png"

echo ""
echo "✅ Demo complete! Browser left open for inspection."
echo "   Run 'agent-browser close' when done."
