import os
import openai
from dotenv import load_dotenv

load_dotenv()

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

# ─────────────────────────────────────────────
# CONFIGURE YOUR BOT HERE
# ─────────────────────────────────────────────
BOT_NAME = "My AI Bot"
SYSTEM_PROMPT = """You are a helpful assistant called {bot_name}.
Your role is to assist users with their questions clearly and concisely.
Always be friendly, professional, and accurate.
If you don't know something, say so honestly.

Customise this prompt to match your bot's purpose — the more specific you are,
the better the bot will perform.
""".format(bot_name=BOT_NAME)
# ─────────────────────────────────────────────

def chat(conversation_history: list, user_message: str) -> str:
    """Send a message and get a reply."""
    conversation_history.append({"role": "user", "content": user_message})

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "system", "content": SYSTEM_PROMPT}] + conversation_history,
        temperature=0.7,
    )

    assistant_message = response.choices[0].message.content
    conversation_history.append({"role": "assistant", "content": assistant_message})
    return assistant_message


def main():
    print(f"═══════════════════════════════════")
    print(f"  {BOT_NAME} — Powered by RoboSmith")
    print(f"═══════════════════════════════════")
    print("Type 'exit' to quit.\n")

    conversation_history = []

    while True:
        user_input = input("You: ").strip()
        if not user_input:
            continue
        if user_input.lower() in ("exit", "quit", "bye"):
            print(f"\n{BOT_NAME}: Goodbye! Come back anytime.")
            break

        reply = chat(conversation_history, user_input)
        print(f"\n{BOT_NAME}: {reply}\n")


if __name__ == "__main__":
    main()
