name: Reusable Windows Build Workflow

permissions: read-all

on:
  workflow_call:
    inputs:
      arch:
        required: true
        type: string
      qt_version:
        required: true
        type: string
      runs_on:
        required: true
        type: string
      preset:
        required: true
        type: string
      pe_str:
        required: true
        type: string
      run_tests:
        required: false
        default: true
        type: boolean
      cross_extract:
        required: false
        default: false
        type: boolean
      test_cr_compress:
        required: false
        default: true
        type: boolean
      test_utf8_compress:
        required: false
        default: false
        type: boolean
      test_utf8_decompress:
        required: false
        default: false
        type: boolean
      test_utf8_archive_type:
        required: false
        default: "with_flag"
        type: string
    secrets:
      GH_TOKEN:
        required: false

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  # All aqt options: https://ddalcino.github.io/aqt-list-server/
  BUILD_TYPE: Release

jobs:
  build:
    runs-on: windows-latest
    name: Windows-${{ inputs.arch }}-Qt-${{ inputs.qt_version }}-Preset-${{ inputs.preset }}
    steps:
      - uses: actions/checkout@v6

      - name: Install Qt6
        if: "startsWith(inputs.qt_version, '6.')"
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ inputs.qt_version }}
          cache: 'true'
          cache-key-prefix: ${{ runner.os }}-Qt-Cache-${{ inputs.qt_version }}
          dir: ${{ github.workspace }}/Qt
          modules: 'qt5compat'
          arch: 'win64_msvc2022_64'

      - name: Install Qt5
        if: "startsWith(inputs.qt_version, '5.')"
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ inputs.qt_version }}
          cache: 'true'
          cache-key-prefix: ${{ runner.os }}-Qt-Cache-${{ inputs.qt_version }}
          dir: ${{ github.workspace }}/Qt
          arch: ${{ inputs.arch == 'x86' && 'win32_msvc2019' || 'win64_msvc2019_64' }}

      - uses: TheMrMilchmann/setup-msvc-dev@v3
        with:
          arch: ${{ inputs.arch }}

      - name: Install Conan
        if: contains(inputs.preset, 'conan')
        uses: turtlebrowser/get-conan@main

      - name: Init Conan
        if: contains(inputs.preset, 'conan')
        run: conan profile detect

      - name: Install dependencies with Conan
        if: contains(inputs.preset, 'conan')
        shell: cmd
        run: conan install . -of build -s build_type=Release -o *:shared=False --build=missing ${{ inputs.arch == 'x86' && '-s:h arch=x86' || '' }}

      - name: Configure
        shell: cmd
        run: cmake --preset ${{ inputs.preset }} -DQUAZIP_ENABLE_TESTS=ON

      - name: Build
        shell: cmd
        run: cmake --build ${{github.workspace}}/build --config Release

      # This second build is here temporarily until we figure out why qztest dep .dlls are not copied after first build
      - name: Build 2
        shell: cmd
        run: cmake --build ${{github.workspace}}/build --config Release

      - name: Check PE
        shell: bash
        working-directory: ./build/quazip/Release
        run: |
          DLL_NAME="quazip1-qt${{ startsWith(inputs.qt_version, '6') && '6' || '5' }}.dll"
          file "$DLL_NAME"
          file "$DLL_NAME" |grep "${{ inputs.pe_str }}"

      - name: Download UTF-8 archives
        if: inputs.test_utf8_decompress
        uses: actions/download-artifact@v8
        with:
          path: build/quazip
          merge-multiple: false

      - name: Run tests
        if: inputs.run_tests && !inputs.cross_extract
        env:
          TEST_CR_COMPRESS: ${{ inputs.test_cr_compress }}
          TEST_UTF8_COMPRESS: ${{ inputs.test_utf8_compress  }}
          TEST_UTF8_DECOMPRESS: ${{ inputs.test_utf8_decompress  }}
          TEST_UTF8_ARCHIVE_TYPE: ${{ inputs.test_utf8_archive_type }}
          #TEST_ZIP_UNZIP_LARGE: true
          QT_ASSUME_STDERR_HAS_CONSOLE: 1 # !! qDebug() is not visible if this is not set on Win runners
        working-directory: ./build
        run: ctest --verbose -C Release

      - name: Upload cp.zip to GitHub Actions Storage
        if: inputs.run_tests && !inputs.cross_extract
        uses: actions/upload-artifact@v7
        with:
          retention-days: 1
          overwrite: true
          name: "${{ inputs.runs_on }}_${{ inputs.preset }}_qt${{ inputs.qt_version }}_shared${{ inputs.shared }}_cp"
          path: build/quazip/cp.zip

      - name: Download bundle containing cp.zip
        if: inputs.cross_extract
        uses: actions/download-artifact@v8
        with:
          path: build/quazip
          merge-multiple: false

      - name: Run extract tests
        if: inputs.cross_extract
        working-directory: ${{github.workspace}}/build
        env:
          TEST_CR_DECOMPRESS: "true"
          QT_ASSUME_STDERR_HAS_CONSOLE: 1
        run: ctest --verbose -C Release