#!/usr/bin/env python3
"""
Progressive test: Add tools one by one to find which one breaks.
"""

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
from slack_bot.tools.registry import TOOLS_SCHEMA

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")
system_prompt = get_system_instruction()

print("=" * 60)
print("Progressive Tool Test - Finding the Breaking Point")
print("=" * 60)

# Test with increasing number of tools
test_counts = [1, 3, 5, 10, 21]

for count in test_counts:
    print(f"\nTest with {count} tool(s)")
    print("-" * 60)

    tools_subset = TOOLS_SCHEMA[:count]

    try:
        response = client.chat.completions.create(
            model=model,
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": "今天睡得怎么样？"}
            ],
            tools=tools_subset,
            tool_choice="auto"
        )

        content = response.choices[0].message.content or ""
        has_tools = hasattr(response.choices[0].message, 'tool_calls') and response.choices[0].message.tool_calls

        status = "✅ WORKS" if (content or has_tools) else "❌ EMPTY"
        print(f"{status}")
        print(f"   Response: {len(content)} chars")
        print(f"   Tool calls: {has_tools}")

    except Exception as e:
        print(f"❌ EXCEPTION: {str(e)[:100]}")

print("\n" + "=" * 60)
print("This will show at which tool count it starts failing.")
