"""
Combined RAG + agent evaluation — server-side via EvalKitClient.

Submits both RAG and agent test cases to the evalkit server in a single
evaluation. The server runs the judge — no local LLM calls needed.

Requirements:
    pip install evalkit-llm
    export OPENAI_API_KEY=sk-...   # or configure another judge on the server
    # Start the server in another terminal: evalkit serve

Usage:
    python examples/server_combined.py
    python examples/server_combined.py http://my-server:9000   # custom server URL
"""

from __future__ import annotations

import argparse

from evalkit import EvalKitClient, TestCase, ToolCall

# RAG test cases: question + context + answer
RAG_TEST_CASES = [
    TestCase(
        question="What is Python?",
        context=[
            "Python is a high-level, general-purpose programming language.",
            "It was created by Guido van Rossum and first released in 1991.",
        ],
        answer="Python is a high-level programming language created by Guido van Rossum.",
        expected_answer="Python is a high-level, general-purpose programming language.",
    ),
    TestCase(
        question="What is FastAPI?",
        context=[
            "FastAPI is a modern web framework for building APIs with Python 3.8+.",
            "It is based on standard Python type hints.",
        ],
        answer="FastAPI is a modern Python web framework for building APIs.",
        expected_answer="FastAPI is a modern, fast web framework for building APIs with Python.",
    ),
    TestCase(
        question="Who created Linux?",
        context=[
            "Python is a high-level programming language.",
            "JavaScript is used for web development.",
        ],
        answer="Linux was created by Linus Torvalds in 1991.",
        # Answer is NOT supported by context — faithfulness should fail.
    ),
]

# Agent test cases: expected vs actual tool calls
AGENT_TEST_CASES = [
    TestCase(
        question="What is the weather like in Paris?",
        expected_tool_calls=[ToolCall(name="get_weather", parameters={"city": "Paris"})],
        actual_tool_calls=[ToolCall(name="get_weather", parameters={"city": "Paris"})],
    ),
    TestCase(
        question="Search for the latest Python news.",
        expected_tool_calls=[ToolCall(name="search_web", parameters={"query": "latest Python news"})],
        actual_tool_calls=[ToolCall(name="search_web", parameters={"query": "Python news 2025"})],
    ),
]

ALL_TEST_CASES = RAG_TEST_CASES + AGENT_TEST_CASES


def main() -> None:
    parser = argparse.ArgumentParser(description="Combined RAG + agent evaluation with evalkit server")
    parser.add_argument(
        "server_url",
        nargs="?",
        default="http://localhost:8000",
        help="evalkit server URL (default: http://localhost:8000)",
    )
    args = parser.parse_args()

    client = EvalKitClient(args.server_url)
    config = client.get_config()
    print(f"Server: {args.server_url}")
    print(f"  Judge: {config.judge_provider} | Suite: {config.suite}\n")

    project = client.create_project("Combined Demo")
    print(f"Project: {project.name} (id={project.id})")

    # Submit all test cases in one evaluation
    print(f"\nSubmitting {len(ALL_TEST_CASES)} test cases ({len(RAG_TEST_CASES)} RAG + {len(AGENT_TEST_CASES)} agent)…")
    evaluation = client.evaluate(project.id, ALL_TEST_CASES)
    print(f"  Evaluation queued (id={evaluation.id})")

    print("  Waiting for results…")
    result = client.wait_for_evaluation(evaluation.id, poll_interval=2.0)

    if result.status == "failed":
        print("  Evaluation failed — check server logs.")
        return

    print("\nEvaluation complete!")
    print(f"  Average score: {result.average_score:.2%}")
    print(f"  Dashboard: {args.server_url}/evaluations/{result.id}")


if __name__ == "__main__":
    main()
