"""
Complexity Score Test
"""

import textstat


def extract_submetrics(text):
    """
    Extracts various submetrics from the given text.

    Args:
        text (str): The input text.

    Returns:
        dict: A dictionary containing various submetrics.
    """
    return {
        "lexicon_count": textstat.lexicon_count(text),
        "syllable_count": textstat.syllable_count(text),
        "sentence_count": textstat.sentence_count(text),
        "char_count": textstat.char_count(text),
        "letter_count": textstat.letter_count(text),
        "polysyllabcount": textstat.polysyllabcount(text),
        "monosyllabcount": textstat.monosyllabcount(text),
        "difficult_words": textstat.difficult_words(text),
        "coleman_liau_index": textstat.coleman_liau_index(text),
    }


# this is using extra metrics for calculation: lexicon_count, syllable_count, sentence_count, character_count, letter_count, polysylable_count,
# monosyllablecount, difficult_words, coleman_liau_index
def complexity_test(prompt, response=None, threshold=11):
    """
    Provides the complexity score.

    Args:
        prompt (str): The prompt given to the model.
        response (str, optional): The response generated by the model.
        threshold (float): The threshold for complexity score (default is 11).

    Returns:
        dict: A dictionary containing the prompt, response, prompt complexity score,
              response complexity score, prompt submetrics, response submetrics,
              is_passed, and threshold.
    """
    prompt_submetrics = extract_submetrics(prompt)
    total_metrics = len(prompt_submetrics)
    prompt_complexity_score = sum(prompt_submetrics.values()) / total_metrics
    prompt_is_complex = "Pass" if prompt_complexity_score > threshold else "Fail"

    if response is not None:
        response_submetrics = extract_submetrics(response)
        response_complexity_score = sum(response_submetrics.values()) / total_metrics
        response_is_complex = (
            "Pass" if response_complexity_score < threshold else "Fail"
        )
    else:
        response_submetrics = None
        response_complexity_score = None
        response_is_complex = None

    result = {
        "prompt": prompt,
        "response": response,
        "score": {
            "prompt_complexity_score": prompt_complexity_score,
            "response_complexity_score": response_complexity_score,
        },
        "is_passed": {
            "prompt_is_complex": prompt_is_complex,
            "response_is_complex": response_is_complex,
        },
        "prompt_submetrics": prompt_submetrics,
        "response_submetrics": response_submetrics,
        "threshold": threshold,
    }
    return result
