"""本地评测 SDK 使用示例"""

from evaluation.local import LocalEvaluator, TerminalVisualizer, LLMConfig


def create_llm_config() -> LLMConfig:
    """创建 LLM 配置（用户自己在调用端配置）"""
    # 方式1: 从 .env 文件读取
    from dotenv import load_dotenv
    import os

    load_dotenv()

    return LLMConfig(
        model=os.getenv("LLM_MODEL", "deepseek-chat"),
        base_url=os.getenv("LLM_BASE_URL", "https://api.deepseek.com"),
        api_key=os.getenv("LLM_API_KEY", "EMPTY"),
        # extra_body={"enable_thinking": False},
    )


def basic_example():
    """基础用法示例"""
    print("=" * 50)
    print("基础用法示例")
    print("=" * 50)

    # 用户自己配置 LLM
    llm_config = create_llm_config()

    # 创建评测器
    evaluator = LocalEvaluator(
        problem_path="problems/maze",
        user_code_path="answer/maze_answer/solution.py",
        llm_config=llm_config,
    )

    # 运行评测
    result = evaluator.evaluate()

    # 打印结果
    print(f"状态: {result.status}")
    print(f"通过: {result.passed_cases}/{result.total_cases}")
    print(f"得分: {result.score}")
    print(f"耗时: {result.total_time}ms")


def stream_example():
    """流式评测示例（带可视化）"""
    print("\n" + "=" * 50)
    print("流式评测示例")
    print("=" * 50)

    llm_config = create_llm_config()

    evaluator = LocalEvaluator(
        problem_path="problems/maze",
        user_code_path="answer/maze_answer/solution.py",
        llm_config=llm_config,
    )

    # 创建终端可视化器
    visualizer = TerminalVisualizer(
        show_oa_sequence=True,
        show_user_logs=True,
        show_progress=True,
    )

    # 流式运行并可视化
    for event in evaluator.evaluate_stream():
        visualizer.on_event(event)

    print("\n评测完成!")


def custom_callback_example():
    """自定义回调示例"""
    print("\n" + "=" * 50)
    print("自定义回调示例")
    print("=" * 50)

    from evaluation.local import EvalEvent, EvalEventType

    oa_history = []
    user_logs = []

    def my_callback(event: EvalEvent) -> None:
        """自定义事件处理

        Args:
            event: 评测事件对象，包含 type, case_index, data 等字段
        """
        if event.type == EvalEventType.OBSERVATION:
            oa_history.append({"type": "obs", "data": event.data})
            print(f"[OBS] Case {event.case_index}: {event.data.get('content', '')[:50]}...")
        elif event.type == EvalEventType.ACTION:
            oa_history.append({"type": "action", "data": event.data})
            print(f"[ACT] Case {event.case_index}: {event.data.get('content', '')[:50]}...")
        elif event.type == EvalEventType.USER_LOG:
            user_logs.append(event.data.get("content", ""))

    llm_config = create_llm_config()

    evaluator = LocalEvaluator(
        problem_path="problems/maze",
        user_code_path="answer/maze_answer/solution.py",
        llm_config=llm_config,
        on_event=my_callback,
    )

    evaluator.evaluate()

    # 事后分析
    print(f"\n评测完成!")
    print(f"OA 交互次数: {len(oa_history)}")
    print(f"用户日志行数: {len(user_logs)}")


def partial_eval_example():
    """部分评测示例（只跑指定 case）"""
    print("\n" + "=" * 50)
    print("部分评测示例（只跑前 2 个 case）")
    print("=" * 50)

    llm_config = create_llm_config()

    evaluator = LocalEvaluator(
        problem_path="problems/maze",
        user_code_path="answer/maze_answer/solution.py",
        llm_config=llm_config,
    )

    # 只跑第 0, 1 两个 case
    result = evaluator.evaluate(case_indices=[0, 1])

    print(f"运行了 {result.total_cases} 个 case")
    print(f"通过: {result.passed_cases}")


def judge_log_example():
    """捕获 Judge 沙箱日志示例（用于调试评测逻辑）"""
    print("\n" + "=" * 50)
    print("捕获 Judge 沙箱日志示例")
    print("=" * 50)

    from evaluation.local import EvalEvent, EvalEventType

    judge_logs = []

    def my_callback(event: EvalEvent) -> None:
        """捕获所有事件类型

        Args:
            event: 评测事件对象，包含 type, case_index, timestamp, data 字段
        """
        if event.type == EvalEventType.OBSERVATION:
            print(f"[OBS] Case {event.case_index}")
        elif event.type == EvalEventType.ACTION:
            print(f"[ACT] Case {event.case_index}")
        elif event.type == EvalEventType.USER_LOG:
            print(f"[USER LOG] {event.data.get('content', '')[:100]}...")
        elif event.type == EvalEventType.JUDGE_LOG:
            # 捕获 Judge 沙箱的输出（评测逻辑日志）
            log_content = event.data.get("content", "")
            judge_logs.append(log_content)
            print(f"[JUDGE LOG] {log_content[:100]}...")

    llm_config = create_llm_config()

    evaluator = LocalEvaluator(
        problem_path="problems/maze",
        user_code_path="answer/maze_answer/solution.py",
        llm_config=llm_config,
        on_event=my_callback,
    )

    # 使用 TerminalVisualizer 也可以显示 Judge 日志
    # visualizer = TerminalVisualizer(
    #     show_oa_sequence=True,
    #     show_judge_logs=True,  # 显示 Judge 日志
    #     show_user_logs=True,
    # )

    evaluator.evaluate()

    print(f"\n评测完成!")
    print(f"Judge 日志条数: {len(judge_logs)}")


if __name__ == "__main__":
    # 运行示例（实际运行需要 Docker 和 LLM API）
    basic_example()
    # stream_example()
    # custom_callback_example()
    # partial_eval_example()
    # judge_log_example()  # 捕获 Judge 沙箱日志

    print("""
本地评测 SDK 使用说明:

1. 确保已安装依赖:
   uv pip install python-dotenv

2. 创建 .env 文件（可选，也可以直接传参）:
   LLM_MODEL=deepseek-chat
   LLM_BASE_URL=https://api.deepseek.com
   LLM_API_KEY=your-api-key

3. 确保 Docker 正在运行

4. 运行示例:
   uv run python -m evaluation.local.example

API 设计原则:
- LLMConfig 由用户自己创建和传入，SDK 不强制读取 .env
- 用户完全控制配置，包括 extra_body 等高级参数
- SDK 只负责评测逻辑，不关心配置来源
""")
