#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test Dify API response to see what's being returned
"""

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'app'))

from app import create_app
from app.services.dify_client import DifyClient

def test_dify_response():
    """Test what the Dify client actually returns"""
    app = create_app()
    
    with app.app_context():
        client = DifyClient()
        
        test_text = "1、目的"
        print(f"Testing translation of: '{test_text}'")
        print(f"From: zh-cn -> To: en")
        
        try:
            result = client.translate_text(
                text=test_text,
                source_language="zh-cn", 
                target_language="en",
                user_id=1,
                job_id=1
            )
            
            print(f"\nDify API Response:")
            for key, value in result.items():
                if key == 'metadata':
                    print(f"  {key}: {type(value).__name__} with {len(value) if isinstance(value, dict) else 'N/A'} items")
                    for mk, mv in value.items() if isinstance(value, dict) else []:
                        print(f"    {mk}: {mv}")
                else:
                    print(f"  {key}: {repr(value)}")
            
            # Check if translated_text exists and what it contains
            translated_text = result.get('translated_text', 'NOT FOUND')
            print(f"\nTranslated text: {repr(translated_text)}")
            
            if translated_text == test_text:
                print("⚠️  WARNING: Translation is identical to source text!")
            elif translated_text == 'NOT FOUND':
                print("❌ ERROR: No translated_text in response!")
            elif not translated_text.strip():
                print("❌ ERROR: Translated text is empty!")
            else:
                print("✅ Translation looks different from source")
                
        except Exception as e:
            print(f"ERROR: {e}")
            import traceback
            traceback.print_exc()

if __name__ == "__main__":
    test_dify_response()