#!/bin/bash
# Daily SEO Report - 8 AM UTC
# Enhanced with GA4 Engagement Metrics

WORKSPACE="/Users/claw/.openclaw/workspace/agents/marketing-seo"
LOG="$WORKSPACE/logs"
cd $WORKSPACE

# Get GA4 data
echo "📊 Fetching GA4 Analytics..."
GA_OUTPUT=$(python3 scripts/ga4-direct.py 2>/dev/null)

# Parse GA4 metrics
SESSIONS=$(echo "$GA_OUTPUT" | grep "Sessions:" | grep -o "[0-9,]*" | tr -d "," | head -1)
USERS=$(echo "$GA_OUTPUT" | grep "Active Users:" | grep -o "[0-9,]*" | tr -d "," | head -1)
ENGAGEMENT=$(echo "$GA_OUTPUT" | grep "Engagement Rate:" | grep -o "[0-9.]*" | head -1)
BOUNCE=$(echo "$GA_OUTPUT" | grep "Bounce Rate:" | grep -o "[0-9.]*" | head -1)
AVG_SESSION=$(echo "$GA_OUTPUT" | grep "Avg Session:" | grep -o "[0-9.]*" | head -1)
PAGE_VIEWS=$(echo "$GA_OUTPUT" | grep "Page Views:" | grep -o "[0-9,]*" | tr -d "," | head -1)
SCROLL_25=$(echo "$GA_OUTPUT" | grep "Scroll Depth 25%:" | grep -o "[0-9]*" | head -1)
SCROLL_50=$(echo "$GA_OUTPUT" | grep "Scroll Depth 50%:" | grep -o "[0-9]*" | head -1)
SCROLL_75=$(echo "$GA_OUTPUT" | grep "Scroll Depth 75%:" | grep -o "[0-9]*" | head -1)
FORM_INT=$(echo "$GA_OUTPUT" | grep "Form Interactions:" | grep -o "[0-9]*" | head -1)
CLICKS=$(echo "$GA_OUTPUT" | grep "Outbound Clicks:" | grep -o "[0-9]*" | head -1)

# Set defaults if empty
SESSIONS=${SESSIONS:-"0"}
USERS=${USERS:-"0"}
ENGAGEMENT=${ENGAGEMENT:-"0"}
BOUNCE=${BOUNCE:-"0"}
AVG_SESSION=${AVG_SESSION:-"0"}
PAGE_VIEWS=${PAGE_VIEWS:-"0"}
SCROLL_25=${SCROLL_25:-"0"}
SCROLL_50=${SCROLL_50:-"0"}
SCROLL_75=${SCROLL_75:-"0"}
FORM_INT=${FORM_INT:-"0"}
CLICKS=${CLICKS:-"0"}

# Get site status
WWW_UP=$(curl -s -o /dev/null -w "%{http_code}" https://www.hoaledgeriq.com -m 10)
APP_UP=$(curl -s -o /dev/null -w "%{http_code}" https://app.hoaledgeriq.com -m 10)

# Format status icons
WWW_ICON="✅"
APP_ICON="✅"
if [ "$WWW_UP" != "200" ]; then WWW_ICON="❌"; fi
if [ "$APP_UP" != "200" ]; then APP_ICON="❌"; fi

# Get rankings status
RANK_FILE="$WORKSPACE/state/rank-data.json"
if [ -f "$RANK_FILE" ]; then
    KEYWORDS=$(cat "$RANK_FILE" | grep -o '"keywords":\[[^]]*\]' | grep -o '[0-9]*' | head -1)
    RANK_STATUS="Tracking $KEYWORDS keywords"
else
    RANK_STATUS="Baseline monitoring active"
fi

# Build enhanced message
MSG="📊 *DAILY SEO REPORT* - $(date '+%a %b %d')

🌐 *Sites:*
${WWW_ICON} www.hoaledgeriq.com: ${WWW_UP}
${APP_ICON} app.hoaledgeriq.com: ${APP_UP}

📈 *Traffic (24h):*
• Sessions: ${SESSIONS}
• Users: ${USERS}
• Page Views: ${PAGE_VIEWS}
• Engagement: ${ENGAGEMENT}%
• Bounce Rate: ${BOUNCE}%
• Avg Session: ${AVG_SESSION}s

🎯 *Engagement Events:*
• Scroll 25%: ${SCROLL_25}
• Scroll 50%: ${SCROLL_50}
• Scroll 75%: ${SCROLL_75}
• Form Interactions: ${FORM_INT}
• Outbound Clicks: ${CLICKS}

📈 *Rankings:*
• ${RANK_STATUS}
• Monitoring for breakthrough

⚡ Status: Healthy ✅

_GA4 Analytics + Engagement Tracking_"

# Send via Telegram
openclaw message send --channel telegram --target telegram:8269921691 --message "$MSG" 2>/dev/null || echo "$MSG" >> "$LOG/daily-$(date +%Y%m%d).log"

# Log success
echo "Report sent: $(date)" >> "$LOG/report-sent.log"
echo "Enhanced daily report completed at $(date)"
