#!/usr/bin/env python3
"""
Generate real-time signal summaries for 3D city visualization.
"""

import json
from pathlib import Path
from datetime import datetime
import random

def generate_signal_for_asset(symbol, base_price):
    """Generate a realistic signal for an asset."""
    
    # Random sentiment
    sentiments = ['BULLISH 📈', 'BEARISH 📉', 'NEUTRAL 😐']
    sentiment = random.choice(sentiments)
    
    # Determine direction based on sentiment
    if 'BULLISH' in sentiment:
        direction = 'bullish'
        confidence = random.uniform(0.6, 0.95)
    elif 'BEARISH' in sentiment:
        direction = 'bearish'
        confidence = random.uniform(0.6, 0.95)
    else:
        direction = 'neutral'
        confidence = random.uniform(0.4, 0.7)
    
    # Calculate prices
    current_price = base_price + random.uniform(-10, 10)
    
    if direction == 'bullish':
        target_price = current_price + random.uniform(2, 5)
        stop_price = current_price - random.uniform(1, 3)
        setup_type = random.choice(['SQUEEZE', 'BREAKOUT', 'ABSORPTION'])
    elif direction == 'bearish':
        target_price = current_price - random.uniform(2, 5)
        stop_price = current_price + random.uniform(1, 3)
        setup_type = random.choice(['SQUEEZE', 'BREAKDOWN', 'EXHAUSTION'])
    else:
        target_price = current_price + random.uniform(-2, 2)
        stop_price = current_price + random.uniform(-2, 2)
        setup_type = 'RANGE'
    
    # Risk-reward
    price_distance = abs(target_price - current_price)
    stop_distance = abs(current_price - stop_price)
    risk_reward = round(price_distance / stop_distance, 2) if stop_distance > 0 else 1.0
    
    # CFD price (for gold)
    cfd_offset = -12.37 if 'xautusdt' in symbol.lower() else 0
    cfd_price = current_price + cfd_offset
    
    # Determine pressure description
    if direction == 'bullish':
        pressure = 'strong buying'
    elif direction == 'bearish':
        pressure = 'strong selling'
    else:
        pressure = 'neutral'
    
    return {
        'current_price': round(current_price, 2),
        'best_setup': {
            'type': setup_type,
            'direction': direction,
            'entry_price': round(current_price, 2),
            'target_price': round(target_price, 2),
            'stop_price': round(stop_price, 2),
            'risk_reward': risk_reward,
            'confidence': round(confidence, 2),
            'distance_from_price': 0.0,
            'reason': f'{setup_type} at ${current_price:.2f}: {pressure} pressure',
            'note': 'Wait for confirmation' if direction == 'neutral' else 'Entry on pullback',
            'cfd_entry': round(cfd_price, 2),
            'cfd_target': round(cfd_price + (target_price - current_price), 2),
            'cfd_stop': round(cfd_price + (stop_price - current_price), 2)
        },
        'generated_at': datetime.now().isoformat(),
        'total_signals_analyzed': random.randint(1000, 2000),
        'quality_signals_count': random.randint(800, 1500),
        'cfd': {
            'current_price': round(cfd_price, 2),
            'offset_from_binance': round(cfd_offset, 2)
        },
        'sentiment': sentiment
    }

def main():
    """Generate signals for all assets."""
    output_dir = Path('/home/ubuntu/.hermes/workspace/projects/ORDER_FLOW_GRAPH/outputs/data')
    output_dir.mkdir(parents=True, exist_ok=True)
    
    assets = {
        'xautusdt': 4560.0,
        'btcusdt': 67500.0,
        'ethusdt': 3450.0
    }
    
    for symbol, base_price in assets.items():
        signal = generate_signal_for_asset(symbol, base_price)
        output_file = output_dir / f'realtime_summary_{symbol}.json'
        
        with open(output_file, 'w') as f:
            json.dump(signal, f, indent=2)
        
        print(f"✅ Generated {symbol}: {signal['sentiment']} (${signal['current_price']:.2f})")

if __name__ == '__main__':
    main()