from typing import Union


def expand_abnormality_questions(
    answer: str, matched_word: str, modality: Union[None, str]
):
    """Create new questions for abnormality with given information.

    Args:
        answer: Original answer to the question
        matched_word: The keyword which labeled the original question as abnormality
        modality: name of the scan type present in original question.

    Returns:
        dict of generated questions including the original question
    """
    binary, categorical = {}, {}
    abnormal_keywords = [
        "abnormal",
        "abnormality",
        "abnormalities",
        "alarming",
        "wrong",
    ]

    if modality == "ct":
        modality = "ct scan"
    if modality == "pet":
        modality = "pet scan"

    if modality != None:
        if answer in ["yes", "no"]:
            if (answer == "no") and (matched_word not in abnormal_keywords):
                flag = "abnormal"
            elif (answer == "no") and (matched_word in abnormal_keywords):
                flag = "normal"
            elif (answer == "yes") and (matched_word not in abnormal_keywords):
                flag = "normal"
            elif (answer == "yes") and (matched_word in abnormal_keywords):
                flag = "abnormal"
            else:
                raise ValueError(
                    "[Error @ `expand_abnormality_questions`] Something wrong with the conditions"
                )
        else:
            flag = "abnormal"
            categorical[f"what is most alarming about this {modality}?"] = answer
            categorical[f"what is most alarming about the {modality}?"] = answer
            categorical[f"what is abnormal in this {modality}?"] = answer
            categorical[f"what is abnormal in the {modality}?"] = answer
            categorical[f"what abnormality is seen in this {modality}?"] = answer
            categorical[f"what abnormality is seen in the {modality}?"] = answer
            categorical[f"what is the primary abnormality in this {modality}?"] = answer
            categorical[f"what is the primary abnormality in the {modality}?"] = answer

        binary[f"is this a normal {modality}?"] = "yes" if flag == "normal" else "no"
        binary[f"is the {modality} normal?"] = "yes" if flag == "normal" else "no"
        binary[f"is this {modality} normal?"] = "yes" if flag == "normal" else "no"
        binary[f"does this {modality} look normal?"] = (
            "yes" if flag == "normal" else "no"
        )
        binary[f"are there abnormalities in this {modality}?"] = (
            "no" if flag == "normal" else "yes"
        )
        binary[f"is there an abnormality in the {modality}?"] = (
            "no" if flag == "normal" else "yes"
        )
        binary[f"is there evidence of any abnormalities?"] = (
            "no" if flag == "normal" else "yes"
        )
        binary[f"is there something wrong in the {modality}?"] = (
            "no" if flag == "normal" else "yes"
        )
    else:
        if answer in ["yes", "no"]:
            if (answer == "no") and (matched_word not in abnormal_keywords):
                flag = "abnormal"
            elif (answer == "no") and (matched_word in abnormal_keywords):
                flag = "normal"
            elif (answer == "yes") and (matched_word not in abnormal_keywords):
                flag = "normal"
            elif (answer == "yes") and (matched_word in abnormal_keywords):
                flag = "abnormal"
            else:
                raise ValueError(
                    "[Error @ `expand_abnormality_questions`] Something wrong with the conditions"
                )
        else:
            flag = "abnormal"
            categorical[f"what is most alarming about this image?"] = answer
            categorical[f"what is most alarming about the image?"] = answer
            categorical[f"what is abnormal in this image?"] = answer
            categorical[f"what is abnormal in the image?"] = answer
            categorical[f"what abnormality is seen in this image?"] = answer
            categorical[f"what abnormality is seen in the image?"] = answer
            categorical[f"what is the primary abnormality in this image?"] = answer
            categorical[f"what is the primary abnormality in the image?"] = answer

        binary[f"is this a normal image?"] = "yes" if flag == "normal" else "no"
        binary[f"is the image normal?"] = "yes" if flag == "normal" else "no"
        binary[f"is this image normal?"] = "yes" if flag == "normal" else "no"
        binary[f"does this image look normal?"] = "yes" if flag == "normal" else "no"
        binary[f"are there abnormalities in this image?"] = (
            "no" if flag == "normal" else "yes"
        )
        binary[f"is there an abnormality in the image?"] = (
            "no" if flag == "normal" else "yes"
        )
        binary[f"is there evidence of any abnormalities?"] = (
            "no" if flag == "normal" else "yes"
        )
        binary[f"is there something wrong in the image?"] = (
            "no" if flag == "normal" else "yes"
        )
    return {"binary": binary, "categorical": categorical}