import argparse
import sys
import os
from ai_agent_system.core.agent import AIAgent

def main_cli():
    """
    Main command-line interface for the AI Agent.
    """
    # This is to ensure that the package modules can be imported correctly when running as a script
    sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))

    parser = argparse.ArgumentParser(description="AI Agent System")
    parser.add_argument("query", type=str, nargs='?', default=None, help="The user's query for the agent.")
    parser.add_argument("--interactive", "-i", action="store_true", help="Run in interactive mode.")
    parser.add_argument("--model", type=str, default="gpt-4", help="OpenAI model to use.")
    parser.add_argument("--memory-path", type=str, default="./agent_memory", help="Path to persist memory data.")
    parser.add_argument("--no-reasoning", action="store_false", dest="reasoning", help="Disable detailed reasoning display.")
    parser.add_argument("--no-realtime", action="store_false", dest="realtime", help="Disable real-time output.")
    parser.add_argument("--minimal", action="store_true", help="Enable minimal output.")
    parser.add_argument("--typing-delay", type=float, default=0.01, help="Typing delay for typewriter effect.")
    parser.add_argument("--clear-memory", action="store_true", help="Clear agent's memory before starting.")

    args = parser.parse_args()

    agent = AIAgent(
        model_name=args.model,
        memory_persist_path=args.memory_path,
        enable_reasoning_display=args.reasoning,
        realtime_output=args.realtime,
        minimal_output=args.minimal,
        typing_delay=args.typing_delay
    )

    if args.clear_memory:
        agent.clear_memory()
        print("Agent memory has been cleared.")
        return

    if args.interactive:
        print("Starting interactive session... (type 'exit' or 'quit' to end)")
        while True:
            try:
                query = input("> ")
                if query.lower() in ["exit", "quit"]:
                    break
                if query.strip():
                    agent.process_query(query)
            except (KeyboardInterrupt, EOFError):
                break
        print("\nExiting interactive session.")
    elif args.query:
        agent.process_query(args.query)
    else:
        parser.print_help()
        sys.exit(1)

if __name__ == "__main__":
    main_cli() 