#!/usr/bin/env python3
"""
Test if the problem is the system prompt content.
"""

import os
import sys
sys.path.append(os.getcwd())

from health.utils.env_loader import load_env_with_extras
from openai import OpenAI
from slack_bot.llm.gemini import get_system_instruction

load_env_with_extras()

api_key = os.environ.get("GEMINI_API_KEY")
base_url = os.environ.get("GEMINI_BASE_URL")
model = os.environ.get("GEMINI_MODEL", "gemini-3-flash")

client = OpenAI(api_key=api_key, base_url=f"{base_url}/v1")

# Simple tool
simple_tool = {
    "type": "function",
    "function": {
        "name": "get_health_data",
        "description": "Get health data",
        "parameters": {
            "type": "object",
            "properties": {"date": {"type": "string"}},
            "required": ["date"]
        }
    }
}

print("=" * 60)
print("Testing System Prompt Impact")
print("=" * 60)

# Test 1: Simple system prompt + tool
print("\nTest 1: Simple system prompt + 1 tool")
print("-" * 60)
try:
    response1 = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "今天睡得怎么样？"}
        ],
        tools=[simple_tool],
        tool_choice="auto"
    )
    content1 = response1.choices[0].message.content or ""
    has_tools1 = hasattr(response1.choices[0].message, 'tool_calls') and response1.choices[0].message.tool_calls
    print(f"{'✅ WORKS' if content1 or has_tools1 else '❌ EMPTY'}")
    print(f"   Response: {len(content1)} chars, Tool calls: {has_tools1}")
except Exception as e:
    print(f"❌ Error: {e}")

# Test 2: Full system prompt (with TOOL-FIRST POLICY) + tool
print("\nTest 2: Full health system prompt + 1 tool")
print("-" * 60)
full_prompt = get_system_instruction()
try:
    response2 = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": full_prompt},
            {"role": "user", "content": "今天睡得怎么样？"}
        ],
        tools=[simple_tool],
        tool_choice="auto"
    )
    content2 = response2.choices[0].message.content or ""
    has_tools2 = hasattr(response2.choices[0].message, 'tool_calls') and response2.choices[0].message.tool_calls
    print(f"{'✅ WORKS' if content2 or has_tools2 else '❌ EMPTY'}")
    print(f"   Response: {len(content2)} chars, Tool calls: {has_tools2}")
except Exception as e:
    print(f"❌ Error: {e}")

# Test 3: Full system prompt WITHOUT tools
print("\nTest 3: Full health system prompt, NO tools")
print("-" * 60)
try:
    response3 = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": full_prompt},
            {"role": "user", "content": "今天睡得怎么样？"}
        ]
        # NO tools
    )
    content3 = response3.choices[0].message.content or ""
    print(f"{'✅ WORKS' if content3 else '❌ EMPTY'}")
    print(f"   Response: {len(content3)} chars")
    print(f"   Preview: {content3[:200]}")
except Exception as e:
    print(f"❌ Error: {e}")

print("\n" + "=" * 60)
print("KEY INSIGHT:")
print("=" * 60)
print("If Test 1 works but Test 2 fails:")
print("  → System prompt instructs model to call tools")
print("  → Model tries to call tool")
print("  → Your proxy rejects/mangles the response")
print("\nIf Test 3 works:")
print("  → System prompt itself is fine")
print("  → Problem is ONLY when tools are provided")
