import multiprocessing
from typing import Callable

import openai
import pandas as pd
import tqdm


def run_AIM_jailbreak(prompt: str, model: str):
    """
    Run AIM jailbreak on a single prompt.

    Args:
    prompt: str
        The prompt to run the jailbreak on.
    model: str
        The model to use for the jailbreak.

    Returns:
    str
        The response from the model.
    """
    with open("strongreject/AIM.txt", "r") as f:
        AIM_txt = f.read()
    jailbreak_prompt = AIM_txt.replace("[INSERT PROMPT HERE]", prompt)
    with openai.OpenAI() as client:
        completion = client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": jailbreak_prompt}],
            temperature=0.0,
            max_tokens=1000,
        )
        model_output = completion.choices[0].message.content
    return model_output


def run_jb_on_dataset(
    jb: Callable, dataset: pd.DataFrame, model: str = "gpt-3.5-turbo-0613"
):
    """
    Run a jailbreak on a dataset.

    Args:
    jb: Callable
        The jailbreak function to use.
    dataset: pd.DataFrame
        The dataset to run the jailbreak on. Has to have a column called 'forbidden_prompt'.
    model: str
        The model to use for the jailbreak.

    Returns:
    pd.DataFrame
        The dataset with the responses from the jailbreak. The responses are stored in a column called 'response'.
    """

    prompts = dataset["forbidden_prompt"]
    with multiprocessing.Pool() as pool:
        results = list(
            tqdm.tqdm(
                pool.starmap(jb, zip(prompts, [model] * len(prompts))),
                total=len(prompts),
            )
        )
    dataset["response"] = results
    return dataset
