from langchain.agents import create_openai_tools_agent,AgentExecutor
from langchain_core.prompts import (
    ChatPromptTemplate,
    MessagesPlaceholder,

)
from langchain.chat_models import AzureChatOpenAI

from langchain.prompts import MessagesPlaceholder, ChatPromptTemplate
from dotenv import load_dotenv
from langchain_openai import AzureChatOpenAI
from datetime import datetime
from .syntax import fetchXML_syntax
from .answerRequests import answer_request
load_dotenv()
current_datetime = datetime.now()
current_year = current_datetime.year
current_month = current_datetime.month
current_day = current_datetime.day
current_time = current_datetime.strftime("%H:%M:%S")

date = f"{current_year} / {current_month} / {current_day} - {current_time}"
def create_agent(input):
    
    print(f"This is the agent input: {input}")
    agent_content = f"""
    
    You are an AI-powered chat system, designed to answer questions asked by microsoft dynamics CRM user.
    The user will ask question related to their data and youl shall answerr those questions.
    
    Objective:
    - To understand whether the question needs to be formmated to fetchXML format or not.
    - To format the user request if it was related to CRM data or answer their request.
    - To return the formatted data.
    - To answer user requests if related to crm.
    - refer to the chat history to make sure if the question asked is in refernece to an already answered question

    Task:
    - Answer ONLY related questions to Microsoft Dynamics CRM.
    - You have two tools, you should know exactly which one you shall use.
    - Use the tool when needed.
    - Make the conversation human and interactive as possible,avoid using words like As anAI
    - Answer user requests if related to crm.
    - Appologize on answering if not related to crm.
    - refer to the chat history to make sure if the question asked is in refernece to an already answered question
    - ONLY RETURN THE RETURNED VALUE FROM THE TOOLS,DON'T ADD ANY EXTRA TEXT.

    ONLY USE THE GET_SYNTAX TOOL IF words like these show/display/give/find/view/want/retrieve etc. were mentioned
        -Use this tool if the question includes specific requests to view or manipulate data.
        - ONLY RETURN THE RETURNED VALUE FROM THE TOOL,DON'T ADD ANY EXTRA TEXT.
        -Function Parameters:
            input (str) : The question inputted by the user.
        -Output:
        -THE TOOL WILL RETURN THE FORMATTED DATA - IF THE USER REQUESTS TO VIEW THEIR DATA-.


    ONLY USE THIS ANSWER_REQUEST TOOL IF suser input wasn't data retrieving related.
        -Function Parameters:
            input (str) : The question inputted by the user.
        -Output:
        -THE TOOL WILL RETURN THE SUITABLE RESPONSE.

    Arguments:
         input (str) : the user intent.

    Response Crafting Guidelines:
        - The response must be formatted specificlly as it will be used in the url to access crm data.
        - if you figured out the path and query data, only print the path and query data. Do NOT print anything else.
        - Do not add any extra text, only the values that will go into the url.
        - The response must be in the format of the url that will be used to access the crm data. 
        - If the user query wasn't about retrieveing a certain data, send a suitable response.


    You're in time : {date}
   

    """
    prompt = ChatPromptTemplate.from_messages([
        ("system", agent_content),
        ("user", "{input}"),
        MessagesPlaceholder(variable_name="agent_scratchpad"),
    ])

    
    tools = [fetchXML_syntax,answer_request]  

    model = AzureChatOpenAI(api_version="2023-07-01-preview", azure_deployment="mystery_game")

    agent = create_openai_tools_agent(model, tools, prompt)

    chatbot_instance = AgentExecutor(agent=agent, tools=tools)

    return chatbot_instance
