#!/bin/bash
# weekly-patterns.sh - Analyze Lessons Learned for recurring patterns
# Runs weekly (Sunday night) to identify improvement opportunities
#
# Cron: 0 20 * * 0 /root/clawd/scripts/weekly-patterns.sh
# (8pm UTC Sunday = 7am Monday Sydney AEDT)

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/config.env"

LESSONS_LEARNED_DB="01d0b783-d5bf-426a-9c3f-09612708a3bc"
PATTERN_FILE="$SCRIPT_DIR/.pattern_counts"

# Calculate date range (last 7 days)
WEEK_AGO=$(date -d "7 days ago" +"%Y-%m-%d" 2>/dev/null || date -v-7d +"%Y-%m-%d")
TODAY=$(date +"%Y-%m-%d")

echo "======================================"
echo "  WEEKLY PATTERN DETECTION"
echo "  $WEEK_AGO to $TODAY"
echo "======================================"
echo ""

# Fetch lessons from past week
LESSONS=$(curl -s -X POST "https://api.notion.com/v1/databases/${LESSONS_LEARNED_DB}/query" \
    -H "Authorization: Bearer $NOTION_API_KEY" \
    -H "Content-Type: application/json" \
    -H "Notion-Version: 2022-06-28" \
    -d "{
        \"filter\": {
            \"property\": \"Date Learned\",
            \"date\": {
                \"on_or_after\": \"$WEEK_AGO\"
            }
        },
        \"sorts\": [{\"property\": \"Date Learned\", \"direction\": \"descending\"}]
    }")

LESSON_COUNT=$(echo "$LESSONS" | python3 -c "import sys,json; print(len(json.load(sys.stdin).get('results',[])))" 2>/dev/null || echo "0")

echo "📊 Found $LESSON_COUNT lessons this week"
echo ""

if [ "$LESSON_COUNT" -eq 0 ]; then
    echo "No lessons to analyze. Exiting."
    exit 0
fi

# Analyze patterns using Python
ANALYSIS=$(echo "$LESSONS" | python3 << 'PYEOF'
import sys
import json
from collections import Counter, defaultdict

data = json.load(sys.stdin)
results = data.get('results', [])

# Tracking
categories = Counter()
sources = Counter()
keywords = Counter()
do_more = []
avoid = []
improve = []

# Common words to ignore
stopwords = {'the', 'and', 'for', 'that', 'this', 'with', 'was', 'were', 'been', 'have', 'has',
             'had', 'are', 'but', 'not', 'you', 'all', 'can', 'her', 'his', 'from', 'they',
             'will', 'would', 'there', 'their', 'what', 'about', 'which', 'when', 'make',
             'like', 'just', 'over', 'such', 'into', 'than', 'them', 'then', 'could', 'after'}

for r in results:
    props = r.get('properties', {})

    # Get fields
    title_arr = props.get('Lesson', {}).get('title', [])
    title = title_arr[0].get('text', {}).get('content', '') if title_arr else ''

    category = props.get('Category', {}).get('select', {})
    category_name = category.get('name', 'Unknown') if category else 'Unknown'
    categories[category_name] += 1

    source = props.get('Source', {}).get('select', {})
    source_name = source.get('name', 'Unknown') if source else 'Unknown'
    sources[source_name] += 1

    details_arr = props.get('Details', {}).get('rich_text', [])
    details = details_arr[0].get('text', {}).get('content', '') if details_arr else ''

    # Extract keywords (words > 4 chars, not stopwords)
    text = f"{title} {details}".lower()
    words = [w for w in text.split() if len(w) > 4 and w.isalpha() and w not in stopwords]
    keywords.update(words)

    # Categorize patterns
    text_lower = text.lower()
    if 'worked' in text_lower or 'success' in text_lower or 'good' in text_lower:
        do_more.append(title)
    if 'fail' in text_lower or 'error' in text_lower or 'bug' in text_lower or 'avoid' in text_lower:
        avoid.append(title)
    if 'slow' in text_lower or 'improve' in text_lower or 'better' in text_lower or 'long' in text_lower:
        improve.append(title)

# Output results
print("=== CATEGORY BREAKDOWN ===")
for cat, count in categories.most_common():
    pct = (count / len(results)) * 100
    print(f"  {cat}: {count} ({pct:.0f}%)")

print("\n=== SOURCE BREAKDOWN ===")
for src, count in sources.most_common():
    print(f"  {src}: {count}")

print("\n=== TOP KEYWORDS (repeated 2+) ===")
repeated_keywords = [(k, c) for k, c in keywords.most_common(20) if c >= 2]
for kw, count in repeated_keywords[:10]:
    print(f"  {kw}: {count}")

print("\n=== PATTERN CLUSTERS ===")
print(f"DO MORE ({len(do_more)} items):")
for item in do_more[:3]:
    print(f"  ✅ {item[:60]}...")
print(f"AVOID ({len(avoid)} items):")
for item in avoid[:3]:
    print(f"  ❌ {item[:60]}...")
print(f"IMPROVE ({len(improve)} items):")
for item in improve[:3]:
    print(f"  🔄 {item[:60]}...")

# Generate recommendations
print("\n=== RECOMMENDATIONS ===")
recommendations = []

# Category-based recommendations
if categories.get('Bug Pattern', 0) >= 3:
    recommendations.append("High bug frequency - consider adding more tests or code review")
if categories.get('Process', 0) >= 3:
    recommendations.append("Multiple process learnings - document in CLAUDE.md")

# Keyword-based recommendations
for kw, count in repeated_keywords:
    if count >= 3:
        recommendations.append(f"'{kw}' appears {count} times - investigate root cause")

# Pattern-based recommendations
if len(avoid) >= 3:
    recommendations.append(f"{len(avoid)} 'avoid' patterns - add to LESSONS.md gotchas section")
if len(do_more) >= 3:
    recommendations.append(f"{len(do_more)} 'do more' patterns - document as best practices")

if recommendations:
    for i, rec in enumerate(recommendations[:5], 1):
        print(f"  {i}. {rec}")
else:
    print("  No specific recommendations this week")

# Output JSON for task creation
import json as j
output = {
    "lesson_count": len(results),
    "top_category": categories.most_common(1)[0][0] if categories else "None",
    "top_keywords": [k for k, c in repeated_keywords[:5]],
    "recommendations": recommendations[:3],
    "do_more_count": len(do_more),
    "avoid_count": len(avoid),
    "improve_count": len(improve)
}
print("\n=== JSON_OUTPUT ===")
print(j.dumps(output))
PYEOF
)

echo "$ANALYSIS"

# Extract JSON output for task creation
JSON_DATA=$(echo "$ANALYSIS" | sed -n '/=== JSON_OUTPUT ===/,$ p' | tail -1)

# Parse recommendations
RECOMMENDATIONS=$(echo "$JSON_DATA" | python3 -c "import sys,json; d=json.load(sys.stdin); print('\n'.join(['• ' + r for r in d.get('recommendations',[])]))" 2>/dev/null || echo "• No specific recommendations")
LESSON_COUNT_PARSED=$(echo "$JSON_DATA" | python3 -c "import sys,json; print(json.load(sys.stdin).get('lesson_count',0))" 2>/dev/null || echo "0")
TOP_KEYWORDS=$(echo "$JSON_DATA" | python3 -c "import sys,json; print(', '.join(json.load(sys.stdin).get('top_keywords',[])))" 2>/dev/null || echo "none")

# Create improvement task in Work Tracker if we have recommendations
if [ -n "$RECOMMENDATIONS" ] && [ "$RECOMMENDATIONS" != "• No specific recommendations" ]; then
    echo ""
    echo "Creating improvement task in Work Tracker..."

    TASK_CONTENT="Weekly Pattern Analysis ($WEEK_AGO to $TODAY)

Lessons analyzed: $LESSON_COUNT_PARSED
Top keywords: $TOP_KEYWORDS

RECOMMENDATIONS:
$RECOMMENDATIONS

ACTION: Review these patterns and update docs/processes as needed."

    RESPONSE=$(curl -s -X POST "https://api.notion.com/v1/pages" \
        -H "Authorization: Bearer $NOTION_API_KEY" \
        -H "Content-Type: application/json" \
        -H "Notion-Version: 2022-06-28" \
        -d "{
            \"parent\": { \"database_id\": \"$WORK_TRACKER_DB\" },
            \"properties\": {
                \"Task\": { \"title\": [{ \"text\": { \"content\": \"Weekly Pattern Review - $(date +%b\ %d)\" } }] },
                \"Type\": { \"select\": { \"name\": \"Ops\" } },
                \"Status\": { \"select\": { \"name\": \"To Do\" } },
                \"Priority\": { \"select\": { \"name\": \"P2 Medium\" } },
                \"Owner\": { \"select\": { \"name\": \"Growth Engine\" } },
                \"Source\": { \"select\": { \"name\": \"Slack\" } },
                \"Notes\": { \"rich_text\": [{ \"text\": { \"content\": $(echo "$TASK_CONTENT" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))') } }] }
            }
        }")

    PAGE_ID=$(echo "$RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin).get('id',''))" 2>/dev/null || echo "")

    if [ -n "$PAGE_ID" ]; then
        echo "✅ Created task: Weekly Pattern Review"
    else
        echo "⚠️ Failed to create task"
    fi
fi

# Send summary to Telegram
echo ""
echo "Sending summary to Telegram..."

TELEGRAM_MSG="📊 *WEEKLY PATTERN REPORT*
$WEEK_AGO to $TODAY

Lessons analyzed: $LESSON_COUNT_PARSED
Top keywords: $TOP_KEYWORDS

*Recommendations:*
$RECOMMENDATIONS"

"$SCRIPT_DIR/telegram-alert.sh" "INFO" "$TELEGRAM_MSG"

echo ""
echo "======================================"
echo "  WEEKLY PATTERN DETECTION COMPLETE"
echo "======================================"
