#!/bin/bash
# Morning Business Summary - 9 AM Daily Report
# Generates summary of prior 24h activity

WORKSPACE="/Users/claw/.openclaw/workspace/agents/morning-report"
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
TODAY=$(date +%Y-%m-%d)
YESTERDAY_START="${YESTERDAY}T00:00:00"
YESTERDAY_END="${TODAY}T00:00:00"

# 1. Sales Prospector Leads Found Yesterday
echo "📊 MORNING BUSINESS SUMMARY"
echo "📆 $(date '+%A, %B %d, %Y')"
echo "📈 Prior 24 Hours Activity"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🏠 SALES PROSPECTOR LEADS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
PROSPECTOR_LOGS="/Users/claw/.openclaw/workspace/agents/sales-prospector/logs/"
if [ -d "$PROSPECTOR_LOGS" ]; then
    # Check yesterday's logs
    YESTERDAY_LOG="${PROSPECTOR_LOGS}/prospector-*-${YESTERDAY//-/}.log"
    LEADS_FOUND=$(grep -h "LEAD [0-9]" $YESTERDAY_LOG 2>/dev/null | wc -l)
    echo "✅ Leads discovered yesterday: ${LEADS_FOUND:-0}"
    
    # Show yesterday's leads
    grep -h "LEAD [0-9]" $YESTERDAY_LOG 2>/dev/null | tail -5 | while read line; do
        echo "   → $line"
    done
else
    echo "⚠️  No prospector logs found"
fi
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🤖 JAE LEAD QUALIFICATIONS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
JAE_LOGS="/Users/claw/.openclaw/workspace/agents/junior-ae/logs/"
if [ -d "$JAE_LOGS" ]; then
    # Check yesterday's JAE activity
    YESTERDAY_JAE="${JAE_LOGS}/jae-*-${YESTERDAY//-/}.log"
    UPGRADES=$(grep -h "UPGRADE" $YESTERDAY_JAE 2>/dev/null | wc -l)
    PROCESSED=$(grep -h "Done:" $YESTERDAY_JAE 2>/dev/null | tail -1 | grep -o "[0-9]* processed" | grep -o "[0-9]*")
    echo "✅ Leads elevated (COLD→WARM or WARM→HOT): ${UPGRADES:-0}"
    echo "✅ Total processed: ${PROCESSED:-0}"
    echo ""
    echo "   Validation rate: ~${UPGRADES} upgrades from ${PROCESSED:-0} checks"
else
    echo "⚠️  No JAE logs found"
fi
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🌐 WEBSITE LEADS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
INTEGRATION_LOG="/Users/claw/.openclaw/workspace/agents/sales-lead/integration.log"
WEBSITE_LEADS=$(grep "NEW LEAD.*$(date -d yesterday '+%Y-%m-%d')" "$INTEGRATION_LOG" 2>/dev/null | wc -l)

echo "✅ Website form submissions: ${WEBSITE_LEADS:-0}"

# Count by source
INTEREST_FORM=$(grep "NEW LEAD" "$INTEGRATION_LOG" 2>/dev/null | grep "$(date -d yesterday '+%Y-%m-%d')" | grep -i "interest\|landing" | wc -l)
ROI_CALC=$(grep "NEW LEAD" "$INTEGRATION_LOG" 2>/dev/null | grep "$(date -d yesterday '+%Y-%m-%d')" | grep -i "calc\|calculator" | wc -l)

echo "   → Interest form: ${INTEREST_FORM:-0}"
echo "   → ROI calculator: ${ROI_CALC:-0}"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📊 TOTAL NEW OPPORTUNITIES"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
TOTAL=$((LEADS_FOUND + WEBSITE_LEADS))
echo "🎯 Total new leads in CRM: $TOTAL"
echo ""

# Optional: current totals
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📈 CRM STATUS (All Time)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
PROSPECTOR_STATE="/Users/claw/.openclaw/workspace/agents/sales-prospector/state/prospector-v14-state.json"
if [ -f "$PROSPECTOR_STATE" ]; then
    TOTAL_PROSPECTOR=$(cat "$PROSPECTOR_STATE" | python3 -c "import json,sys; print(json.load(sys.stdin).get('leads',0))" 2>/dev/null)
    echo "   Total prospector leads: ${TOTAL_PROSPECTOR:-0}"
fi

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