#!/usr/bin/env python3
"""
Gandalf's Hyperliquid Trader - Simple Telegram Reporter
Runs the bot and sends status updates to Telegram
"""

import sys
import os
import json
import time
import subprocess
from datetime import datetime

# Telegram config
TELEGRAM_CHAT_ID = "6725426239"

def send_telegram(message):
    """Send message to Telegram"""
    try:
        import requests
        token = "EMPTY"  # Will be filled by cron delivery
        # For now, just print
        print(f"[TELEGRAM] {message}")
    except:
        print(f"[TELEGRAM] {message}")

def run_bot_once():
    """Run one iteration of the bot and return status"""
    try:
        # Import and run the trading logic
        sys.path.insert(0, '/home/node/.openclaw/workspace/crypto-wallet/bot')
        from hyperliquid_bot import FundingRateTrader
        
        bot = FundingRateTrader()
        bot.run_once()  # Single iteration instead of loop
        
        # Get status
        status_file = '/home/node/.openclaw/workspace/crypto-wallet/bot/status.json'
        if os.path.exists(status_file):
            with open(status_file) as f:
                return json.load(f)
    except Exception as e:
        return {"error": str(e)}

def main():
    print(f"🧙‍♂️ Gandalf Trader - Run at {datetime.now()}")
    
    status = run_bot_once()
    
    if "error" in status:
        msg = f"❌ Bot Error: {status['error']}"
    else:
        msg = f"""📊 GANDALF TRADER UPDATE {datetime.now().strftime('%H:%M:%S')}

💰 Bankroll: ${status.get('bankroll', 'N/A')}
📈 P&L: {status.get('total_pnl_percent', 0):.2f}%
🎯 Trades: {status.get('closed_trades', 0)} ({status.get('win_rate', 0):.0f}% WR)
📍 Open: {status.get('open_positions', 0)}
⏱️ DD: {status.get('drawdown', 0):.2f}%

🟡 Mode: {status.get('mode', 'PAPER')}
⚡ Strategy: {status.get('strategy', 'N/A')}
📉 Sharpe: {status.get('sharpe', 'N/A')}"""
    
    send_telegram(msg)
    print(msg)

if __name__ == "__main__":
    main()
