#!/usr/bin/env python3
"""
Test system prompt length limit.
"""

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

client = OpenAI(
    api_key=os.environ.get("GEMINI_API_KEY"),
    base_url=f"{os.environ.get('GEMINI_BASE_URL')}/v1"
)
model = os.environ.get("GEMINI_MODEL")

print("=" * 60)
print("System Prompt Length Limit Test")
print("=" * 60)

# Test with increasing lengths
test_lengths = [1000, 2000, 3000, 4000, 5000, 6000]

for length in test_lengths:
    # Create a prompt of exact length
    prompt = "You are a helpful assistant. " * (length // 30)
    prompt = prompt[:length]

    try:
        response = client.chat.completions.create(
            model=model,
            messages=[
                {"role": "system", "content": prompt},
                {"role": "user", "content": "Hello"}
            ]
        )
        content = response.choices[0].message.content or ""
        status = "✅ WORKS" if content else "❌ EMPTY"
        print(f"{length:5d} chars → {status} ({len(content)} chars response)")
    except Exception as e:
        print(f"{length:5d} chars → ❌ Error: {str(e)[:50]}")

print("\n" + "=" * 60)
print("CONCLUSION:")
print("=" * 60)
print("The length where it starts failing is your proxy's limit!")
print("Your full system prompt is 6094 chars.")
