#!/usr/bin/env python3
"""
Real-time signal generator - runs continuously.

Every 60 seconds, processes the latest order book data
and generates a SINGLE best trading signal per asset.
"""

import subprocess
import time
from pathlib import Path


def main():
    """Main loop."""

    print("="*80)
    print("REAL-TIME SINGLE SIGNAL GENERATOR")
    print("="*80)
    print("\nUpdating best signal every 60 seconds...")
    print("Press Ctrl+C to stop\n")

    update_count = 0

    try:
        while True:
            update_count += 1

            print(f"\n[{update_count}] Update at {time.strftime('%H:%M:%S')}")
            print("-"*80)

            # Run multi-asset signal generator
            print("1. Generating signals from order book...")
            result = subprocess.run(
                ['/home/ubuntu/.hermes/hermes-agent/.venv/bin/python3', 'generate_multi_asset_signals.py'],
                capture_output=True,
                text=True,
                cwd='/home/ubuntu/.hermes/workspace/projects/ORDER_FLOW_GRAPH/scripts'
            )

            if result.returncode != 0:
                print(f"  ✗ Error: {result.stderr}")
                time.sleep(60)
                continue

            # Generate single best signal
            print("2. Selecting best signal (closest to price)...")
            result = subprocess.run(
                ['/home/ubuntu/.hermes/hermes-agent/.venv/bin/python3', 'generate_single_signal.py'],
                capture_output=True,
                text=True,
                cwd='/home/ubuntu/.hermes/workspace/projects/ORDER_FLOW_GRAPH/scripts'
            )

            if result.returncode == 0:
                print("✓ Best signal selected")
                # Show the output
                for line in result.stdout.split('\n'):
                    if line.strip() and not line.startswith('=') and not line.startswith('-'):
                        print(f"  {line}")
            else:
                print(f"  ✗ Error: {result.stderr}")

            # Wait 60 seconds
            print("\n⏱️  Waiting 60 seconds...")
            time.sleep(60)

    except KeyboardInterrupt:
        print("\n\n✓ Stopped by user")
        print(f"Total updates: {update_count}")


if __name__ == "__main__":
    main()
