#!/usr/bin/env python3
"""
Test if smart quotes in system prompt cause the issue.
"""

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

from health.utils.env_loader import load_env_with_extras
from openai import OpenAI

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")

print("=" * 60)
print("Testing Smart Quotes Hypothesis")
print("=" * 60)

# Test 1: With smart quotes
prompt_with_smart_quotes = 'You are a helpful assistant. Follow the "TOOL-FIRST POLICY" guidelines.'

print("\nTest 1: System prompt with smart quotes ("")")
print("-" * 60)
try:
    response1 = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": prompt_with_smart_quotes},
            {"role": "user", "content": "Hello"}
        ]
    )
    content1 = response1.choices[0].message.content or ""
    print(f"{'✅ WORKS' if content1 else '❌ EMPTY'}")
    print(f"   Response: {len(content1)} chars")
except Exception as e:
    print(f"❌ Error: {e}")

# Test 2: With normal quotes
prompt_with_normal_quotes = 'You are a helpful assistant. Follow the "TOOL-FIRST POLICY" guidelines.'

print("\nTest 2: System prompt with normal quotes (\"\")")
print("-" * 60)
try:
    response2 = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": prompt_with_normal_quotes},
            {"role": "user", "content": "Hello"}
        ]
    )
    content2 = response2.choices[0].message.content or ""
    print(f"{'✅ WORKS' if content2 else '❌ EMPTY'}")
    print(f"   Response: {len(content2)} chars")
except Exception as e:
    print(f"❌ Error: {e}")

print("\n" + "=" * 60)
print("VERDICT:")
print("=" * 60)
print("If Test 1 fails but Test 2 works:")
print("  → Your proxy doesn't handle Unicode smart quotes properly!")
print("  → Solution: Replace all " and " with standard ASCII \" in system prompt")
