@echo off
REM ============================================================
REM  build_windows.bat — Compile Pseudonymisation GUI v5
REM  avec Nuitka (Python -> C -> .exe natif Windows)
REM ============================================================
REM  Prerequis :
REM    - Python 3.10+ installe et dans le PATH
REM    - pip install nuitka zstandard
REM    - pip install -r requirements.txt
REM    - Visual Studio Build Tools (ou MinGW64)
REM ============================================================

setlocal
set APP_NAME=Pseudonymisation
set ENTRY=Pseudonymisation_Gui_V5.py
set DIST_DIR=build\Pseudonymisation_Gui_V5.dist
set MODEL_ID=cmarkea/distilcamembert-base-ner

echo [build] Verification de Python...
python --version || (echo Python introuvable & exit /b 1)

echo [build] Installation de Nuitka si absent...
pip install nuitka zstandard 2>nul

echo [build] Telechargement du modele NER si absent...
python -c "from ner_manager_onnx import NerModelManager; m=NerModelManager(cache_dir='models'); m.load('%MODEL_ID%'); print('Modele OK'); m.unload()"

echo [build] Compilation avec Nuitka (cela peut prendre 10-20 min)...
python -m nuitka ^
    --standalone ^
    --enable-plugin=tk-inter ^
    --include-module=anonymizer_core_refactored_onnx ^
    --include-module=ner_manager_onnx ^
    --include-module=eds_pseudo_manager ^
    --include-data-dir=config=config ^
    --include-data-dir=data=data ^
    --include-data-dir=models=models ^
    --nofollow-import-to=onnxruntime ^
    --nofollow-import-to=numpy ^
    --nofollow-import-to=transformers ^
    --nofollow-import-to=optimum ^
    --nofollow-import-to=tokenizers ^
    --nofollow-import-to=sentencepiece ^
    --nofollow-import-to=torch ^
    --nofollow-import-to=PIL ^
    --nofollow-import-to=pdfplumber ^
    --nofollow-import-to=pdfminer ^
    --nofollow-import-to=fitz ^
    --nofollow-import-to=pymupdf ^
    --nofollow-import-to=yaml ^
    --nofollow-import-to=huggingface_hub ^
    --nofollow-import-to=safetensors ^
    --nofollow-import-to=regex ^
    --nofollow-import-to=tqdm ^
    --nofollow-import-to=requests ^
    --nofollow-import-to=filelock ^
    --nofollow-import-to=packaging ^
    --no-deployment-flag=excluded-module-usage ^
    --windows-console-mode=disable ^
    --output-dir=build ^
    --company-name="Hopital" ^
    --product-name="Pseudonymisation de PDF" ^
    --product-version=5.0.0 ^
    --file-description="Pseudonymisation automatique de documents PDF" ^
    --assume-yes-for-downloads ^
    %ENTRY%

if %ERRORLEVEL% NEQ 0 (
    echo [build] ERREUR : la compilation a echoue.
    exit /b 1
)

echo [build] Copie des packages Python dans le dossier dist...
pip install --target="%DIST_DIR%" -r requirements.txt --no-warn-script-location

echo [build] OK — Dossier cree : %DIST_DIR%\
echo [build] Le modele NER est embarque. Aucun telechargement necessaire.
endlocal
