# chatbot/responder.py (OpenAI version)
import os
import openai
from dotenv import load_dotenv

load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
client = openai.OpenAI(api_key=openai_api_key)

CHAT_MODEL = "gpt-3.5-turbo"

PROMPT_TEMPLATE = (
    "You are a helpful AI assistant for the Axiom Trade website. "
    "Your goal is to provide polite and fantastic answers based only on the provided website data. "
    "If the question is about payments or pricing, politely direct the user to 'contact our manager or post a query' for more information. "
    "If a question is unrelated to Axiom Trade, kindly state that you can only answer questions about the website. "
    "Always maintain a professional, helpful, and knowledgeable tone"
    "only answer for those services , products  or any other details which are in axiom, no other related services or produst question should be answered"
    "Please keep your answers between 50 and 60 words.\n\n"
    "Website Data:\n{context}\n\n"
    "Question: {question}\n\n"
    "Answer:"
)

def generate_answer(query: str, context_chunks):
    context = "\n---\n".join(context_chunks[:3])
    prompt = PROMPT_TEMPLATE.format(context=context, question=query)

    response = client.chat.completions.create(
        model=CHAT_MODEL,
        messages=[{"role": "user", "content": prompt}],
        max_tokens=100,
        temperature=0.2
    )

    return response.choices[0].message.content.strip()