#!/bin/bash
# Test REAL data scraping and Pushover notifications
# PROJECT RULE: NEVER USE HARDCODED DATA - ALWAYS SCRAPE REAL DATA

echo "=== Testing REAL Solar Data Scraping ==="

# Configuration
PUSHOVER_USER_KEY="ubacf6yrzqi39wv5758ogeorksmyg2"
PUSHOVER_APP_TOKEN="apqypemqperophpagxtspat3tn7sfr"
SECOND_USER_KEY="us5sg9aqb2i7iqbjmmocr7p2df1ymp"

echo "1. Running Python scraper to get REAL data from e-SenZ..."
python3 solar_scraper.py > scraper_output.json 2>/dev/null

if [ $? -eq 0 ]; then
    echo "✅ Scraper completed successfully"
    
    # Extract real data from scraper output
    REAL_DATA=$(tail -n 1 scraper_output.json)
    
    if [ -n "$REAL_DATA" ]; then
        echo "📊 REAL Data obtained from e-SenZ:"
        echo "$REAL_DATA"
        
        # Parse real data for notification
        CURRENT_POWER=$(echo "$REAL_DATA" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('current_power', 'N/A'))")
        TODAY_ENERGY=$(echo "$REAL_DATA" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('today_energy', 'N/A'))")
        YESTERDAY_ENERGY=$(echo "$REAL_DATA" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('yesterday_energy', 'N/A'))")
        MONTHLY_ENERGY=$(echo "$REAL_DATA" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('monthly_energy', 'N/A'))")
        
        # Create message with REAL data from e-SenZ
        MESSAGE="🔋 Current Power: $CURRENT_POWER
📊 E Today: $TODAY_ENERGY
📈 E Yesterday: $YESTERDAY_ENERGY
📅 E This Month: $MONTHLY_ENERGY

⏰ $(date '+%Y-%m-%d %H:%M:%S')
🌐 REAL DATA from e-SenZ website"

        echo ""
        echo "2. Sending REAL data notification to primary user..."
        curl -s \
          --form-string "token=$PUSHOVER_APP_TOKEN" \
          --form-string "user=$PUSHOVER_USER_KEY" \
          --form-string "message=$MESSAGE" \
          --form-string "title=⚡ REAL Solar Data from e-SenZ" \
          --form-string "sound=cosmic" \
          https://api.pushover.net/1/messages.json

        if [ $? -eq 0 ]; then
            echo "✅ Primary user notification sent with REAL data from e-SenZ!"
        else
            echo "❌ Primary user notification failed!"
        fi

        echo ""
        echo "3. Sending REAL data notification to second user..."
        curl -s \
          --form-string "token=$PUSHOVER_APP_TOKEN" \
          --form-string "user=$SECOND_USER_KEY" \
          --form-string "message=$MESSAGE" \
          --form-string "title=⚡ REAL Solar Data from e-SenZ" \
          --form-string "sound=cosmic" \
          https://api.pushover.net/1/messages.json

        if [ $? -eq 0 ]; then
            echo "✅ Second user notification sent with REAL data from e-SenZ!"
        else
            echo "❌ Second user notification failed!"
        fi
        
        echo ""
        echo "🎯 REAL DATA CONFIRMED FROM E-SENZ:"
        echo "   Current Power: $CURRENT_POWER"
        echo "   Today Energy: $TODAY_ENERGY"
        echo "   Yesterday Energy: $YESTERDAY_ENERGY"
        echo "   Monthly Energy: $MONTHLY_ENERGY"
        
    else
        echo "❌ No data extracted from scraper"
    fi
    
    # Clean up
    rm -f scraper_output.json
    
else
    echo "❌ Scraper failed - check Python dependencies"
    echo "   Install: pip3 install -r requirements.txt"
fi

echo ""
echo "=== REAL Data Test Complete ===" 