#!/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
    # Count LEAD entries
    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
    # Check total from state
    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}"
    if [ "$PROCESSED" -gt 0 ] 2>/dev/null; then
        RATE=$(( UPGRADES * 100 / PROCESSED ))
        echo "   Validation rate: ${RATE}%"
    fi
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")
YESTERDAY_LEADS_COUNT=$(grep "$YESTERDAY_GREP" "$INTEGRATION_LOG" 2>/dev/null | grep -c "NEW LEAD" 2>/dev/null); YESTERDAY_LEADS_COUNT=${YESTERDAY_LEADS_COUNT:-0}

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

# Show last few leads
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 via OpenClaw
send_telegram() {
    MSG=$(<<-EOF
📊 *MORNING BUSINESS REPORT*
$(date '+📆 %A, %B %d, %Y')

*Prospector:* $PROSPECTOR_LEADS leads
*JAE:* $JAE_PROCESSED processed, $JAE_UPGRADED upgraded
*Website:* $WEBSITE_LEADS all-time, $YESTERDAY today

$(if [ "${YESTERDAY_LEADS_COUNT:-0}" -gt 0 ]; then echo "✅ Active day"; else echo "⚠️ No new activity"; fi)
EOF
)
    # Use OpenClaw's messaging
    openclaw message send --channel telegram --target telegram:8269921691 --message "$MSG" 2>/dev/null || echo "$MSG" >&2
}

send_telegram
