name: Build Windows EXE (Nuitka)

on:
  workflow_dispatch:
  push:
    tags:
      - 'v*'

jobs:
  build-windows:
    runs-on: windows-latest
    timeout-minutes: 90

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: pip

      - name: Cache Nuitka
        uses: actions/cache@v4
        with:
          path: |
            ~\AppData\Local\Nuitka\Nuitka
            nuitka-build-cache
          key: nuitka-${{ runner.os }}-${{ hashFiles('requirements.txt', 'Pseudonymisation_Gui_V5.py') }}
          restore-keys: nuitka-${{ runner.os }}-

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip setuptools wheel
          pip install -r requirements.txt
          pip install nuitka zstandard

      - name: Download NER model
        run: |
          python -c "from ner_manager_onnx import NerModelManager; m=NerModelManager(cache_dir='models'); m.load('cmarkea/distilcamembert-base-ner'); print('Model OK:', m.is_loaded()); m.unload()"

      - name: Build with Nuitka (standalone folder)
        run: |
          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=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 `
            Pseudonymisation_Gui_V5.py

      - name: Copy Python packages into dist folder
        run: |
          $dist = "build\Pseudonymisation_Gui_V5.dist"
          Write-Host "Installing packages into dist folder..."
          pip install --target="$dist" -r requirements.txt --no-warn-script-location
          Write-Host "Done. Dist folder size:"
          "{0:N0} MB" -f ((Get-ChildItem -Recurse "$dist" | Measure-Object -Property Length -Sum).Sum / 1MB)

      - name: Prepare distribution zip
        run: |
          Rename-Item -Path "build\Pseudonymisation_Gui_V5.dist" -NewName "Pseudonymisation"
          Compress-Archive -Path "build\Pseudonymisation" -DestinationPath "Pseudonymisation-Windows-x64.zip"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: Pseudonymisation-Windows-x64
          path: Pseudonymisation-Windows-x64.zip
          retention-days: 30

      - name: Upload to release (on tag)
        if: startsWith(github.ref, 'refs/tags/')
        uses: softprops/action-gh-release@v2
        with:
          files: Pseudonymisation-Windows-x64.zip
