import os
import KCALLMConst
import json
import KCALLMActivityUtilities
import KCALLMNutritionUtilities
import KCADBProcessorUtilities as KCAProc
import KCALLMArchetypes


#-----------------------------------------------------------------------------------
#                              PROMPT TO PROMPTS
#-----------------------------------------------------------------------------------
def getSplitPrompts(prompt):

    preprompt = 'Here is the sentence to analyze: """' + prompt + '""" \n\
Extract from the sentence into 2 sentences and 2 questions: \n\
- A sentence relative to food, if it exists \n\
- A sentence relative to sport & physical activity, if it exists \n\
- A question relative to food, if this question exists in the sentence \n\
- A question relative to sport & physical activity, if this question exists in the sentence \n\
Each sentence must keep the notion of time or date even after splitting. \n\
Format the result in JSON: {"foodSentence":, "activitySentence":, "foodQuestion":, "activityQuestion":}'

    return preprompt

#-----------------------------------------------------------------------------------
#                              PROMPT TO INTENT
#-----------------------------------------------------------------------------------
def getIntentDetectionPrompt(prompt, intentList):

    slist = json.dumps(intentList)
    preprompt = 'Identify in this list of intents: ' + slist + ', the intents of the prompt: ###' + prompt + '###. \n \
Format the result in JSON format: {"intents": []}.'

    return preprompt

        

#-----------------------------------------------------------------------------------
#                              DATA TO SPEECH
#-----------------------------------------------------------------------------------
def getD2SPreprompt(mapper, intent):

    preprompt = ""
    
    if intent == KCALLMConst.INTENT_IMAGE_TO_FOOD:

        preprompt = "Write a very short & personalized sentence in french with punctuation, to expose the issue, then provide\
the cause of this issue. Finally, provide the reminder. \
Use 'Tu' in the sentence. "

    return preprompt

        

#-----------------------------------------------------------------------------------
#                              SPEECH TO DATA 
#-----------------------------------------------------------------------------------
def getPreprompt(text, mapper, intent, portraitRobot):

    preprompt = ""
    pathMainPrompts = os.path.join(os.getcwd(), "prompts")

    #==================================================================
    #  INTENT TEXT 2 FOOD
    #==================================================================
    if intent == KCALLMConst.INTENT_TEXT_TO_FOOD:

        tab =  mapper['type']
        smap = json.dumps(tab)
        
        #pathPrompt = os.path.join(pathMainPrompts,"food_prompt.txt")
        pathPrompt = os.path.join(pathMainPrompts,"text2food_prompt_RDF.txt")
        preprompt = ""
        with open(pathPrompt, 'r', encoding='utf-8') as f:
            preprompt = f.read()
            f.close()

        farch = KCALLMArchetypes.getFoodArchetype()
        preprompt = preprompt.replace("$TEXT$", text)
        preprompt = preprompt.replace("$ARCH$", farch)

    #==================================================================
    #  INTENT IMAGE 2 FOOD
    #==================================================================
    elif intent == KCALLMConst.INTENT_IMAGE_TO_FOOD:

        tab =  mapper['type']
        smap = json.dumps(tab)
        
        pathPrompt = os.path.join(pathMainPrompts,"image2food_prompt_RDF.txt")
        preprompt = ""
        with open(pathPrompt, 'r', encoding='utf-8') as f:
            preprompt = f.read()
            f.close()

        farch = KCALLMArchetypes.getFoodArchetype()
        preprompt = preprompt.replace("$ARCH$", farch)

    #==================================================================
    #  INTENT NUTRITION QUESTION
    #==================================================================
    elif intent == KCALLMConst.INTENT_NUTRITION_QUESTION:
        
        preprompt =  "Here is all known information: \n\n" + portraitRobot + "\n\n" + \
            'Answer in less than 50 words to this question with a short explanation if needed: "' \
                + text + '"\n" + "Mention the data source in the response if it exists. The answer must be in the same language than the question'
            
    #==================================================================
    #  INTENT TEXT 2 ACTIVITY 
    #==================================================================
    elif intent == KCALLMConst.INTENT_TEXT_TO_ACTIVITY:

        tab =  mapper['name']

        # Level of activity
        levelAct = KCALLMActivityUtilities.getLevelOfActivity()
        slevel = json.dumps(levelAct)

        pathPrompt = os.path.join(pathMainPrompts,"activity_prompt_RDF.txt")
        preprompt = ""
        with open(pathPrompt, 'r', encoding='utf-8') as f:
            preprompt = f.read()
            f.close()
        aarch = KCALLMArchetypes.getActivityArchetype()
        preprompt = preprompt.replace("$TEXT$", text)
        preprompt = preprompt.replace("$ARCH$", aarch)

        # Add mapping
        enumSmap = ''
        for ss in tab:
            nss = KCAProc.setSimpleCamelCaseFormat(ss)
            sss = 'activity:' + nss + ' a activity:EnumActivity ;\n'
            sss += '    rdfs:label "' + ss + '"@fr .\n'  # Needed for Mistral
            enumSmap += sss
        preprompt = preprompt.replace("$SPORT_MAPPING$", enumSmap)

    #==================================================================
    #  INTENT PROFILE
    #==================================================================
    elif intent == KCALLMConst.INTENT_TEXT_TO_PROFILE:

        preprompt = '\
    The sentence I\'m analyzing is: "$TEXT$". \
    Extract all food intolerances if it exists. \
    Map intolerance on [\'lactose\', \'gluten\', \'fruits de mer\']. \
    Extract all food allergies if it exists. \
    Map allergy on [\'fruits de mer\', \'poisson\', \'fruits à coque\']. \
    Extract age if it exists. \
    Identify if he is ecologist and map result in a range between 0 and 3. \
    Identify if he is vegetarian and map result in a range between 0 and 3. \
    List if it exists food list that I do not like, project on \'foodNoLike\' attribute. \
    List if it exists food list that I like, project on \'foodLike\' attribute. \
    Format the result in JSON in an array of tuples {\"ecologist\":,\"vegetarian\":,\"intolerances\":, \"allergies\":, \"age\":, \"foodNoLike\":}.  \
    Remove markups in the response.'

        preprompt = preprompt.replace("$TEXT$", text)


    return preprompt
