#!/usr/bin/env python3
"""
Debug Gemini empty response issue with full request/response logging.
"""

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

# Enable debug mode
os.environ["DEBUG_GEMINI_PAYLOAD"] = "true"

from health.utils.env_loader import load_env_with_extras
from slack_bot.llm.gemini import GeminiLLM
from slack_bot.tools.registry import TOOLS_SCHEMA

load_env_with_extras()

print("=" * 60)
print("Gemini Empty Response Debug Session")
print("=" * 60)

print(f"\nConfiguration:")
print(f"  Model: {os.environ.get('GEMINI_MODEL')}")
print(f"  Base URL: {os.environ.get('GEMINI_BASE_URL')}")
print(f"  Debug Mode: ENABLED")

print(f"\nTest scenario: Simple health query with all 21 tools")
print(f"=" * 60)

llm = GeminiLLM()

# Test case that's known to fail
message = "今天睡得怎么样？"
context = []

print(f"\nSending request...")
print(f"  Message: {message}")
print(f"  Context: {len(context)} messages")
print(f"  Tools: {len(TOOLS_SCHEMA)}")

response, tool_calls = llm.generate_response(
    message=message,
    context=context,
    tools=TOOLS_SCHEMA,
    images=None
)

print(f"\n" + "=" * 60)
print("RESULT:")
print(f"=" * 60)
print(f"Response length: {len(response) if response else 0}")
print(f"Tool calls: {tool_calls}")

if response and response.strip():
    print(f"\n✅ SUCCESS - Got response")
    print(f"Response preview:\n{response[:500]}")
else:
    print(f"\n❌ FAILED - Empty response")
    print(f"\nNext steps:")
    print(f"  1. Check debug_request.json for the full request payload")
    print(f"  2. Check debug_response.json for the response details")
    print(f"  3. Check if response has finish_reason or other error indicators")
