"""Work/Trade Balance Example.

Demonstrates the decision-making between trading and learning
based on economic status.
"""

from openclaw.core.work_trade_balance import WorkTradeBalance, WorkTradeConfig
from openclaw.core.economy import TradingEconomicTracker


def simulate_agent(name: str, balance: float, skill: float, win_rate: float):
    """Simulate an agent with given parameters."""
    tracker = TradingEconomicTracker(agent_id=name, initial_capital=balance)

    config = WorkTradeConfig()

    balance_obj = WorkTradeBalance(
        economic_tracker=tracker,
        config=config
    )

    status = tracker.get_survival_status()
    decision = balance_obj.decide_activity(skill, win_rate)
    intensity = balance_obj.get_trade_intensity(win_rate)

    return {
        'name': name,
        'balance': tracker.balance,
        'status': status,
        'decision': decision.value,
        'position_multiplier': intensity.position_size_multiplier,
        'max_positions': intensity.max_concurrent_positions,
        'risk_per_trade': intensity.risk_per_trade,
    }


def main():
    """Run the work/trade balance example."""
    print("=" * 60)
    print("OpenClaw Trading - Work/Trade Balance Example")
    print("=" * 60)

    # Simulate different economic scenarios
    print("\n1. Different Economic Scenarios:")
    print("-" * 60)

    scenarios = [
        ("Rich Trader", 15000.0, 0.7, 0.65),    # Thriving
        ("Average Trader", 10000.0, 0.5, 0.50),  # Stable
        ("Struggling Trader", 8000.0, 0.4, 0.45), # Struggling
        ("Poor Trader", 3000.0, 0.3, 0.35),      # Critical
    ]

    for name, balance, skill, win_rate in scenarios:
        result = simulate_agent(name, balance, skill, win_rate)

        print(f"\n   {name}:")
        print(f"     Balance: ${result['balance']:,.2f}")
        print(f"     Status: {result['status'].value}")
        print(f"     Decision: {result['decision']}")
        print(f"     Position Size: {result['position_multiplier']:.0%}")
        print(f"     Max Positions: {result['max_positions']}")
        print(f"     Risk/Trade: {result['risk_per_trade']:.1%}")

    # Show decision rules
    print("\n2. Decision Rules by Economic Status:")
    print("-" * 60)
    print("""
   Thriving (>150%):   70% trade, 30% learn  | Max 25% position, 3% risk
   Stable (100-150%):  80% trade, 20% learn  | Max 20% position, 2% risk
   Struggling (50-100%): 90% trade, 10% learn | Max 10% position, 1% risk
   Critical (<50%):    100% minimal trade    | Max 5% position, 0.5% risk
    """)

    # Skill level impact
    print("\n3. Skill Level Impact:")
    print("-" * 60)

    tracker = TradingEconomicTracker(agent_id="skill_test", initial_capital=15000.0)

    for skill in [0.2, 0.5, 0.8]:
        config = WorkTradeConfig()
        balance = WorkTradeBalance(
            economic_tracker=tracker,
            config=config
        )
        decision = balance.decide_activity(skill, 0.5)
        print(f"   Skill {skill:.0%}: Decision = {decision.value}")

    # Win rate impact
    print("\n4. Win Rate Impact:")
    print("-" * 60)

    for win_rate in [0.3, 0.5, 0.7]:
        config = WorkTradeConfig()
        balance = WorkTradeBalance(
            economic_tracker=tracker,
            config=config
        )
        intensity = balance.get_trade_intensity(win_rate)
        print(f"   Win Rate {win_rate:.0%}: "
              f"Position {intensity.position_size_multiplier:.0%}, "
              f"Max Positions {intensity.max_concurrent_positions}")

    print("\n" + "=" * 60)
    print("Work/Trade balance example complete!")
    print("=" * 60)


if __name__ == "__main__":
    main()
