import requests
from cmd_habit_tracker.utils.data import DOCUMENTATION
# import os module
import os
from cmd_habit_tracker.db.db_operations import add_habit
from random import randint

# access environment variable
token = os.environ['HUGGING_FACE_TOKEN']

def answer_question_for_app_use(question: str):
    model_id1 = "distilbert/distilbert-base-uncased-distilled-squad"
    model_id = "deepset/roberta-base-squad2"
    model_id2 = "mistralai/Mistral-7B-Instruct-v0.2"
    API_URL = "https://api-inference.huggingface.co/models/" + model_id
    headers = {"Authorization": f"Bearer {token}"}
    payload = {
        "inputs" : {
            "question" : question,
            "context": DOCUMENTATION
        }
    }
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()['answer'] if 'answer' in response.json() else ""

def generate_single_habit(goal: str):
    # https://api-inference.huggingface.co/models/<MODEL_ID>

    model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
    model_id2 = "mistralai/Mistral-7B-Instruct-v0.2"
    API_URL = "https://api-inference.huggingface.co/models/" + model_id2
    headers = {"Authorization": f"Bearer {token}"}
    payload = {
        
        "inputs": f"""
                    My goal: {goal}
                    Suggest me a habit to achieve the previous goal.
                    Write the habit with the following format, you must generate the habit with exact same format: 
                    title: <>, 
                    frequency: <pick one from the following formats: every day, every week, every month, every z days, every z weeks, every z months>, 
                    period:<should represent number of days - must be included - and must be a number>, 
                    target: <in this format: <amount - must be a single word> <metric - must be a single word>>,
                    note: <A short note to make the user more motivated>

                    An example of a well-formated habit (you should suggest the habit with the exact same format):
                    title: Exercise
                    frequency: every day
                    period: 30 days
                    target: 60 minutes
                    note: Prioritize your physical and mental well-being.
                    """
    }


    response = requests.post(API_URL, headers=headers, json=payload)
    suggested_habit = response.json()[0]['generated_text'][len(payload['inputs']):]
    return suggested_habit