#!/bin/bash
# Morning Business Summary - 9 AM Daily Report

YESTERDAY=$(date -v-1d '+%Y-%m-%d')
YESTERDAY_DASH=$(date -v-1d '+%Y%m%d')

echo "📊 MORNING BUSINESS SUMMARY"
echo "📆 $(date '+%A, %B %d, %Y')"
echo "📈 Prior 24 Hours Activity (since $YESTERDAY)"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🏠 SALES PROSPECTOR LEADS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
PROSPECTOR_LOG="/Users/claw/.openclaw/workspace/agents/sales-prospector/logs/prospector-v14-$YESTERDAY_DASH.log"
if [ -f "$PROSPECTOR_LOG" ]; then
    LEADS_FOUND=$(grep -c "LEAD [0-9]" "$PROSPECTOR_LOG" 2>/dev/null || echo "0")
    echo "✅ Leads discovered yesterday: ${LEADS_FOUND:-0}"
    echo ""
    echo "Recent leads:"
    grep "LEAD [0-9]" "$PROSPECTOR_LOG" 2>/dev/null | tail -3 | while read line; do
        echo "   → $line"
    done
else
    TOTAL=$(cat /Users/claw/.openclaw/workspace/agents/sales-prospector/state/prospector-v14-state.json 2>/dev/null | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('leads',0))")
    echo "✅ Total prospector leads: ${TOTAL:-0}"
    echo "   (v14 complete, targeting 750)"
fi
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🤖 JAE LEAD QUALIFICATIONS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
JAE_LOG="/Users/claw/.openclaw/workspace/agents/junior-ae/logs/jae-v3-$YESTERDAY_DASH.log"
if [ -f "$JAE_LOG" ]; then
    UPGRADES=$(grep -c "UPGRADE" "$JAE_LOG" 2>/dev/null || echo "0")
    DONE_LINE=$(grep "Done:" "$JAE_LOG" 2>/dev/null | tail -1)
    PROCESSED=$(echo "$DONE_LINE" | grep -o "[0-9]* processed" | head -1 | grep -o "[0-9]*")
    echo "✅ Leads elevated: ${UPGRADES:-0}"
    echo "✅ Total processed: ${PROCESSED:-0}"
else
    STATE_FILE="/Users/claw/.openclaw/workspace/agents/junior-ae/state/jae-v3-state.json"
    if [ -f "$STATE_FILE" ]; then
        cat "$STATE_FILE" | python3 -c "
import json,sys
d=json.load(sys.stdin)
print(f'Total processed: {d.get(\"processed\",0)}')
print(f'Total upgraded: {d.get(\"upgraded\",0)}')
print(f'Last run: {d.get(\"last_check\",\"never\")[:19]}')
"
    fi
fi
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🌐 WEBSITE LEADS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
INTEGRATION_LOG="/Users/claw/.openclaw/workspace/agents/sales-lead/integration.log"
YESTERDAY_GREP=$(date -v-1d '+%a %b %d' | sed 's/ 0//')
WEBSITE_LEADS=$(grep -c "NEW LEAD" "$INTEGRATION_LOG" 2>/dev/null || echo "0")

# Get yesterday's leads count - fix double echo
YESTERDAY_LEADS_RAW=$(grep "$YESTERDAY_GREP" "$INTEGRATION_LOG" 2>/dev/null | grep -c "NEW LEAD")
if [ -z "$YESTERDAY_LEADS_RAW" ]; then
    YESTERDAY_LEADS_RAW=0
fi

echo "✅ All-time website leads: ${WEBSITE_LEADS:-0}"
echo "✅ Yesterday's submissions: ~${YESTERDAY_LEADS_RAW:-0}"

echo ""
echo "Recent website leads:"
grep "NEW LEAD" "$INTEGRATION_LOG" 2>/dev/null | tail -3

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "☕ Generated at $(date '+%I:%M %p %Z')"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Auto-send to Telegram
MSG="📊 MORNING REPORT - $(date '+%b %d')

*Prospector:* $(cat /Users/claw/.openclaw/workspace/agents/sales-prospector/state/prospector-v14-state.json 2>/dev/null | python3 -c 'import json,sys; print(json.load(sys.stdin).get("leads",0))') leads
*JAE:* $(cat /Users/claw/.openclaw/workspace/agents/junior-ae/state/jae-v3-state.json 2>/dev/null | python3 -c 'import json,sys; d=json.load(sys.stdin); print(f"{d.get(\"upgraded\",0)} upgraded")')
*Website:* $(grep -c 'NEW LEAD' /Users/claw/.openclaw/workspace/agents/sales-lead/integration.log 2>/dev/null || echo '0') total

Report complete."

# Try OpenClaw then curl fallback
curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN:-}/sendMessage" \
  -d "chat_id=${CHAT_ID:-}&text=${MSG}&parse_mode=Markdown" 2>/dev/null || \
  openclaw message send --text "$MSG" 2>/dev/null || \
  echo "Report ready: $(date)" >> ~/.openclaw/workspace/agents/morning-report/logs/sent.log
