#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
檢查原始快取資料庫中ROW291的翻譯記錄
"""

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# 設定編碼
sys.stdout.reconfigure(encoding='utf-8')

from app import create_app

def check_original_cache_row291():
    """檢查原始快取資料庫中ROW291的翻譯記錄"""
    
    print("=" * 80)
    print("檢查原始快取資料庫中的翻譯記錄")
    print("重點：ROW291 vs ROW349 的差異")
    print("=" * 80)
    
    app = create_app()
    
    with app.app_context():
        from sqlalchemy import text as sql_text
        from app import db
        
        # 1. 檢查ROW291附近的記錄
        print(f"1. 檢查ROW291附近的韓文翻譯記錄")
        print("-" * 60)
        
        result = db.session.execute(sql_text("""
            SELECT id, source_text, translated_text, target_language, created_at
            FROM dt_translation_cache 
            WHERE id >= 285 AND id <= 295 AND target_language = 'ko'
            ORDER BY id
        """))
        
        row291_records = result.fetchall()
        
        if not row291_records:
            print("❌ ROW285-295範圍內沒有韓文記錄")
        else:
            for record in row291_records:
                print(f"\nROW {record[0]}:")
                print(f"  原文: {repr(record[1][:50])}...")
                print(f"  韓文: {repr(record[2][:50])}...")
                print(f"  時間: {record[4]}")
        
        # 2. 檢查ROW349附近的記錄 (我手動補充的)
        print(f"\n2. 檢查ROW349附近的韓文翻譯記錄 (手動補充)")
        print("-" * 60)
        
        result = db.session.execute(sql_text("""
            SELECT id, source_text, translated_text, target_language, created_at
            FROM dt_translation_cache 
            WHERE id >= 345 AND id <= 355 AND target_language = 'ko'
            ORDER BY id
        """))
        
        row349_records = result.fetchall()
        
        if not row349_records:
            print("❌ ROW345-355範圍內沒有韓文記錄")
        else:
            for record in row349_records:
                print(f"\nROW {record[0]}:")
                print(f"  原文: {repr(record[1][:50])}...")
                print(f"  韓文: {repr(record[2][:50])}...")
                print(f"  時間: {record[4]}")
        
        # 3. 直接查找D2內容的所有翻譯記錄
        print(f"\n3. 查找D2內容的所有翻譯記錄")
        print("-" * 60)
        
        d2_content = "與 WB inline 串線（DB→WB）、時效快；支援 Sn/Au 晶片\n最小可支援9mil晶粒\n支援EAP管控"
        
        result = db.session.execute(sql_text("""
            SELECT id, source_text, translated_text, target_language, created_at
            FROM dt_translation_cache 
            WHERE source_text = :text
            ORDER BY id
        """), {'text': d2_content})
        
        d2_records = result.fetchall()
        
        if not d2_records:
            print(f"❌ 沒有找到D2內容的翻譯記錄")
            print(f"   查找內容: {repr(d2_content[:50])}...")
        else:
            print(f"✅ 找到 {len(d2_records)} 筆D2翻譯記錄:")
            for record in d2_records:
                print(f"\nROW {record[0]} ({record[3]}):")
                print(f"  原文: {repr(record[1][:50])}...")
                print(f"  翻譯: {repr(record[2][:50])}...")
                print(f"  時間: {record[4]}")
        
        # 4. 檢查最新的韓文快取總數
        print(f"\n4. 檢查韓文快取總數")
        print("-" * 60)
        
        result = db.session.execute(sql_text("""
            SELECT COUNT(*) as total, 
                   MIN(id) as min_id, 
                   MAX(id) as max_id,
                   MIN(created_at) as earliest,
                   MAX(created_at) as latest
            FROM dt_translation_cache 
            WHERE target_language = 'ko'
        """))
        
        stats = result.fetchone()
        print(f"韓文快取統計:")
        print(f"  總數: {stats[0]}")
        print(f"  ID範圍: {stats[1]} - {stats[2]}")
        print(f"  時間範圍: {stats[3]} - {stats[4]}")
        
        # 5. 比較原始DIFY翻譯 vs 手動補充翻譯
        print(f"\n5. 比較原始DIFY翻譯 vs 手動補充翻譯")
        print("-" * 60)
        
        if d2_records:
            if len(d2_records) == 1:
                print("✅ 只有一筆D2翻譯記錄，沒有重複")
            else:
                print(f"⚠️  有 {len(d2_records)} 筆重複的D2翻譯記錄:")
                for i, record in enumerate(d2_records, 1):
                    print(f"\n  記錄 {i} (ROW {record[0]}):")
                    print(f"    語言: {record[3]}")
                    print(f"    翻譯: {record[2][:100]}...")
                    print(f"    時間: {record[4]}")
                    
                    # 判斷來源
                    if record[0] <= 300:
                        print(f"    來源: 🤖 原始DIFY翻譯")
                    else:
                        print(f"    來源: ✋ 手動補充翻譯")
        
        # 6. 查看為什麼原始翻譯沒有生效
        print(f"\n6. 分析翻譯映射問題")
        print("-" * 60)
        
        if d2_records:
            original_record = min(d2_records, key=lambda x: x[0])  # 最早的記錄
            print(f"原始翻譯記錄 (ROW {original_record[0]}):")
            print(f"  是否為韓文: {original_record[3] == 'ko'}")
            print(f"  翻譯內容長度: {len(original_record[2])}")
            print(f"  翻譯內容: {repr(original_record[2])}")
            
            if original_record[3] == 'ko' and original_record[2]:
                print("✅ 原始翻譯記錄看起來正常")
                print("❓ 問題可能在於翻譯映射邏輯沒有正確使用這個快取")
            else:
                print("❌ 原始翻譯記錄有問題")
    
    print(f"\n" + "=" * 80)
    print("原始快取記錄檢查完成！")
    print("請查看上述分析找出真正的問題原因")
    print("=" * 80)

if __name__ == "__main__":
    check_original_cache_row291()