from pathlib import Path

from translate_docx import (
    GoogleTranslatorWrapper,
    ManualTranslator,
    extract_document,
    rebuild_document,
    translate_document,
)

# Define translations
translations = {
    "Hallo": "Hello",
    "Welt": "World",
    "Zakenman": "Businessman"
}

input_path = Path.cwd() / "user_data" / "input_new_A.docx"
output_path = Path.cwd() / "user_data" / "output_new_A_TEST.docx"

# Extract → Translate → Rebuild (using input as template to preserve line spacing)
doc = extract_document(input_path.absolute().as_posix())

# Configure rate limiting and progress reporting
# Adjust delay_between_calls based on your needs:
# - 0.3 = 300ms (default, ~3 calls/second, good for most cases)
# - 0.5 = 500ms (more conservative, ~2 calls/second)
# - 1.0 = 1s (very conservative, ~1 call/second)
# - 0 = disable rate limiting (not recommended for large documents)
translator = GoogleTranslatorWrapper(
    delay_between_calls=0.5,  # 500ms between calls (2 calls/second)
    max_retries=3              # Retry up to 3 times on failure
)
# translator = ManualTranslator(translations, word_level=True)

# Translate with progress reporting (enabled by default)
# Set show_progress=False to disable progress output
translated = translate_document(doc, translator, "nl", "en")
rebuild_document(translated, output_path.absolute().as_posix(), template_path=input_path.absolute().as_posix())

print("✓ Translation complete! Output saved to output_example.docx")
