"""
run_evaluation.py — Evaluate AI Model Outputs with Azure AI Evaluation
=======================================================================
Runs built-in quality evaluators against a set of question-answer pairs:
  - Groundedness  : Is the answer grounded in the provided context?
  - Relevance     : Is the answer relevant to the question?
  - Coherence     : Is the answer coherent and well-structured?
  - Fluency       : Is the answer grammatically correct and fluent?

Prerequisites:
    pip install azure-ai-evaluation azure-identity openai python-dotenv rich

Usage:
    python run_evaluation.py
"""

import os
import sys
from dotenv import load_dotenv
from azure.ai.evaluation import (
    GroundednessEvaluator,
    RelevanceEvaluator,
    CoherenceEvaluator,
    FluencyEvaluator,
    evaluate,
)
from rich.console import Console
from rich.table import Table

load_dotenv()
console = Console()


def get_env(key: str) -> str:
    value = os.getenv(key)
    if not value:
        console.print(f"[red]ERROR:[/red] '{key}' not set. See .env.example.")
        sys.exit(1)
    return value


# ---------------------------------------------------------------------------
# Sample evaluation dataset
# ---------------------------------------------------------------------------
EVAL_DATA = [
    {
        "query": "What is Azure AI Foundry?",
        "context": (
            "Azure AI Foundry is a unified platform for building, evaluating, and "
            "deploying generative AI solutions. It provides a hub-and-project model "
            "for organizing resources and collaborating across teams."
        ),
        "response": (
            "Azure AI Foundry is Microsoft's unified platform for building and "
            "deploying generative AI applications. It uses a hub-and-project structure "
            "to help teams collaborate and share resources efficiently."
        ),
    },
    {
        "query": "How does RAG work?",
        "context": (
            "Retrieval-Augmented Generation (RAG) combines a retrieval step, where "
            "relevant documents are fetched from a knowledge base, with a generation "
            "step, where an LLM uses the retrieved context to produce a grounded answer."
        ),
        "response": (
            "RAG works by first retrieving relevant documents from a knowledge base "
            "using search (keyword, vector, or hybrid), then passing those documents "
            "as context to a large language model which generates a grounded answer."
        ),
    },
    {
        "query": "What evaluators does Azure AI Foundry provide?",
        "context": (
            "Azure AI Foundry offers built-in evaluators including groundedness, "
            "relevance, coherence, fluency, and similarity metrics for assessing "
            "generative AI quality."
        ),
        "response": (
            "Azure AI Foundry provides evaluators for groundedness, relevance, "
            "coherence, fluency, and similarity to systematically measure the "
            "quality of AI-generated content."
        ),
    },
]


def main() -> None:
    """Run evaluators against the sample dataset and display results."""

    # --- Model configuration for evaluators ----------------------------------
    model_config = {
        "azure_endpoint": get_env("AZURE_OPENAI_ENDPOINT"),
        "api_key": get_env("AZURE_OPENAI_API_KEY"),
        "azure_deployment": get_env("AZURE_OPENAI_CHAT_DEPLOYMENT"),
        "api_version": get_env("AZURE_OPENAI_API_VERSION"),
    }

    console.print("[bold cyan]🔬 Running AI Evaluations...[/bold cyan]\n")

    # --- Initialize evaluators -----------------------------------------------
    groundedness = GroundednessEvaluator(model_config)
    relevance = RelevanceEvaluator(model_config)
    coherence = CoherenceEvaluator(model_config)
    fluency = FluencyEvaluator(model_config)

    # --- Evaluate each sample ------------------------------------------------
    results = []
    for i, sample in enumerate(EVAL_DATA, 1):
        console.print(f"Evaluating sample {i}/{len(EVAL_DATA)}: {sample['query'][:50]}...")

        g_score = groundedness(
            query=sample["query"],
            context=sample["context"],
            response=sample["response"],
        )
        r_score = relevance(
            query=sample["query"],
            context=sample["context"],
            response=sample["response"],
        )
        c_score = coherence(
            query=sample["query"],
            response=sample["response"],
        )
        f_score = fluency(
            query=sample["query"],
            response=sample["response"],
        )

        results.append({
            "query": sample["query"],
            "groundedness": g_score.get("groundedness", "N/A"),
            "relevance": r_score.get("relevance", "N/A"),
            "coherence": c_score.get("coherence", "N/A"),
            "fluency": f_score.get("fluency", "N/A"),
        })

    # --- Display results table -----------------------------------------------
    table = Table(title="Evaluation Results", show_lines=True)
    table.add_column("Query", style="cyan", max_width=40)
    table.add_column("Groundedness", justify="center")
    table.add_column("Relevance", justify="center")
    table.add_column("Coherence", justify="center")
    table.add_column("Fluency", justify="center")

    for r in results:
        table.add_row(
            r["query"],
            str(r["groundedness"]),
            str(r["relevance"]),
            str(r["coherence"]),
            str(r["fluency"]),
        )

    console.print()
    console.print(table)

    # --- Summary -------------------------------------------------------------
    console.print("\n[bold green]✅ Evaluation complete![/bold green]")
    console.print(
        "Scores are on a 1-5 scale. Higher is better.\n"
        "Use these metrics to compare prompt variations, model versions, "
        "or RAG pipeline configurations."
    )


if __name__ == "__main__":
    main()
