from langchain.prompts import ChatPromptTemplate # type: ignore
from langchain_core.output_parsers import JsonOutputParser # type: ignore
from pydantic import BaseModel, Field # type: ignore
from typing import List, Optional
from Ai_Agents.models.language_model import llm




class IntentResult(BaseModel):
    decision: Optional[str] = Field(description="The decision if intent matches, otherwise null")


def determine_intent(user_input: str, decisions: List[str]) -> Optional[dict]:
    template = """
    You are an experienced intent classifier. Based on the user's input and the available decisions, identify which decision the intent matches or not.
    If no match is found, return "null".
    
    User input: {user_input}
    Decisions: {decisions}
    """
    parser = JsonOutputParser(pydantic_object=IntentResult)
    
    prompt = ChatPromptTemplate.from_template(
        template="{format_instructions}\n" + template
    ).partial(format_instructions=parser.get_format_instructions())
    
    chain = prompt | llm | parser
    response = chain.invoke({"user_input": user_input, "decisions": decisions})
    return response
