# ai.py

import requests
import os
from dotenv import load_dotenv
from personality import load_persona

load_dotenv()
AI_URL = os.getenv("OLLAMA_API_URL")

def get_ai_response(user_prompt):
    persona = load_persona()
    full_prompt = (
        f"You are {persona['name']}.\n"
        f"{persona['prompt_inject']}\n"
        f"Never break character.\n\n"
        f"User: {user_prompt}\n"
        f"{persona['name']}:"
    )

    payload = {
        "model": "mistral:7b",  # adjust if you use a different one later
        "prompt": full_prompt,
        "stream": False
    }

    try:
        response = requests.post(AI_URL, json=payload)
        if response.status_code == 200:
            result = response.json()
            return result.get("response", "[No response]")
        else:
            return f"[Error {response.status_code}] {response.text}"
    except Exception as e:
        return f"[Request failed] {str(e)}"
