#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
測試_append_after函數是否正常工作
"""

import sys
import os
import tempfile
from pathlib import Path

# Fix encoding for Windows console
if sys.stdout.encoding != 'utf-8':
    sys.stdout.reconfigure(encoding='utf-8')
if sys.stderr.encoding != 'utf-8':
    sys.stderr.reconfigure(encoding='utf-8')

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

from app.services.document_processor import _append_after, _is_our_insert_block

def test_append_after_function():
    """測試_append_after函數是否正常工作"""
    
    print("=== 測試_append_after函數 ===")
    
    try:
        from docx import Document
        from docx.shared import Pt
        
        # 創建測試文檔
        doc = Document()
        
        # 添加原始段落
        original_para = doc.add_paragraph("這是原始中文段落。")
        print(f"✅ 創建原始段落: {original_para.text}")
        
        # 使用_append_after插入英文翻譯
        translation_text = "This is the English translation."
        
        try:
            new_para = _append_after(original_para, translation_text, italic=True, font_size_pt=12)
            print(f"✅ 使用_append_after插入翻譯: {new_para.text}")
            
            # 檢查插入的段落是否有我們的標記
            if _is_our_insert_block(new_para):
                print(f"✅ 翻譯段落包含零寬空格標記")
            else:
                print(f"❌ 翻譯段落缺少零寬空格標記")
                
            # 檢查格式是否正確
            if new_para.runs and new_para.runs[0].italic:
                print(f"✅ 翻譯段落格式正確（斜體）")
            else:
                print(f"❌ 翻譯段落格式不正確")
                
        except Exception as e:
            print(f"❌ _append_after插入失敗: {e}")
            return False
        
        # 再插入一個翻譯來測試鏈式插入
        try:
            vietnamese_translation = "Đây là bản dịch tiếng Việt."
            new_para2 = _append_after(new_para, vietnamese_translation, italic=True, font_size_pt=12)
            print(f"✅ 鏈式插入第二個翻譯: {new_para2.text}")
        except Exception as e:
            print(f"❌ 鏈式插入失敗: {e}")
        
        # 保存測試文檔
        test_file = Path(tempfile.gettempdir()) / "test_append_after.docx"
        doc.save(str(test_file))
        print(f"✅ 測試文檔保存至: {test_file}")
        
        # 重新讀取文檔驗證
        try:
            doc2 = Document(str(test_file))
            paragraphs = [p.text.strip() for p in doc2.paragraphs if p.text.strip()]
            
            print(f"\n📄 測試文檔內容驗證:")
            print(f"總段落數: {len(paragraphs)}")
            
            for i, para_text in enumerate(paragraphs):
                has_chinese = any('\u4e00' <= c <= '\u9fff' for c in para_text)
                has_english = any(ord(c) < 128 and c.isalpha() for c in para_text)
                has_vietnamese = any('\u00C0' <= c <= '\u1EF9' for c in para_text)
                
                lang_info = []
                if has_chinese:
                    lang_info.append("中文")
                if has_english:
                    lang_info.append("英文")
                if has_vietnamese:
                    lang_info.append("越南文")
                
                print(f"  段落 {i+1}: [{'/'.join(lang_info)}] {para_text}")
            
            # 檢查是否有正確的交錯格式
            expected_sequence = [
                ("中文", "這是原始中文段落。"),
                ("英文", "This is the English translation."),
                ("越南文", "Đây là bản dịch tiếng Việt.")
            ]
            
            success = True
            for i, (expected_lang, expected_text) in enumerate(expected_sequence):
                if i < len(paragraphs):
                    actual_text = paragraphs[i]
                    if expected_text in actual_text:
                        print(f"  ✅ 段落 {i+1} 包含預期的{expected_lang}內容")
                    else:
                        print(f"  ❌ 段落 {i+1} 不包含預期的{expected_lang}內容")
                        success = False
                else:
                    print(f"  ❌ 缺少第 {i+1} 個段落")
                    success = False
            
            if success:
                print(f"\n✅ _append_after函數工作正常！")
                return True
            else:
                print(f"\n❌ _append_after函數有問題")
                return False
                
        except Exception as e:
            print(f"❌ 讀取測試文檔失敗: {e}")
            return False
        
    except Exception as e:
        print(f"❌ 測試失敗: {e}")
        return False

if __name__ == "__main__":
    success = test_append_after_function()
    if success:
        print(f"\n🎉 _append_after函數測試通過")
    else:
        print(f"\n💥 _append_after函數測試失敗")