"""Learning System Example.

Demonstrates the agent learning and skill improvement system.
"""

from openclaw.agents.trader import TraderAgent
from openclaw.learning.manager import CourseManager
from openclaw.learning.courses import (
    create_technical_analysis_course,
    create_risk_management_course,
)


def main():
    """Run the learning system example."""
    print("=" * 60)
    print("OpenClaw Trading - Learning System Example")
    print("=" * 60)

    # 1. Create agent
    print("\n1. Creating trading agent...")
    agent = TraderAgent(agent_id="student_001", initial_capital=2000.0)
    print(f"   Agent ID: {agent.agent_id}")
    print(f"   Initial Skill Level: {agent.state.skill_level:.2f}")
    print(f"   Balance: ${agent.balance:,.2f}")

    # 2. Create learning manager
    print("\n2. Creating learning manager...")
    manager = CourseManager(agent=agent)
    print("   Learning manager initialized")

    # 3. Show available courses
    print("\n3. Available Courses:")
    courses = [
        create_technical_analysis_course(),
        create_risk_management_course(),
    ]

    for course in courses:
        print(f"\n   {course.name}")
        print(f"     ID: {course.course_id}")
        print(f"     Duration: {course.duration_days} days")
        print(f"     Cost: ${course.cost:,.2f}")
        if course.effects:
            print(f"     Effect: +{course.effects[0].improvement:.0%} {course.effects[0].skill_type.value}")

    # 4. Check enrollment eligibility
    print("\n4. Checking enrollment eligibility...")
    can_enroll, reason = manager.can_enroll("technical_analysis_101")
    print(f"   Can enroll in 'Technical Analysis': {can_enroll}")
    if not can_enroll:
        print(f"   Reason: {reason}")

    # 5. Enroll in course
    print("\n5. Enrolling in course...")
    success, message = manager.enroll("technical_analysis_101")
    print(f"   Success: {success}")
    print(f"   Message: {message}")

    if success:
        # 6. Check if learning
        print("\n6. Checking learning status...")
        is_learning = manager.is_learning()
        print(f"   Is Learning: {is_learning}")

        # 7. Get current course
        print("\n7. Current course progress:")
        current = manager.get_current_learning()
        if current:
            print(f"   Course: {current.course_id}")
            print(f"   Status: {current.status.value}")
            print(f"   Progress: {current.progress_percent:.1f}%")

        # 8. Simulate progress
        print("\n8. Simulating learning progress...")
        for progress in [25, 50, 75, 100]:
            manager.update_progress("technical_analysis_101", progress)
            print(f"   Progress: {progress}%")

        # 9. Check skill levels
        print("\n9. Current skill levels:")
        for skill, level in manager.skill_levels.items():
            print(f"   {skill.value}: {level:.2f}")

        # 10. Get learning history
        print("\n10. Learning history:")
        history = manager.learning_history
        print(f"    Courses completed: {len(history.completed_courses)}")
        print(f"    Total spent: ${history.total_spent:,.2f}")

    print("\n" + "=" * 60)
    print("Learning system example complete!")
    print("=" * 60)


if __name__ == "__main__":
    main()
