#!/usr/bin/env python3
"""
Simple status page generator
"""
import json
import os
from datetime import datetime

status_file = '/home/node/.openclaw/workspace/crypto-wallet/bot/status.json'
trades_file = '/home/node/.openclaw/workspace/crypto-wallet/bot/trades.log'

def format_num(n, decimals=2):
    try:
        return f"{float(n):.{decimals}f}"
    except:
        return str(n)

html = """<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🧙‍♂️ Gandalf Trader Status</title>
    <style>
        body { background: #0d1117; color: #e6edf3; font-family: 'Segoe UI', system-ui, sans-serif; padding: 20px; }
        .card { background: #161b22; border: 1px solid #30363d; border-radius: 8px; padding: 20px; margin: 10px 0; }
        .metric { display: inline-block; margin: 10px 20px 10px 0; }
        .label { color: #7d8590; font-size: 14px; }
        .value { font-size: 24px; font-weight: bold; }
        .positive { color: #3fb950; }
        .negative { color: #f85149; }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        th, td { text-align: left; padding: 8px; border-bottom: 1px solid #30363d; }
        th { color: #7d8590; }
        h1 { color: #58a6ff; }
        .header { display: flex; align-items: center; gap: 10px; }
    </style>
</head>
<body>
    <div class="header">
        <h1>🧙‍♂️ Gandalf Trader</h1>
        <span style="background:#1f6feb22;color:#58a6ff;padding:3px 10px;border-radius:12px;font-size:12px">PAPER MODE</span>
    </div>
"""

try:
    with open(status_file) as f:
        s = json.load(f)
    
    pnl = float(s.get('total_pnl', 0))
    pnl_pct = float(s.get('total_pnl_percent', 0))
    bankroll = float(s.get('bankroll', 0))
    trades = int(s.get('closed_trades', 0))
    wr = float(s.get('win_rate', 0))
    open_pos = int(s.get('open_positions', 0))
    dd = float(s.get('drawdown', 0))
    positions = s.get('positions', [])
    recent_trades = s.get('recent_trades', [])[:5]
    
    html += f"""
    <div class="card">
        <div class="metric">
            <div class="label">Bankroll</div>
            <div class="value">${format_num(bankroll)}</div>
        </div>
        <div class="metric">
            <div class="label">P&L</div>
            <div class="value {'positive' if pnl >= 0 else 'negative'}">{'+' if pnl >= 0 else ''}${format_num(pnl)} ({'+' if pnl_pct >= 0 else ''}{pnl_pct:.2f}%)</div>
        </div>
        <div class="metric">
            <div class="label">Win Rate</div>
            <div class="value">{wr:.0f}%</div>
        </div>
        <div class="metric">
            <div class="label">Trades</div>
            <div class="value">{trades}</div>
        </div>
        <div class="metric">
            <div class="label">Open</div>
            <div class="value">{open_pos}</div>
        </div>
        <div class="metric">
            <div class="label">DD</div>
            <div class="value negative">{dd:.2f}%</div>
        </div>
    </div>
    
    <div class="card">
        <h3>📍 Open Positions</h3>
"""
    
    if positions:
        html += "<table><tr><th>ID</th><th>Coin</th><th>Side</th><th>Entry</th><th>Size</th><th>Age (min)</th></tr>"
        for p in positions:
            html += f"<tr><td>{p.get('id','')}</td><td>{p.get('coin','')}</td><td>{p.get('side','')}</td><td>${format_num(p.get('entry',0))}</td><td>${format_num(p.get('size',0))}</td><td>{p.get('age',0)}</td></tr>"
        html += "</table>"
    else:
        html += "<p>No open positions</p>"
    
    html += "</div>"
    
    html += f"""
    <div class="card">
        <h3>📜 Recent Trades</h3>
"""
    
    if recent_trades:
        html += "<table><tr><th>Coin</th><th>Side</th><th>Entry</th><th>Exit</th><th>P&L</th><th>Reason</th></tr>"
        for t in recent_trades:
            pnl_t = float(t.get('pnl', 0))
            html += f"<tr><td>{t.get('coin','')}</td><td>{t.get('side','')}</td><td>${format_num(t.get('entry',0))}</td><td>${format_num(t.get('exit',0))}</td><td class={'positive' if pnl_t >= 0 else 'negative'}>{'+' if pnl_t >= 0 else ''}${format_num(pnl_t)}</td><td>{t.get('reason','')}</td></tr>"
        html += "</table>"
    else:
        html += "<p>No trades yet</p>"
    
    html += "</div>"

except Exception as e:
    html += f"<p>Error loading status: {e}</p>"

html += f"""
    <div class="card">
        <p style="color:#7d8590;font-size:12px">Last updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
        <p style="color:#7d8590;font-size:12px">Wallet: 0x0bA3cb704A41832ebEceAB78B7243b5a5b111F98</p>
    </div>
</body>
</html>"""

print(html)
