
import os
import requests

API_URL = "https://openrouter.ai/api/v1/chat/completions"
MODEL = "xiaomi/mimo-v2-flash:free"

SYSTEM_PROMPT = """
You are M2H AI, created by M2H Web Solution.
Expert in Web Dev, APIs, Cyber Security, Automation.
"""

def ask_ai(prompt):
    api_key = os.getenv("OPENROUTER_API_KEY")
    if not api_key:
        return "OPENROUTER_API_KEY not set"

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    payload = {
        "model": MODEL,
        "messages": [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": prompt}
        ]
    }

    r = requests.post(API_URL, headers=headers, json=payload, timeout=60)
    return r.json()["choices"][0]["message"]["content"]
