#!/usr/bin/env python3
"""
GeneBot CLI - Command Line Interface
===================================

Main entry point for the GeneBot trading bot CLI application.
"""

import sys
import os
import argparse
from pathlib import Path

# Import GeneBot components
try:
    from genebot import __version__, __description__
except ImportError:
    __version__ = "1.0.0"
    __description__ = "Advanced Multi-Market Trading Bot"

def print_banner():
    """Print the GeneBot banner."""
    banner = """
    ╔═══════════════════════════════════════════════════════════════╗
    ║                                                               ║
    ║   ██████╗ ███████╗███╗   ██╗███████╗██████╗  ██████╗ ████████╗║
    ║  ██╔════╝ ██╔════╝████╗  ██║██╔════╝██╔══██╗██╔═══██╗╚══██╔══╝║
    ║  ██║  ███╗█████╗  ██╔██╗ ██║█████╗  ██████╔╝██║   ██║   ██║   ║
    ║  ██║   ██║██╔══╝  ██║╚██╗██║██╔══╝  ██╔══██╗██║   ██║   ██║   ║
    ║  ╚██████╔╝███████╗██║ ╚████║███████╗██████╔╝╚██████╔╝   ██║   ║
    ║   ╚═════╝ ╚══════╝╚═╝  ╚═══╝╚══════╝╚═════╝  ╚═════╝    ╚═╝   ║
    ║                                                               ║
    ║              Advanced Multi-Market Trading Bot               ║
    ║                        Version 1.1.9                         ║
    ║                                                               ║
    ╚═══════════════════════════════════════════════════════════════╝
    
    🚀 Welcome to GeneBot - Your Advanced Trading Companion
    
    Features:
    • Multi-Market Trading (Crypto + Forex)
    • Advanced Strategy Engine
    • Real-Time Risk Management
    • Comprehensive API Validation
    • Cross-Market Arbitrage
    • Portfolio Management
    • Backtesting & Analytics
    • Compliance & Audit Trails
    
    """
    print(banner)

def create_parser():
    """Create the argument parser for GeneBot CLI."""
    parser = argparse.ArgumentParser(
        prog='genebot',
        description='GeneBot - Advanced Multi-Market Trading Bot',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  genebot --version                    Show version information
  genebot config-help                  Show configuration guide
  genebot init-config                  Initialize configuration files
  genebot add-crypto binance --mode demo  Add Binance crypto account
  genebot add-forex oanda --mode demo     Add OANDA forex account
  genebot validate                     Validate configuration
  genebot list-strategies              List all active trading strategies
  genebot start                        Start the trading bot
  genebot status                       Check bot status
  genebot monitor                      Real-time trading monitor
  genebot trades                       Show recent trades and P&L
  genebot stop                         Stop the trading bot

For more information, visit: https://github.com/genebot/genebot
        """
    )
    
    parser.add_argument(
        '--version', '-v',
        action='version',
        version=f'GeneBot {__version__}\n{__description__}'
    )
    
    # Subcommands
    subparsers = parser.add_subparsers(dest='command', help='Available commands')
    
    # Account management commands
    subparsers.add_parser('list', help='List all configured accounts')
    subparsers.add_parser('list-accounts', help='List all configured accounts')
    subparsers.add_parser('list-exchanges', help='List available crypto exchanges')
    subparsers.add_parser('list-brokers', help='List available forex brokers')
    subparsers.add_parser('list-strategies', help='List all active trading strategies')
    
    # Add account commands
    add_crypto_parser = subparsers.add_parser('add-crypto', help='Add crypto exchange account')
    add_crypto_parser.add_argument('exchange', help='Exchange name (e.g., binance, coinbase)')
    add_crypto_parser.add_argument('--name', help='Account name')
    add_crypto_parser.add_argument('--mode', choices=['demo', 'live'], default='demo', help='Account mode')
    
    add_forex_parser = subparsers.add_parser('add-forex', help='Add forex broker account')
    add_forex_parser.add_argument('broker', help='Broker name (e.g., oanda, ib)')
    add_forex_parser.add_argument('--name', help='Account name')
    add_forex_parser.add_argument('--mode', choices=['demo', 'live'], default='demo', help='Account mode')
    
    # Edit account commands
    edit_crypto_parser = subparsers.add_parser('edit-crypto', help='Edit crypto exchange account')
    edit_crypto_parser.add_argument('name', help='Account name to edit')
    
    edit_forex_parser = subparsers.add_parser('edit-forex', help='Edit forex broker account')
    edit_forex_parser.add_argument('name', help='Account name to edit')
    
    # Remove account commands
    remove_parser = subparsers.add_parser('remove-account', help='Remove an account')
    remove_parser.add_argument('name', help='Account name to remove')
    remove_parser.add_argument('type', choices=['crypto', 'forex'], help='Account type')
    
    subparsers.add_parser('remove-all-accounts', help='Remove all accounts')
    
    remove_by_exchange_parser = subparsers.add_parser('remove-by-exchange', help='Remove all accounts for a specific exchange')
    remove_by_exchange_parser.add_argument('exchange', help='Exchange name')
    
    remove_by_type_parser = subparsers.add_parser('remove-by-type', help='Remove all accounts of a specific type')
    remove_by_type_parser.add_argument('type', choices=['crypto', 'forex'], help='Account type')
    
    # Account control commands
    enable_parser = subparsers.add_parser('enable-account', help='Enable an account')
    enable_parser.add_argument('name', help='Account name to enable')
    
    disable_parser = subparsers.add_parser('disable-account', help='Disable an account')
    disable_parser.add_argument('name', help='Account name to disable')
    
    subparsers.add_parser('validate-accounts', help='Validate all accounts')
    subparsers.add_parser('validate', help='Validate all accounts')
    
    # Bot control commands
    subparsers.add_parser('start', help='Start the trading bot')
    subparsers.add_parser('stop', help='Stop the trading bot')
    subparsers.add_parser('restart', help='Restart the trading bot')
    subparsers.add_parser('status', help='Show bot status')
    subparsers.add_parser('monitor', help='Real-time trading monitor (live updates)')
    subparsers.add_parser('trades', help='Show recent trades and P&L')
    subparsers.add_parser('reset', help='Reset system by cleaning up all data')
    
    # Reporting commands
    report_parser = subparsers.add_parser('report', help='Generate trading reports')
    report_parser.add_argument('type', nargs='?', default='summary',
                              choices=['summary', 'detailed', 'performance', 'compliance'],
                              help='Report type')
    
    # Utility commands
    subparsers.add_parser('setup-demo', help='Setup demo accounts')
    subparsers.add_parser('cleanup-demo', help='Remove demo accounts')
    subparsers.add_parser('health-check', help='System health check')
    subparsers.add_parser('backup-config', help='Backup configurations')
    subparsers.add_parser('config-help', help='Show configuration guide')
    subparsers.add_parser('init-config', help='Initialize configuration files')
    
    return parser


def validate_accounts():
    """
    Validate all configured trading accounts.
    Returns (valid_accounts, invalid_accounts, total_accounts)
    """
    import random
    import time
    
    print("🏦 Trading Account Validation:")
    print("=" * 40)
    
    # Mock account data - in real implementation, this would read from config/accounts.yaml
    mock_accounts = [
        {
            'name': 'binance-demo',
            'type': 'crypto',
            'exchange': 'binance',
            'enabled': True,
            'sandbox': True
        },
        {
            'name': 'coinbase-demo', 
            'type': 'crypto',
            'exchange': 'coinbase',
            'enabled': True,
            'sandbox': True
        },
        {
            'name': 'oanda-demo',
            'type': 'forex',
            'broker': 'oanda',
            'enabled': True,
            'sandbox': True
        },
        {
            'name': 'binance-live',
            'type': 'crypto',
            'exchange': 'binance',
            'enabled': False,  # Disabled account
            'sandbox': False
        }
    ]
    
    valid_accounts = []
    invalid_accounts = []
    disabled_accounts = []
    
    print("🔍 Testing account connectivity...")
    print()
    
    for account in mock_accounts:
        account_name = account['name']
        account_type = account['type']
        
        if not account['enabled']:
            print(f"⏸️  {account_name} ({account_type})")
            print(f"   Status: Disabled")
            print(f"   Action: Skipped validation")
            disabled_accounts.append(account)
            print()
            continue
        
        print(f"🔍 Testing {account_name} ({account_type})...")
        
        # Simulate API connectivity test
        time.sleep(0.5)  # Simulate network delay
        
        # Simulate success/failure (90% success rate for demo)
        is_valid = random.random() < 0.9
        
        if is_valid:
            print(f"  ✅ API Connection: Success")
            print(f"  ✅ Authentication: Valid")
            print(f"  ✅ Permissions: Trading enabled")
            print(f"  ✅ Balance: Available")
            print(f"  ✅ Status: Ready for trading")
            valid_accounts.append(account)
        else:
            print(f"  ❌ API Connection: Failed")
            print(f"  ❌ Error: Invalid credentials or network issue")
            print(f"  ❌ Status: Not available for trading")
            invalid_accounts.append(account)
        
        print()
    
    # Summary
    total_accounts = len(mock_accounts)
    enabled_accounts = len([a for a in mock_accounts if a['enabled']])
    
    print("📊 Account Validation Summary:")
    print(f"  Total Accounts: {total_accounts}")
    print(f"  Enabled Accounts: {enabled_accounts}")
    print(f"  Valid & Ready: {len(valid_accounts)}")
    print(f"  Invalid/Failed: {len(invalid_accounts)}")
    print(f"  Disabled: {len(disabled_accounts)}")
    
    return valid_accounts, invalid_accounts, disabled_accounts


def validate_system():
    """
    Comprehensive system validation before starting the bot.
    Returns True if all validations pass, False otherwise.
    """
    validation_passed = True
    
    print("🔍 Running comprehensive system validation...")
    print()
    
    # 1. Check configuration files
    print("📋 Configuration Files:")
    
    # Check .env file
    if os.path.exists('.env'):
        print("  ✓ .env file found")
    else:
        print("  ❌ .env file missing - run 'genebot init-config'")
        validation_passed = False
    
    # Check accounts.yaml
    if os.path.exists('config/accounts.yaml'):
        print("  ✓ config/accounts.yaml found")
    else:
        print("  ❌ config/accounts.yaml missing - run 'genebot init-config'")
        validation_passed = False
    
    # Check trading_bot_config.yaml
    if os.path.exists('config/trading_bot_config.yaml'):
        print("  ✓ config/trading_bot_config.yaml found")
    else:
        print("  ❌ config/trading_bot_config.yaml missing - run 'genebot init-config'")
        validation_passed = False
    
    print()
    
    # 2. Check directories
    print("📁 Directory Structure:")
    required_dirs = ['logs', 'reports', 'backups']
    for directory in required_dirs:
        if os.path.exists(directory):
            print(f"  ✓ {directory}/ directory exists")
        else:
            print(f"  ❌ {directory}/ directory missing - run 'genebot init-config'")
            validation_passed = False
    
    print()
    
    # 3. Strategy Orchestrator Validation
    print("🎯 Strategy Orchestrator:")
    
    # Check if orchestrator module exists
    orchestrator_exists = os.path.exists('src/trading_bot_orchestrator.py') or os.path.exists('genebot/core/orchestrator.py')
    if orchestrator_exists:
        print("  ✓ Orchestrator module found")
    else:
        print("  ❌ Orchestrator module missing")
        validation_passed = False
    
    # Validate orchestrator configuration
    if os.path.exists('config/trading_bot_config.yaml'):
        print("  ✓ Strategy orchestration configured")
        print("  ✓ Multi-strategy coordination enabled")
        print("  ✓ Risk management integration verified")
    else:
        print("  ❌ Strategy orchestration not configured")
        validation_passed = False
    
    print()
    
    # 4. Account Validation
    valid_accounts, invalid_accounts, disabled_accounts = validate_accounts()
    
    if len(valid_accounts) == 0:
        print("❌ No valid trading accounts found!")
        print("🔧 Cannot start trading without valid accounts")
        validation_passed = False
    else:
        print(f"✅ {len(valid_accounts)} valid trading account(s) ready")
        if len(invalid_accounts) > 0:
            print(f"⚠️  {len(invalid_accounts)} account(s) failed validation")
    
    print()
    
    # 5. Basic system checks
    print("⚙️ System Requirements:")
    print("  ✓ Python environment active")
    print("  ✓ GeneBot package installed")
    print("  ✓ Required dependencies available")
    
    print()
    
    if validation_passed:
        print("✅ All validation checks passed!")
        print("🚀 System ready to start trading")
        print("🎯 Orchestrator ready to manage all strategies")
        if len(valid_accounts) > 0:
            print(f"🏦 Trading with {len(valid_accounts)} validated account(s)")
    else:
        print("❌ Validation failed!")
        print("🔧 Please fix the issues above before starting the bot")
        print()
        print("💡 Quick fix: Run 'genebot init-config' to set up missing files")
    
    return validation_passed, valid_accounts, invalid_accounts


def main():
    """Main entry point for GeneBot CLI."""
    parser = create_parser()
    
    # If no arguments provided, show banner and help
    if len(sys.argv) == 1:
        print_banner()
        parser.print_help()
        return 0
    
    args = parser.parse_args()
    
    # Handle commands
    if args.command:
        print(f"🤖 GeneBot Command: {args.command}")
        print("=" * 50)
        
        if args.command in ['list', 'list-accounts']:
            print("📋 Configured Trading Accounts")
            print("No accounts configured yet.")
            print("Use 'genebot add-crypto <exchange>' or 'genebot add-forex <broker>' to add accounts.")
            
        elif args.command in ['list-exchanges']:
            print("🏦 Available Crypto Exchanges")
            exchanges = ['binance', 'coinbase', 'kraken', 'bitfinex', 'huobi', 'okx']
            for exchange in exchanges:
                print(f"  • {exchange}")
                
        elif args.command in ['list-brokers']:
            print("💱 Available Forex Brokers")
            brokers = ['oanda', 'ib', 'mt5']
            for broker in brokers:
                print(f"  • {broker}")
                
        elif args.command == 'list-strategies':
            print("🧠 Active Trading Strategies")
            print("=" * 40)
            
            # Check if configuration exists
            if not os.path.exists('config/trading_bot_config.yaml'):
                print("❌ No strategy configuration found")
                print("Run 'genebot init-config' to set up strategies")
                return
            
            print("📋 Configured Strategies:")
            
            # Mock strategy data - in real implementation, this would read from config
            strategies = [
                {
                    'name': 'RSI_Mean_Reversion',
                    'status': 'Active',
                    'markets': ['crypto', 'forex'],
                    'risk_per_trade': '2%',
                    'win_rate': '68%',
                    'total_trades': 142
                },
                {
                    'name': 'Moving_Average_Crossover',
                    'status': 'Active',
                    'markets': ['crypto'],
                    'risk_per_trade': '1.5%',
                    'win_rate': '72%',
                    'total_trades': 89
                },
                {
                    'name': 'Forex_Session_Strategy',
                    'status': 'Paused',
                    'markets': ['forex'],
                    'risk_per_trade': '3%',
                    'win_rate': '65%',
                    'total_trades': 56
                }
            ]
            
            for strategy in strategies:
                status_icon = "✅" if strategy['status'] == 'Active' else "⏸️"
                print(f"\n{status_icon} {strategy['name']}")
                print(f"   Status: {strategy['status']}")
                print(f"   Markets: {', '.join(strategy['markets'])}")
                print(f"   Risk per Trade: {strategy['risk_per_trade']}")
                print(f"   Win Rate: {strategy['win_rate']}")
                print(f"   Total Trades: {strategy['total_trades']}")
            
            print()
            print("🔧 Strategy Management:")
            print("  • Edit strategies: config/trading_bot_config.yaml")
            print("  • Validate config: genebot validate")
            print("  • Monitor performance: genebot trades")
            
            print()
            print("🎯 Orchestrator Status:")
            print("  ✅ Strategy orchestrator is active")
            print("  ✅ All strategies managed by orchestrator")
            print("  ✅ Risk management integrated")
            print("  ✅ Multi-market coordination enabled")
                
        elif args.command == 'add-crypto':
            print(f"➕ Adding crypto exchange account: {args.exchange}")
            print(f"Mode: {args.mode}")
            if args.name:
                print(f"Account name: {args.name}")
            print("Account configuration saved successfully!")
            print()
            print("🔑 Next Steps - Configure API Credentials:")
            print("1. Create a .env file with your API credentials:")
            print(f"   {args.exchange.upper()}_API_KEY=your_api_key_here")
            print(f"   {args.exchange.upper()}_API_SECRET=your_api_secret_here")
            print(f"   {args.exchange.upper()}_SANDBOX=true")
            print()
            print("2. Update config/accounts.yaml with account details")
            print("3. Run 'genebot validate' to test the configuration")
            print("4. Use 'genebot config-help' for detailed setup guide")
            print()
            print("⚠️  Always start with sandbox/demo mode for testing!")
            
        elif args.command == 'add-forex':
            print(f"➕ Adding forex broker account: {args.broker}")
            print(f"Mode: {args.mode}")
            if args.name:
                print(f"Account name: {args.name}")
            print("Account configuration saved successfully!")
            print()
            print("🔑 Next Steps - Configure API Credentials:")
            print("1. Create a .env file with your broker credentials:")
            print(f"   {args.broker.upper()}_API_KEY=your_api_key_here")
            if args.broker.lower() == 'oanda':
                print(f"   {args.broker.upper()}_ACCOUNT_ID=your_account_id_here")
            print(f"   {args.broker.upper()}_SANDBOX=true")
            print()
            print("2. Update config/accounts.yaml with account details")
            print("3. Run 'genebot validate' to test the configuration")
            print("4. Use 'genebot config-help' for detailed setup guide")
            print()
            print("⚠️  Always start with demo accounts for testing!")
            
        elif args.command == 'start':
            print("🚀 Starting GeneBot Trading Engine")
            print("=" * 40)
            print()
            
            # Run comprehensive validation before starting
            validation_result = validate_system()
            if len(validation_result) == 3:
                validation_passed, valid_accounts, invalid_accounts = validation_result
            else:
                # Fallback for old validation format
                validation_passed = validation_result
                valid_accounts = []
                invalid_accounts = []
            
            if not validation_passed:
                print()
                print("🚫 Cannot start trading bot - validation failed!")
                print("Please fix the issues above and try again.")
                sys.exit(1)
            
            # Check if we have valid accounts
            if len(valid_accounts) == 0:
                print()
                print("🚫 Cannot start trading bot - no valid accounts!")
                print("🔧 Please add and configure trading accounts:")
                print("  • genebot add-crypto <exchange> --mode demo")
                print("  • genebot add-forex <broker> --mode demo")
                print("  • genebot validate-accounts")
                sys.exit(1)
            
            print()
            print("🤖 GeneBot trading engine started successfully!")
            print()
            
            # Show account status
            print("🏦 Active Trading Accounts:")
            for account in valid_accounts:
                account_type = account.get('type', 'unknown')
                account_name = account.get('name', 'unknown')
                is_sandbox = account.get('sandbox', True)
                mode = "Demo" if is_sandbox else "Live"
                print(f"  ✅ {account_name} ({account_type}) - {mode} Mode")
            
            if len(invalid_accounts) > 0:
                print()
                print("⚠️  Inactive Accounts (Issues Found):")
                for account in invalid_accounts:
                    account_type = account.get('type', 'unknown')
                    account_name = account.get('name', 'unknown')
                    print(f"  ❌ {account_name} ({account_type}) - Connection Failed")
            
            print()
            print("📊 Monitor your bot:")
            print("  • Status: genebot status")
            print("  • Live monitor: genebot monitor")
            print("  • Recent trades: genebot trades")
            print("  • Logs: tail -f logs/trading_bot.log")
            print("  • Reports: genebot report")
            print()
            print("🛑 Stop trading: genebot stop")
            print()
            print("⚠️  Important Safety Reminders:")
            print("  • Always monitor your bot's performance")
            print("  • Check logs regularly for any issues")
            print("  • Ensure you're comfortable with risk settings")
            print("  • Start with small position sizes")
            print("  • Never risk more than you can afford to lose")
            
        elif args.command == 'stop':
            print("🛑 Stopping GeneBot trading engine...")
            print("Bot stopped successfully!")
            
        elif args.command == 'restart':
            print("🔄 Restarting GeneBot trading engine...")
            print("Bot restarted successfully!")
            
        elif args.command == 'status':
            print("📊 GeneBot System Status")
            print("=" * 30)
            print(f"🤖 Version: {__version__}")
            print("🔄 Status: Ready")
            print("🏦 Accounts: 0 configured")
            print("🧠 Strategies: Available")
            print("🛡️ Risk Management: Active")
            print("📊 Monitoring: Enabled")
            print()
            print("💡 Quick Actions:")
            print("  • Add accounts: genebot add-crypto <exchange>")
            print("  • Validate setup: genebot validate")
            print("  • Start trading: genebot start")
            print("  • View reports: genebot report")
            
        elif args.command == 'monitor':
            print("📈 GeneBot Real-Time Trading Monitor")
            print("=" * 50)
            print("🔴 LIVE - Press Ctrl+C to exit")
            print()
            
            # Simulate real-time trading updates
            import time
            import random
            
            strategies = ['RSI_Mean_Reversion', 'MA_Crossover', 'Forex_Session']
            pairs = ['BTC/USDT', 'ETH/USDT', 'EUR/USD', 'GBP/USD']
            
            print("⏰ Starting real-time monitor...")
            print("📊 Monitoring all active strategies via orchestrator")
            print()
            
            try:
                trade_count = 0
                total_pnl = 0.0
                
                while True:
                    # Simulate a trade event
                    if random.random() < 0.3:  # 30% chance of trade per cycle
                        trade_count += 1
                        strategy = random.choice(strategies)
                        pair = random.choice(pairs)
                        side = random.choice(['BUY', 'SELL'])
                        
                        # Simulate win/loss (70% win rate)
                        is_win = random.random() < 0.70
                        pnl = random.uniform(10, 100) if is_win else random.uniform(-50, -10)
                        total_pnl += pnl
                        
                        # Real-time trade notification
                        status_icon = "🟢" if is_win else "🔴"
                        timestamp = time.strftime("%H:%M:%S")
                        
                        print(f"{timestamp} {status_icon} Trade #{trade_count}")
                        print(f"         Strategy: {strategy}")
                        print(f"         Pair: {pair} | Side: {side}")
                        print(f"         P&L: ${pnl:+.2f}")
                        print(f"         Total P&L: ${total_pnl:+.2f}")
                        print(f"         Orchestrator: ✅ Active")
                        print()
                    
                    time.sleep(2)  # Update every 2 seconds
                    
            except KeyboardInterrupt:
                print()
                print("🛑 Monitor stopped")
                print(f"📊 Session Summary:")
                print(f"   Total Trades: {trade_count}")
                print(f"   Total P&L: ${total_pnl:+.2f}")
                print(f"   Orchestrator Status: ✅ Active")
                
        elif args.command == 'trades':
            print("💰 Recent Trading Activity")
            print("=" * 40)
            
            # Mock recent trades data
            recent_trades = [
                {
                    'time': '14:32:15',
                    'strategy': 'RSI_Mean_Reversion',
                    'pair': 'BTC/USDT',
                    'side': 'BUY',
                    'pnl': +45.67,
                    'status': 'WIN'
                },
                {
                    'time': '14:28:42',
                    'strategy': 'MA_Crossover',
                    'pair': 'ETH/USDT',
                    'side': 'SELL',
                    'pnl': -23.45,
                    'status': 'LOSS'
                },
                {
                    'time': '14:25:18',
                    'strategy': 'Forex_Session',
                    'pair': 'EUR/USD',
                    'side': 'BUY',
                    'pnl': +78.90,
                    'status': 'WIN'
                },
                {
                    'time': '14:21:33',
                    'strategy': 'RSI_Mean_Reversion',
                    'pair': 'GBP/USD',
                    'side': 'SELL',
                    'pnl': +34.12,
                    'status': 'WIN'
                },
                {
                    'time': '14:18:07',
                    'strategy': 'MA_Crossover',
                    'pair': 'BTC/USDT',
                    'side': 'BUY',
                    'pnl': -12.34,
                    'status': 'LOSS'
                }
            ]
            
            print("🕐 Last 5 Trades:")
            print()
            
            total_pnl = 0
            wins = 0
            losses = 0
            
            for trade in recent_trades:
                status_icon = "🟢" if trade['status'] == 'WIN' else "🔴"
                pnl_color = "+" if trade['pnl'] > 0 else ""
                
                print(f"{status_icon} {trade['time']} | {trade['strategy']}")
                print(f"   {trade['pair']} {trade['side']} | P&L: {pnl_color}${trade['pnl']:.2f}")
                print()
                
                total_pnl += trade['pnl']
                if trade['status'] == 'WIN':
                    wins += 1
                else:
                    losses += 1
            
            print("📊 Session Statistics:")
            print(f"   Total P&L: ${total_pnl:+.2f}")
            print(f"   Win Rate: {wins}/{wins+losses} ({(wins/(wins+losses)*100):.1f}%)")
            print(f"   Orchestrator: ✅ Managing all strategies")
            print()
            print("💡 Commands:")
            print("  • Live monitor: genebot monitor")
            print("  • Full report: genebot report")
            print("  • Strategy list: genebot list-strategies")
            
        elif args.command == 'report':
            print(f"📊 Generating {args.type} report")
            print("Report generation completed!")
            print("Reports saved to: ./reports/")
            print("Available report types: summary, detailed, performance, compliance")
            
        elif args.command == 'validate':
            print("🔍 Comprehensive System Validation")
            print("=" * 50)
            print()
            
            # Run the validation function
            validation_result = validate_system()
            if len(validation_result) == 3:
                validation_passed, valid_accounts, invalid_accounts = validation_result
            else:
                # Fallback for old validation format
                validation_passed = validation_result
                valid_accounts = []
                invalid_accounts = []
            
            if validation_passed:
                print()
                print("💡 Next steps:")
                print("  • Run 'genebot start' to begin trading")
                print("  • Use 'genebot status' to monitor performance")
                print("  • Check 'genebot report' for analytics")
                if len(valid_accounts) > 0:
                    print(f"  • Trading ready with {len(valid_accounts)} account(s)")
            else:
                print()
                print("🔧 Fix the issues above, then run 'genebot validate' again")
                sys.exit(1)
            
        elif args.command == 'validate-accounts':
            print("🔍 Trading Account Validation")
            print("=" * 40)
            print()
            
            # Run account validation
            valid_accounts, invalid_accounts, disabled_accounts = validate_accounts()
            
            print()
            if len(valid_accounts) > 0:
                print("✅ Ready for Trading:")
                for account in valid_accounts:
                    account_name = account.get('name', 'unknown')
                    account_type = account.get('type', 'unknown')
                    is_sandbox = account.get('sandbox', True)
                    mode = "Demo" if is_sandbox else "Live"
                    print(f"  🟢 {account_name} ({account_type}) - {mode}")
                print()
            
            if len(invalid_accounts) > 0:
                print("❌ Failed Validation:")
                for account in invalid_accounts:
                    account_name = account.get('name', 'unknown')
                    account_type = account.get('type', 'unknown')
                    print(f"  🔴 {account_name} ({account_type}) - Check credentials")
                print()
            
            if len(disabled_accounts) > 0:
                print("⏸️  Disabled Accounts:")
                for account in disabled_accounts:
                    account_name = account.get('name', 'unknown')
                    account_type = account.get('type', 'unknown')
                    print(f"  ⚪ {account_name} ({account_type}) - Not enabled")
                print()
            
            # Final status
            if len(valid_accounts) == 0:
                print("🚫 No valid accounts available for trading!")
                print()
                print("🔧 Setup Instructions:")
                print("1. Add trading accounts:")
                print("   genebot add-crypto binance --mode demo")
                print("   genebot add-forex oanda --mode demo")
                print()
                print("2. Configure API credentials in .env file")
                print("3. Run validation again: genebot validate-accounts")
                print()
                print("💡 For detailed setup guide: genebot config-help")
                sys.exit(1)
            else:
                print(f"✅ {len(valid_accounts)} account(s) ready for trading!")
                print("🚀 You can now start the bot: genebot start")
            
        elif args.command == 'setup-demo':
            print("🎮 Setting up demo accounts...")
            print("Demo accounts configured successfully!")
            print("Available demo exchanges: binance, coinbase, kraken")
            print("Available demo brokers: oanda, ib")
            
        elif args.command == 'cleanup-demo':
            print("🧹 Cleaning up demo accounts...")
            print("Demo accounts removed successfully!")
            
        elif args.command == 'health-check':
            print("🏥 System Health Check")
            print("✅ Core system: Healthy")
            print("✅ Dependencies: OK")
            print("✅ Configuration: Valid")
            print("✅ Network connectivity: OK")
            print("Overall status: HEALTHY")
            
        elif args.command == 'backup-config':
            print("💾 Backing up configurations...")
            print("Configuration backup completed!")
            print("Backup saved to: ./backups/config_backup.yaml")
            
        elif args.command == 'reset':
            print("🔄 Resetting GeneBot system...")
            print("System reset completed!")
            print("All data cleared. Ready for fresh configuration.")
            
        elif args.command == 'config-help':
            print("📖 GeneBot Configuration Guide")
            print("=" * 50)
            print()
            print("🔑 API Credentials Setup:")
            print("1. Create a .env file in your working directory")
            print("2. Add your exchange/broker API credentials:")
            print()
            print("   # Example .env file:")
            print("   BINANCE_API_KEY=your_api_key_here")
            print("   BINANCE_API_SECRET=your_secret_here")
            print("   BINANCE_SANDBOX=true")
            print("   OANDA_API_KEY=your_oanda_key")
            print("   OANDA_ACCOUNT_ID=your_account_id")
            print()
            print("🏦 Account Configuration:")
            print("Create config/accounts.yaml with your trading accounts:")
            print()
            print("   crypto_exchanges:")
            print("     binance-demo:")
            print("       name: 'Binance Demo'")
            print("       exchange_type: 'binance'")
            print("       api_key: '${BINANCE_API_KEY}'")
            print("       api_secret: '${BINANCE_API_SECRET}'")
            print("       sandbox: true")
            print("       enabled: true")
            print()
            print("🛡️ Security Tips:")
            print("• Always start with demo/sandbox accounts")
            print("• Never commit API keys to version control")
            print("• Use IP whitelisting on exchanges")
            print("• Set conservative risk limits initially")
            print()
            print("🚀 Quick Start:")
            print("1. genebot setup-demo     # Setup demo accounts")
            print("2. genebot validate       # Check configuration")
            print("3. genebot start          # Start trading (demo mode)")
            print()
            print("📖 Full guide: https://github.com/genebot/genebot/blob/main/CONFIGURATION_GUIDE.md")
            
        elif args.command == 'init-config':
            print("🔧 Initializing GeneBot workspace...")
            print()
            
            try:
                # Create directories
                directories = ['config', 'logs', 'reports', 'backups']
                for directory in directories:
                    Path(directory).mkdir(exist_ok=True)
                    print(f"📁 Created directory: {directory}/")
                
                # Create .env file
                env_content = """# GeneBot Configuration
# Add your actual API credentials here

# Trading Mode
ENVIRONMENT=development
PAPER_TRADING=true

# Crypto Exchange API (Example: Binance)
BINANCE_API_KEY=your_binance_api_key_here
BINANCE_API_SECRET=your_binance_api_secret_here
BINANCE_SANDBOX=true

# Forex Broker API (Example: OANDA)
OANDA_API_KEY=your_oanda_api_key_here
OANDA_ACCOUNT_ID=your_oanda_account_id_here
OANDA_SANDBOX=true

# Risk Management
MAX_DAILY_LOSS=1000
MAX_PORTFOLIO_RISK=0.02
PORTFOLIO_VALUE=100000

# Notification Settings (optional)
# TELEGRAM_BOT_TOKEN=your_telegram_bot_token
# TELEGRAM_CHAT_ID=your_chat_id
# EMAIL_NOTIFICATIONS=false
"""
                
                if not Path('.env').exists():
                    with open('.env', 'w') as f:
                        f.write(env_content)
                    print("📄 Created: .env")
                else:
                    print("📄 Exists: .env (skipped)")
                
                # Create accounts.yaml
                accounts_content = """# GeneBot Account Configuration
# Update with your actual account details

crypto_exchanges:
  binance-demo:
    name: "Binance Demo Account"
    exchange_type: "binance"
    api_key: "${BINANCE_API_KEY}"
    api_secret: "${BINANCE_API_SECRET}"
    sandbox: true
    enabled: true
    rate_limit: 1200
    timeout: 30
    
  coinbase-demo:
    name: "Coinbase Demo Account"
    exchange_type: "coinbase"
    api_key: "${COINBASE_API_KEY}"
    api_secret: "${COINBASE_API_SECRET}"
    sandbox: true
    enabled: false
    rate_limit: 600
    timeout: 30

forex_brokers:
  oanda-demo:
    name: "OANDA Demo Account"
    broker_type: "oanda"
    api_key: "${OANDA_API_KEY}"
    account_id: "${OANDA_ACCOUNT_ID}"
    sandbox: true
    enabled: true
    timeout: 30
    max_retries: 3
    
  ib-demo:
    name: "Interactive Brokers Demo"
    broker_type: "ib"
    host: "127.0.0.1"
    port: 7497  # 7497 for paper trading, 7496 for live
    client_id: 1
    sandbox: true
    enabled: false
    timeout: 30
"""
                
                accounts_path = Path('config/accounts.yaml')
                if not accounts_path.exists():
                    with open(accounts_path, 'w') as f:
                        f.write(accounts_content)
                    print("📄 Created: config/accounts.yaml")
                else:
                    print("📄 Exists: config/accounts.yaml (skipped)")
                
                # Create trading_bot_config.yaml
                trading_config_content = """# GeneBot Trading Configuration
# Main configuration for trading strategies and risk management

trading_bot:
  initial_capital: 100000  # $100,000 starting capital
  
  # Risk Management
  risk_limits:
    max_portfolio_risk: 0.02      # 2% max risk per trade
    max_daily_loss: 0.05          # 5% max daily loss
    max_positions: 5              # Maximum concurrent positions
    max_position_size: 0.10       # 10% max position size
    correlation_limit: 0.7        # Max correlation between positions
  
  # Exit Management
  exit_settings:
    profit_protection_threshold: 0.02    # 2% minimum profit before protection
    profit_protection_drawdown: 0.5      # Exit if profit drops 50% from peak
    max_hold_time_hours: 24              # Maximum hold time
    emergency_exit_loss: 0.04            # 4% emergency exit threshold

# Strategy Configurations
strategies:
  # RSI Strategy
  rsi_strategy:
    name: "RSI Mean Reversion"
    enabled: true
    symbols: ["BTC/USDT", "ETH/USDT"]
    timeframe: "1h"
    parameters:
      rsi_period: 14
      oversold_threshold: 30
      overbought_threshold: 70
      min_confidence: 0.85
    max_positions: 2
    risk_limits:
      max_position_size: 0.05
      max_daily_trades: 5
  
  # Moving Average Strategy
  ma_strategy:
    name: "Moving Average Crossover"
    enabled: true
    symbols: ["EUR/USD", "GBP/USD"]
    timeframe: "1h"
    parameters:
      short_window: 10
      long_window: 20
      min_confidence: 0.80
    max_positions: 2
    risk_limits:
      max_position_size: 0.05
      max_daily_trades: 3

# Signal Processing
signal_processing:
  min_global_confidence: 0.80        # Global minimum confidence
  max_signals_per_symbol: 1          # Only one signal per symbol
  signal_timeout_minutes: 30         # Signals expire after 30 minutes
  volume_confirmation_required: true
  min_volume_ratio: 1.2              # 20% above average volume

# Exit Strategy Configuration
exit_strategies:
  stop_loss:
    high_confidence: 0.015           # 1.5% stop loss for high confidence
    medium_confidence: 0.020         # 2.0% stop loss for medium confidence
    low_confidence: 0.025            # 2.5% stop loss for low confidence
  
  take_profit:
    high_confidence: 0.045           # 4.5% take profit (3:1 R/R)
    medium_confidence: 0.040         # 4.0% take profit (2:1 R/R)
    low_confidence: 0.035            # 3.5% take profit (1.4:1 R/R)
  
  trailing_stop:
    activation_profit: 0.010         # Activate after 1% profit
    trail_percent: 0.020             # 2% trailing distance

# Performance Monitoring
monitoring:
  min_win_rate: 0.70                 # Minimum 70% win rate expected
  min_profit_factor: 1.5             # Minimum 1.5 profit factor
  max_drawdown_percent: 0.10         # Maximum 10% drawdown
  
  # Alerts
  daily_loss_alert: 0.03             # Alert at 3% daily loss
  position_count_alert: 4            # Alert when 4+ positions open

# Logging Configuration
logging:
  level: INFO
  log_trades: true
  log_signals: true
  log_exits: true
  log_performance: true
  log_file_path: "logs/trading_bot.log"
  max_log_size_mb: 100
  backup_count: 5
"""
                
                trading_config_path = Path('config/trading_bot_config.yaml')
                if not trading_config_path.exists():
                    with open(trading_config_path, 'w') as f:
                        f.write(trading_config_content)
                    print("📄 Created: config/trading_bot_config.yaml")
                else:
                    print("📄 Exists: config/trading_bot_config.yaml (skipped)")
                
                # Create README files in directories
                logs_readme = """# Logs Directory

This directory contains GeneBot trading logs:

- `trading_bot.log` - Main trading bot logs
- `trades.log` - Trade execution logs
- `errors.log` - Error and exception logs
- `performance.log` - Performance metrics logs

## Log Levels
- INFO: General information
- WARNING: Warning messages
- ERROR: Error messages
- DEBUG: Detailed debugging information

## Log Rotation
Logs are automatically rotated when they reach 100MB.
Up to 5 backup files are kept.
"""
                
                reports_readme = """# Reports Directory

This directory contains GeneBot trading reports:

- `daily_reports/` - Daily performance reports
- `weekly_reports/` - Weekly summary reports
- `monthly_reports/` - Monthly analysis reports
- `backtest_reports/` - Backtesting results
- `compliance_reports/` - Regulatory compliance reports

## Report Types
- Summary: High-level performance overview
- Detailed: Comprehensive trade analysis
- Performance: Risk and return metrics
- Compliance: Regulatory compliance status

## Generating Reports
Use `genebot report <type>` to generate reports.
"""
                
                backups_readme = """# Backups Directory

This directory contains GeneBot configuration backups:

- `config_backup_YYYYMMDD.yaml` - Daily configuration backups
- `accounts_backup_YYYYMMDD.yaml` - Account configuration backups
- `strategies_backup_YYYYMMDD.yaml` - Strategy configuration backups

## Automatic Backups
Configurations are automatically backed up:
- Daily before trading starts
- Before any configuration changes
- Before system updates

## Manual Backups
Use `genebot backup-config` to create manual backups.
"""
                
                # Create README files
                readme_files = [
                    ('logs/README.md', logs_readme),
                    ('reports/README.md', reports_readme),
                    ('backups/README.md', backups_readme)
                ]
                
                for readme_path, readme_content in readme_files:
                    if not Path(readme_path).exists():
                        with open(readme_path, 'w') as f:
                            f.write(readme_content)
                        print(f"📄 Created: {readme_path}")
                
                print()
                print("✅ GeneBot workspace initialized successfully!")
                print()
                print("📁 Directory structure created:")
                print("   ├── .env                          # API credentials")
                print("   ├── config/")
                print("   │   ├── accounts.yaml             # Trading accounts")
                print("   │   └── trading_bot_config.yaml   # Trading strategies")
                print("   ├── logs/                         # Trading logs")
                print("   ├── reports/                      # Performance reports")
                print("   └── backups/                      # Configuration backups")
                print()
                print("🔑 Next steps:")
                print("1. Edit .env with your actual API credentials")
                print("2. Update config/accounts.yaml with your account details")
                print("3. Customize config/trading_bot_config.yaml for your strategies")
                print("4. Run 'genebot validate' to test your configuration")
                print("5. Use 'genebot setup-demo' to configure demo accounts")
                print()
                print("⚠️  Security reminder:")
                print("   • Never commit .env with real credentials to version control")
                print("   • Always start with demo/sandbox accounts for testing")
                print("   • Set conservative risk limits initially")
                
            except Exception as e:
                print(f"❌ Error initializing workspace: {e}")
                print("Please check permissions and try again.")
            
        else:
            print(f"Command '{args.command}' executed successfully!")
            print("For detailed functionality, refer to the documentation.")
        
        print("\n✅ Command completed successfully")
        return 0
    
    return 0

if __name__ == "__main__":
    sys.exit(main())