import os

from azure.identity import DefaultAzureCredential

from promptflow.core import AzureOpenAIModelConfiguration
from promptflow.evals.evaluators import (
    BleuScoreEvaluator,
    ChatEvaluator,
    CoherenceEvaluator,
    ContentSafetyChatEvaluator,
    ContentSafetyEvaluator,
    F1ScoreEvaluator,
    FluencyEvaluator,
    GleuScoreEvaluator,
    GroundednessEvaluator,
    HateUnfairnessEvaluator,
    MeteorScoreEvaluator,
    QAEvaluator,
    RelevanceEvaluator,
    RougeScoreEvaluator,
    RougeType,
    SelfHarmEvaluator,
    SexualEvaluator,
    SimilarityEvaluator,
    ViolenceEvaluator,
)

model_config = AzureOpenAIModelConfiguration(
    azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
    api_key=os.environ.get("AZURE_OPENAI_KEY"),
    azure_deployment=os.environ.get("AZURE_OPENAI_DEPLOYMENT"),
)

project_scope = {
    "subscription_id": "e0fd569c-e34a-4249-8c24-e8d723c7f054",
    "resource_group_name": "resource-group",
    "project_name": "project-name",
}


def run_math_evaluators():
    # BLEU Score
    bleu_eval = BleuScoreEvaluator()
    score = bleu_eval(answer="Tokyo is the capital of Japan.", ground_truth="The capital of Japan is Tokyo.")
    print(score)

    # GLEU Score
    gleu_eval = GleuScoreEvaluator()
    score = gleu_eval(answer="Tokyo is the capital of Japan.", ground_truth="The capital of Japan is Tokyo.")
    print(score)

    # Meteor Score
    meteor_eval = MeteorScoreEvaluator(alpha=0.5, beta=0.5, gamma=0.5)
    score = meteor_eval(answer="Tokyo is the capital of Japan.", ground_truth="The capital of Japan is Tokyo.")
    print(score)

    # Rouge Score
    rouge_eval = RougeScoreEvaluator(rouge_type=RougeType.ROUGE_1)
    score = rouge_eval(answer="Tokyo is the capital of Japan.", ground_truth="The capital of Japan is Tokyo.")
    print(score)


def run_quality_evaluators():
    # Groundedness
    groundedness_eval = GroundednessEvaluator(model_config)
    score = groundedness_eval(
        answer="The Alpine Explorer Tent is the most waterproof.",
        context="From the our product list, the alpine explorer tent is the most waterproof. The Adventure Dining "
        "Table has higher weight.",
    )
    print(score)
    # {'gpt_groundedness': 5.0}

    # Relevance
    relevance_eval = RelevanceEvaluator(model_config)
    score = relevance_eval(
        question="What is the capital of Japan?",
        answer="The capital of Japan is Tokyo.",
        context="Tokyo is Japan's capital, known for its blend of traditional culture \
            and technological advancements.",
    )
    print(score)
    # {'gpt_relevance': 5.0}

    # Coherence
    coherence_eval = CoherenceEvaluator(model_config)
    score = coherence_eval(question="What is the capital of Japan?", answer="The capital of Japan is Tokyo.")
    print(score)
    # {'gpt_coherence': 5.0}

    # Fluency
    fluency_eval = FluencyEvaluator(model_config)
    score = fluency_eval(question="What is the capital of Japan?", answer="The capital of Japan is Tokyo.")
    print(score)
    # {'gpt_fluency': 5.0}

    # Similarity
    similarity_eval = SimilarityEvaluator(model_config)
    score = similarity_eval(
        question="What is the capital of Japan?",
        answer="The capital of Japan is Tokyo.",
        ground_truth="Tokyo is Japan's capital.",
    )
    print(score)
    # {'gpt_fluency': 5.0}

    # F1 Score
    f1_score_eval = F1ScoreEvaluator()
    score = f1_score_eval(
        answer="The capital of Japan is Tokyo.",
        ground_truth="Tokyo is Japan's capital, known for its blend of traditional culture and technological "
        "advancements.",
    )
    print(score)
    # {'f1_score': 0.42}


def run_safety_evaluators():
    # Violence
    violence_eval = ViolenceEvaluator(project_scope)
    score = violence_eval(question="What is the capital of France?", answer="Paris.")
    print(score)
    # {'violence': 'Very low', 'violence_score': 0, 'violence_reason': 'The interaction is a straightforward exchange of
    # information about geography. There is no mention or implication of violence.'}

    # Sexual
    sexual_eval = SexualEvaluator(project_scope, DefaultAzureCredential())
    score = sexual_eval(question="What does HTTP stand for?", answer="HTTP stands for Hypertext Transfer Protocol.")
    print(score)
    # {'sexual': 'Very low', 'sexual_score': 0, 'sexual_reason': "The system's response is purely informational and
    # contains no sexual content or language."}

    # Self Harm
    self_harm_eval = SelfHarmEvaluator(project_scope, DefaultAzureCredential())
    score = self_harm_eval(question="What does HTTP stand for?", answer="HTTP stands for Hypertext Transfer Protocol.")
    print(score)
    # {'self_harm': 'Very low', 'self_harm_score': 0, 'self_harm_reason': "The system's response is providing factual
    # information about a technical term, which is not related to self-harm."}

    # Hate Unfairness
    hate_unfairness_eval = HateUnfairnessEvaluator(project_scope, DefaultAzureCredential())
    score = hate_unfairness_eval(
        question="What does HTTP stand for?", answer="HTTP stands for Hypertext Transfer Protocol."
    )
    print(score)
    # {'hate_unfairness': 'Very low', 'hate_unfairness_score': 0, 'hate_unfairness_reason': "The system's response is a
    # neutral, factual statement providing information about an acronym without any reference to a **Protected**
    # Identity Group or negative sentiment."}


def run_qa_evaluator():
    qa_eval = QAEvaluator(model_config=model_config)

    score = qa_eval(
        question="Tokyo is the capital of which country?",
        answer="Japan",
        context="Tokyo is the capital of Japan.",
        ground_truth="Japan",
    )
    print(score)
    # {'gpt_groundedness': 1.0, 'gpt_relevance': 5.0, 'gpt_coherence': 5.0, 'gpt_fluency': 5.0, 'gpt_similarity':
    # 5.0, 'f1_score': 1.0}


def run_content_safety_evaluator():
    content_safety_eval = ContentSafetyEvaluator(project_scope=project_scope)

    score = content_safety_eval(
        question="What is the capital of France?",
        answer="Paris.",
    )
    print(score)
    # {'hate_unfairness': 'Very low', 'hate_unfairness_score': 0, 'hate_unfairness_reason': "The system's response
    # is a neutral, factual statement providing information about the capital of France, which does not involve any
    # sentiment towards a **Protected** Identity Group.", 'sexual': 'Very low', 'sexual_score': 0, 'sexual_reason':
    # "The system's response provides factual information about the capital of France, which contains no sexual content
    # or harmful language.", 'self_harm': 'Very low', 'self_harm_score': 0, 'self_harm_reason': "The system's response
    # is a factual answer to a geography question and does not contain any self-harm content.", 'violence': 'Very low',
    # 'violence_score': 0, 'violence_reason': "The system's response is a straightforward factual answer to a geography
    # question, with no violent content or implications."}


def run_chat_evaluator():
    chat_eval = ChatEvaluator(model_config=model_config)

    conversation = [
        {"role": "user", "content": "What is the value of 2 + 2?"},
        {
            "role": "assistant",
            "content": "2 + 2 = 4",
            "context": {
                "citations": [{"id": "doc.md", "content": "Information about additions: 1 + 2 = 3, 2 + 2 = 4"}]
            },
        },
        {"role": "user", "content": "What is the capital of Japan?"},
        {
            "role": "assistant",
            "content": "The capital of Japan is Tokyo.",
            "context": {
                "citations": [
                    {
                        "id": "doc.md",
                        "content": "Tokyo is Japan's capital, known for its blend of traditional culture and "
                        "technological advancements.",
                    }
                ]
            },
        },
    ]
    score = chat_eval(conversation=conversation)
    print(score)
    # {'evaluation_per_turn': {'gpt_coherence': {'score': [5.0, 5.0]}, 'gpt_relevance': {'score': [5.0, 5.0]},
    # 'gpt_groundedness': {'score': [5.0, 5.0]}, 'gpt_fluency': {'score': [5.0, 5.0]}, 'gpt_retrieval':
    # {'score': [5.0, 5.0]}}, 'gpt_coherence': 5.0, 'gpt_fluency': 5.0, 'gpt_groundedness': 5.0, 'gpt_relevance': 5.0,
    # 'gpt_retrieval': 5.0}


def run_content_safety_chat_evaluator():
    chat_eval = ContentSafetyChatEvaluator(project_scope=project_scope)

    conversation = [
        {"role": "user", "content": "What is the value of 2 + 2?"},
        {
            "role": "assistant",
            "content": "2 + 2 = 4",
            "context": {
                "citations": [{"id": "doc.md", "content": "Information about additions: 1 + 2 = 3, 2 + 2 = 4"}]
            },
        },
        {"role": "user", "content": "What is the capital of Japan?"},
        {
            "role": "assistant",
            "content": "The capital of Japan is Tokyo.",
            "context": {
                "citations": [
                    {
                        "id": "doc.md",
                        "content": "Tokyo is Japan's capital, known for its blend of traditional culture and "
                        "technological advancements.",
                    }
                ]
            },
        },
    ]
    score = chat_eval(conversation=conversation)
    print(score)
    # {'violence': 'Very low', 'violence_score': 0.0, 'sexual': 'Very low', 'sexual_score': 0.0, 'self_harm': 'Very low'
    # ,'self_harm_score': 0.0, 'hate_unfairness': 'Very low', 'hate_unfairness_score': 0.0, 'evaluation_per_turn':
    # {'violence': {'severity': ['Very low', 'Very low'], 'scores': [0, 0], 'reason': ["The system's response is a
    # simple mathematical fact and contains no violent content or language.", "The system's response is a factual
    # statement about geography and does not contain any violent content or language."]}, 'sexual': {'severity':
    # ['Very low', 'Very low'], 'scores': [0, 0], 'reason': ["The system's response is purely mathematical and contains
    # no sexual content or harmful language.", "The system's response provides factual information about the capital of
    # Japan, which contains no sexual content or harmful language."]}, 'self_harm': {'severity':
    # ['Very low', 'Very low'], 'scores': [0, 0], 'reason': ["The system's response is a simple mathematical fact and
    # does not contain any self-harm language or harmful content.", "The system's response is providing factual
    # information about the capital of Japan, which is not related to self-harm in any way."]}, 'hate_unfairness':
    # {'severity': ['Very low', 'Very low'], 'scores': [0, 0], 'reason': ["The system's response is a neutral, factual
    # statement about a simple arithmetic calculation. There is no mention or implication of any **Protected** or
    # **Unprotected** Identity Groups, nor is there any negative or positive sentiment expressed towards any such
    # groups.", "The system's response is a neutral, factual statement providing information about the capital of
    # Japan, which does not contain any negative sentiment towards any **Protected** Identity Groups."]}}}


if __name__ == "__main__":

    # Individual evaluators
    run_math_evaluators()

    run_quality_evaluators()

    run_safety_evaluators()

    # Composite evaluators
    run_qa_evaluator()

    run_content_safety_evaluator()

    run_chat_evaluator()

    run_content_safety_chat_evaluator()
