"""
Server-side RAG evaluation — submit test cases to an evalkit server.

This example demonstrates server-side evaluation using the EvalKitClient SDK:
  1. Create a project on the server
  2. Submit RAG test cases for evaluation (the server runs the LLM judge)
  3. Poll until the evaluation completes
  4. View the results in the web dashboard

The server handles judge configuration and metric scoring — no local LLM calls
needed from the client side.

Requirements:
    pip install evalkit-llm
    # Start the server in another terminal: evalkit serve

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

from __future__ import annotations

import argparse

from evalkit import EvalKitClient

# RAG test cases — these would come from your RAG pipeline in practice.
# Each test case includes the question, retrieved context, generated answer,
# and (optionally) an expected answer for completeness scoring.
TEST_CASES = [
    {
        "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 created by Guido van Rossum.",
    },
    {
        "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 and is one of the fastest Python frameworks.",
        ],
        "answer": "FastAPI is a modern Python web framework for building APIs, known for its speed.",
        "expected_answer": "FastAPI is a modern, fast web framework for building APIs with Python.",
    },
    {
        "question": "What is Pydantic used for?",
        "context": [
            "Pydantic is a data validation library for Python.",
            "It uses Python type annotations to validate and serialize data.",
        ],
        "answer": "Pydantic is used for data validation using Python type annotations.",
        "expected_answer": "Pydantic validates data using Python type annotations.",
    },
    {
        "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.",
        # No expected_answer — completeness metric will be skipped.
        # The answer is NOT supported by the context, so faithfulness and
        # hallucination metrics should flag this test case.
    },
]


def main() -> None:
    parser = argparse.ArgumentParser(description="Server-side RAG evaluation with evalkit")
    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)

    # Show server configuration
    config = client.get_config()
    print(f"Server: {args.server_url}")
    print(f"  Judge:    {config.judge_provider}")
    print(f"  Suite:    {config.suite}")
    print(f"  Suites:   {', '.join(config.available_suites)}")

    # Create a project (or reuse if it already exists)
    project = client.create_project("RAG Evaluation Demo", "Server-side RAG evaluation example")
    print(f"\nProject: {project.name} (id={project.id})")

    # Submit test cases — the server runs the evaluation in the background
    print(f"\nSubmitting {len(TEST_CASES)} test cases for evaluation…")
    evaluation = client.evaluate(project.id, TEST_CASES, suite="rag_qa")
    print(f"  Evaluation queued (id={evaluation.id})")

    # Poll until complete
    print("  Waiting for results", end="", flush=True)
    result = client.wait_for_evaluation(evaluation.id, poll_interval=2.0)
    print()

    if result.status == "failed":
        print("\nEvaluation failed — check server logs for details.")
        return

    # Print results
    print(f"\n{'=' * 60}")
    print("Evaluation complete!")
    print(f"  Suite:         {result.suite_name}")
    print(f"  Average score: {result.average_score:.2%}")
    print("\nView full results in the dashboard:")
    print(f"  {args.server_url}/evaluations/{result.id}")


if __name__ == "__main__":
    main()
