"""Step 2: download the translation models for EVERY language LibreTranslate offers.

DirectAdmin > Setup Python App > Execute python script:  lt_models.py  > Run Script
It returns at once and keeps downloading in the background (30-90 minutes, about 8-12 GB of disk).
Follow it in File Manager: open lt_models.log in this folder. Each model is listed as it finishes,
and the log ends with "DONE". Running it again only fetches what is still missing.
"""
import os
import subprocess
import sys
import time

HERE = os.path.dirname(os.path.abspath(__file__))
LOG = os.path.join(HERE, 'lt_models.log')
LANGS_FILE = os.path.join(HERE, 'lt_langs.txt')

if '--run' in sys.argv:
    log = open(LOG, 'a', buffering=1)
    sys.stdout = sys.stderr = log
    try:
        from argostranslate import package
        package.update_package_index()
        have = {(p.from_code, p.to_code) for p in package.get_installed_packages()}
        todo = [p for p in package.get_available_packages() if (p.from_code, p.to_code) not in have]
        print('Already installed: %d models. To download: %d models.' % (len(have), len(todo)))
        failed = 0
        for i, p in enumerate(todo, 1):
            for attempt in (1, 2, 3):
                try:
                    package.install_from_path(p.download())
                    print('[%d/%d] %s -> %s' % (i, len(todo), p.from_code, p.to_code))
                    break
                except Exception as e:  # noqa: BLE001 - a network hiccup: try again, then move on
                    if attempt == 3:
                        failed += 1
                        print('[%d/%d] %s -> %s FAILED: %r' % (i, len(todo), p.from_code, p.to_code, e))
                    else:
                        time.sleep(5)
        with open(LANGS_FILE, 'w') as f:
            f.write('all')   # passenger_wsgi.py then loads every installed language
        langs = sorted({c for p in package.get_installed_packages() for c in (p.from_code, p.to_code)})
        print('Languages installed (%d): %s' % (len(langs), ', '.join(langs)))
        if failed:
            print('\n%d models failed. Run lt_models.py again to fetch only those.' % failed)
        print('\nDONE: models installed. Next step: run lt_key.py (once), then restart the app.')
    except Exception as e:  # noqa: BLE001 - everything goes to the log for the admin
        print('\nFAILED: %r\nIs lt_install.log finished with DONE? Then run lt_models.py again.' % (e,))
        sys.exit(1)
else:
    with open(LOG, 'w') as f:
        f.write('Downloading translation models for all languages\n')
    subprocess.Popen([sys.executable, os.path.abspath(__file__), '--run'], cwd=HERE, stdin=subprocess.DEVNULL,
                     stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True)
    print('Started. Downloading in the background: open lt_models.log in File Manager to follow it.')
