#!/bin/bash

# Daily cost alert script for Clawdbot
# Checks token usage and sends alerts if cost exceeds threshold

set -e

TOKEN_DIR="$HOME/clawd/memory/tokens"
LATEST_FILE="$TOKEN_DIR/latest-token-usage.txt"
THRESHOLD=5

# Check if token data exists
if [ ! -f "$LATEST_FILE" ]; then
    echo "Error: Token usage data not found at $LATEST_FILE"
    echo "Run token-tracker.sh first"
    exit 1
fi

# Extract cost from latest file
COST=$(grep "Cost:" "$LATEST_FILE" | cut -d'$' -f2)
ALERT_REQUIRED=$(grep "Alert Required:" "$LATEST_FILE" | cut -d':' -f2 | tr -d ' ')

echo "=== Daily Cost Alert Check ==="
echo "Current cost: \$$COST"
echo "Threshold: \$$THRESHOLD"
echo "Alert required: $ALERT_REQUIRED"
echo ""

# Check if alert is needed
if [ "$ALERT_REQUIRED" = "1" ]; then
    echo "ALERT: Cost exceeds threshold of \$$THRESHOLD!"
    echo "Current cost: \$$COST"
    echo ""
    
    # Prepare alert message for Telegram
    CURRENT_DATE=$(date '+%Y-%m-%d')
    CURRENT_TIME=$(date '+%H:%M:%S')
    
    ALERT_MESSAGE="🚨 *Clawdbot Cost Alert* 🚨

*Date:* $CURRENT_DATE $CURRENT_TIME
*Cost:* \$$COST
*Threshold:* \$$THRESHOLD

Cost has exceeded the daily threshold. Please review token usage."

    echo "Alert message prepared:"
    echo "----------------------"
    echo "$ALERT_MESSAGE"
    echo "----------------------"
    echo ""
    
    # In a real implementation, you would send the Telegram message here
    # For now, we'll log it to a file
    ALERT_LOG="$TOKEN_DIR/alert-log.md"
    
    if [ ! -f "$ALERT_LOG" ]; then
        echo "# Cost Alert Log" > "$ALERT_LOG"
        echo "" >> "$ALERT_LOG"
        echo "| Date | Time | Cost | Threshold | Status |" >> "$ALERT_LOG"
        echo "|------|------|------|-----------|--------|" >> "$ALERT_LOG"
    fi
    
    echo "| $CURRENT_DATE | $CURRENT_TIME | \$$COST | \$$THRESHOLD | Alert Prepared |" >> "$ALERT_LOG"
    
    echo "Alert logged to: $ALERT_LOG"
    echo ""
    echo "Note: In production, this would send a Telegram message to Michael."
    echo "To implement Telegram sending, add:"
    echo "  curl -s -X POST https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage \\"
    echo "    -d chat_id=<MICHAEL_CHAT_ID> \\"
    echo "    -d text=\"$ALERT_MESSAGE\" \\"
    echo "    -d parse_mode=Markdown"
    
    # Return alert flag
    echo "ALERT"
else
    echo "OK: Cost is within threshold."
    echo "No alert needed."
    
    # Log normal operation
    CURRENT_DATE=$(date '+%Y-%m-%d')
    CURRENT_TIME=$(date '+%H:%M:%S')
    ALERT_LOG="$TOKEN_DIR/alert-log.md"
    
    if [ ! -f "$ALERT_LOG" ]; then
        echo "# Cost Alert Log" > "$ALERT_LOG"
        echo "" >> "$ALERT_LOG"
        echo "| Date | Time | Cost | Threshold | Status |" >> "$ALERT_LOG"
        echo "|------|------|------|-----------|--------|" >> "$ALERT_LOG"
    fi
    
    echo "| $CURRENT_DATE | $CURRENT_TIME | \$$COST | \$$THRESHOLD | OK |" >> "$ALERT_LOG"
    
    # Return OK flag
    echo "OK"
fi

echo ""
echo "=== Cost Alert Check Complete ==="