from typing import Union


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

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

    Returns:
        dict of generated questions including the original question
    """
    binary, categorical = {}, {}

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

    if answer in ["yes", "no"]:
        if matched_word == "iv_contrast":
            binary["was iv_contrast given to the patient?"] = answer
        elif matched_word == "gi_contrast":
            binary["was gi_contrast given to the patient?"] = answer

        if ("t1" in matched_word) and answer == "yes":
            binary["is this a t1_weighted image?"] = "yes"
            binary["is this a t2_weighted image?"] = "no"
            binary["is this a flair image?"] = "no"
        if ("t1" in matched_word) and answer == "no":
            binary["is this a t1_weighted image?"] = "no"

        if ("t2" in matched_word) and answer == "yes":
            binary["is this a t1_weighted image?"] = "no"
            binary["is this a t2_weighted image?"] = "yes"
            binary["is this a flair image?"] = "no"
        if ("t2" in matched_word) and answer == "no":
            binary["is this a t2_weighted image?"] = "no"

        if ("flair" in matched_word) and answer == "yes":
            binary["is this a t1_weighted image?"] = "no"
            binary["is this a t2_weighted image?"] = "no"
            binary["is this a flair image?"] = "yes"
        if ("flair" in matched_word) and answer == "no":
            binary["is this a flair image?"] = "no"

        if (matched_word == "contrast") and modality:
            binary[f"is this a noncontrast {modality}?"] = (
                "no" if answer == "yes" else "yes"
            )
            binary[f"was the {modality} taken with contrast?"] = (
                "yes" if answer == "yes" else "no"
            )
        if (matched_word == "noncontrast") and modality:
            binary[f"is this a noncontrast {modality}?"] = (
                "yes" if answer == "yes" else "no"
            )
            binary[f"was the {modality} taken with contrast?"] = (
                "no" if answer == "yes" else "yes"
            )
    else:
        if matched_word == "contrast":
            categorical["what type of contrast did this patient have?"] = answer
        if ("t1" in answer) or ("t2" in answer) or ("flair" in answer):
            categorical["is this a t1_weighted, t2_weighted, or flair image?"] = answer
            categorical["is this image modality t1, t2, or flair?"] = answer
            if "t1" in answer:
                binary["is this a t1_weighted image?"] = "yes"
            elif "t2" in answer:
                binary["is this a t2_weighted image?"] = "yes"
            elif "flair" in answer:
                binary["is this a flair image?"] = "yes"
        else:
            binary["is this a t1_weighted image?"] = "no"
            binary["is this a t2_weighted image?"] = "no"
            binary["is this a flair image?"] = "no"

    return {"binary": binary, "categorical": categorical}