import re

import boto3

from turbofuzzllm.utils.cli_utils import cli


def find_whole_word(w):
    return re.compile(r"\b({0})\b".format(w), flags=re.IGNORECASE).search


def is_bedrock_model(model_id):
    session = boto3.Session(profile_name="default", region_name=cli().region)
    client = session.client("bedrock")

    response = client.list_foundation_models()
    model_ids = {summary["modelId"] for summary in response["modelSummaries"]}
    sanitized_model_id = model_id
    if model_id.startswith("us."):
        sanitized_model_id = model_id[3:]
    return sanitized_model_id in model_ids


def is_openai_model(model_id):
    return model_id.startswith("gpt-") or model_id.startswith("o1-")


def is_local_harmbench_model(model_id):
    if cli().sagemaker_role == "-" and cli().judge_tokenizer != "-":
        return model_id.startswith("cais/HarmBench-")
    return False


def is_sagemaker_harmbench_model(model_id):
    if cli().sagemaker_role != "-" and cli().judge_tokenizer == "-":
        return model_id.startswith("cais/HarmBench-")
    return False


def is_sagemaker_jumpstart_llama_guard_model(model_id):
    return model_id.startswith("meta-textgeneration-llama-guard")


def is_sagemaker_jumpstart_model(model_id):
    if model_id.startswith("huggingface-llm-gemma-7b-instruct-finetuned"):
        return False
    return model_id.startswith("huggingface-llm-") or is_sagemaker_jumpstart_llama_guard_model(
        model_id
    )


def is_sagemaker_huggingface_model(model_id):
    if model_id.startswith("huggingface-llm-gemma-7b-instruct-finetuned"):
        return True
    return is_sagemaker_harmbench_model(model_id)
    # return (model_id.startswith("cais/zephyr_7b_r2d2")
    #         or model_id.startswith("HuggingFaceH4/zephyr-7b-beta")
    #         or is_sagemaker_harmbench_model(model_id))


def is_local_model(model_id):
    return (
        model_id.startswith("cais/zephyr_7b_r2d2")
        or model_id.startswith("HuggingFaceH4/zephyr-7b-beta")
        or is_local_harmbench_model(model_id)
        or model_id.startswith("google/gemma-7b")
        or model_id.startswith("google/gemma-2b")
    )
