#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
驗證最終韓文翻譯結果
"""

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

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

from pathlib import Path
import openpyxl

def verify_final_result():
    """驗證最終韓文翻譯結果"""
    
    print("=" * 80)
    print("驗證最終韓文翻譯結果")
    print("檢查是否成功使用原始DIFY翻譯")
    print("=" * 80)
    
    # 韓文翻譯檔案
    translated_file = Path(r"C:\Users\EGG\WORK\data\user_scrip\TOOL\Document_translator_V2\uploads\98158984-f335-44f5-a0b4-88fb8ccd5d78\original_panjit_98158984_ko_translated.xlsx")
    
    if not translated_file.exists():
        print(f"❌ 翻譯檔案不存在")
        return
    
    print(f"✅ 檢查檔案: {translated_file.name}")
    
    # 1. 檢查D2儲存格詳細內容
    print(f"\n1. D2儲存格詳細分析")
    print("-" * 60)
    
    wb = openpyxl.load_workbook(str(translated_file), data_only=False)
    d2_value = wb.active['D2'].value
    
    print(f"D2完整內容:")
    print(f"  類型: {type(d2_value)}")
    print(f"  長度: {len(d2_value) if d2_value else 0}")
    print(f"  內容: {repr(d2_value)}")
    
    if isinstance(d2_value, str):
        lines = d2_value.split('\n')
        print(f"\n行分解 (共{len(lines)}行):")
        for i, line in enumerate(lines, 1):
            print(f"  行{i}: {repr(line)}")
            
        # 找韓文翻譯部分
        korean_lines = []
        for line in lines:
            # 檢查是否包含韓文字符
            if any('\uac00' <= char <= '\ud7af' for char in line):
                korean_lines.append(line)
        
        print(f"\n韓文行 (共{len(korean_lines)}行):")
        for i, line in enumerate(korean_lines, 1):
            print(f"  韓文{i}: {line}")
            
            # 檢查特徵
            if "와이어 본딩" in line:
                print(f"    🎯 ✅ 原始DIFY翻譯特徵: '와이어 본딩'")
                success = True
            elif "연결" in line and "단축" in line:
                print(f"    ✋ ❌ 手動補充翻譯特徵: '연결' + '단축'")
                success = False
            else:
                print(f"    ❓ 無明顯特徵")
                success = None
    
    # 2. 檢查其他D欄位
    print(f"\n2. 其他D欄位檢查")
    print("-" * 60)
    
    d_cells = ['D3', 'D4', 'D5', 'D6', 'D7', 'D8']
    success_count = 0
    
    for cell_name in d_cells:
        cell_value = wb.active[cell_name].value
        
        if isinstance(cell_value, str) and '\n' in cell_value:
            lines = cell_value.split('\n')
            korean_lines = [line for line in lines if any('\uac00' <= char <= '\ud7af' for char in line)]
            
            if korean_lines:
                print(f"✅ {cell_name}: 有韓文翻譯")
                print(f"   韓文: {korean_lines[0][:30]}...")
                success_count += 1
            else:
                print(f"❌ {cell_name}: 沒有韓文翻譯")
        else:
            print(f"❌ {cell_name}: 沒有翻譯或格式不正確")
    
    print(f"\nD欄位翻譯成功率: {success_count + (1 if success else 0)}/{len(d_cells) + 1} = {((success_count + (1 if success else 0))/(len(d_cells) + 1)*100):.1f}%")
    
    # 3. 最終評估
    print(f"\n3. 最終評估")
    print("-" * 60)
    
    if success is True:
        print(f"🎉 大成功！")
        print(f"   ✅ D2正確使用原始DIFY翻譯")
        print(f"   ✅ 修復邏輯完美運作")
        print(f"   ✅ 文字格式不匹配問題已解決")
        print(f"   📊 整體品質: 使用原始API翻譯，品質更佳")
    elif success is False:
        print(f"⚠️  部分成功")
        print(f"   ❌ D2仍使用手動補充翻譯")
        print(f"   ❓ 可能需要檢查Celery worker是否載入新代碼")
    else:
        print(f"❓ 無法明確判斷")
        print(f"   需要人工檢查翻譯內容")
    
    wb.close()
    
    # 4. 檔案總結
    print(f"\n4. 檔案總結")
    print("-" * 60)
    print(f"最終韓文翻譯檔案:")
    print(f"  檔案: {translated_file.name}")
    print(f"  大小: {translated_file.stat().st_size / 1024:.1f} KB")
    print(f"  狀態: {'可用' if success is not False else '需要進一步檢查'}")
    
    print(f"\n" + "=" * 80)
    print("最終結果驗證完成！")
    if success is True:
        print("🎊 恭喜！問題已完美解決！")
    print("=" * 80)

if __name__ == "__main__":
    verify_final_result()