#!/usr/bin/env python
from __future__ import annotations

import asyncio
import json
import sys
from typing import Any, Dict

from evenage.core import AgentRunner, EvenAgeConfig


def run() -> None:
    inputs: Dict[str, Any] = {}
    if len(sys.argv) > 2:
        try:
            inputs = json.loads(sys.argv[2])
        except Exception:
            inputs = {}
    runner = AgentRunner(env_config=EvenAgeConfig())
    result = asyncio.run(runner.run(inputs, agent_name="{{ default_agent | snake_case }}"))
    print("\n\n=== FINAL OUTPUT ===\n")
    try:
        print(json.dumps(result, indent=2, ensure_ascii=False))
    except Exception:
        print(result)


def replay() -> None:
    if len(sys.argv) < 3:
        print("Usage: python main.py replay <job_id>")
        sys.exit(1)
    runner = AgentRunner(env_config=EvenAgeConfig())
    result = runner.replay(sys.argv[2], reexecute=False)
    print(json.dumps(result, indent=2, ensure_ascii=False))


if __name__ == "__main__":
    command = sys.argv[1] if len(sys.argv) > 1 else "run"
    if command == "run":
        run()
    elif command == "replay":
        replay()
    else:
        print(f"Unknown command: {command}")
