# test_apis.py (run this standalone — tests Gemini, Tavily, ElevenLabs)
import os
from dotenv import load_dotenv
load_dotenv()

# ─── Test 1: Gemini ───
print("Testing Gemini...")
import google.generativeai as genai
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
model = genai.GenerativeModel(
    model_name="gemini-3-flash-preview",
    generation_config=genai.GenerationConfig(
        response_mime_type="application/json",
        temperature=0.7,
    )
)
response = model.generate_content(
    'Return JSON: {"status": "ok", "message": "Gemini works"}'
)
print(f"  Gemini: {response.text}")

# ─── Test 2: Tavily ───
print("\nTesting Tavily...")
from tavily import TavilyClient
tavily = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
result = tavily.search("best Thai restaurants in Bandra Mumbai", include_answer=True, max_results=2)
print(f"  Tavily answer: {result.get('answer', 'No answer')[:150]}")
print(f"  Results count: {len(result.get('results', []))}")

# ─── Test 3: ElevenLabs ───
print("\nTesting ElevenLabs...")
import httpx
resp = httpx.post(
    "https://api.elevenlabs.io/v1/text-to-speech/ZthjuvLPty3kTMaNKVKb",
    headers={"xi-api-key": os.getenv("ELEVENLABS_API_KEY"), "Content-Type": "application/json"},
    json={"text": "Hello from negoT8!", "model_id": "eleven_flash_v2_5",
          "voice_settings": {"stability": 0.5, "similarity_boost": 0.75}},
    timeout=15.0
)
if resp.status_code == 200:
    with open("test_voice.mp3", "wb") as f:
        f.write(resp.content)
    print(f"  ElevenLabs: ✅ Saved test_voice.mp3 ({len(resp.content)} bytes)")
else:
    print(f"  ElevenLabs: ❌ {resp.status_code} — {resp.text[:200]}")

print("\n✅ All API tests complete!")
