name: Reusable Windows workflow

on:
  workflow_call:
    inputs:
      image:
        description: Window Server image
        required: true
        type: string
      msvc:
        description: MSVC version (2019/2022)
        default: "2022"
        type: string
      vcpkg-deps-tag:
        description: Git tag of our deps
        required: true
        type: string
      build-type:
        default: RelWithDebInfo
        type: string
      package:
        default: false
        type: boolean
      release:
        default: false
        type: boolean

jobs:
  Windows:
    name: windows-${{ inputs.image }}

    runs-on: windows-${{ inputs.image }}

    env:
      archive: FAILEDTODOWNLOAD

    steps:
    - name: Install NSIS
      if: ${{ inputs.package }}
      run: choco install nsis

    - uses: actions/checkout@v4

    - name: Create directories for dependencies
      run: |
        mkdir -p ${{ github.workspace }}/deps
        mkdir -p ${{ github.workspace }}/deps/Qt

    - name: Download prebuilt vcpkg packages
      working-directory: ${{ github.workspace }}/deps
      run: |
        $MANIFEST = "vcpkg-x64-${{ inputs.msvc }}-${{ inputs.vcpkg-deps-tag }}.txt"
        curl --fail --retry 3 -L -o "$MANIFEST" "https://gitlab.com/OpenMW/openmw-deps/-/raw/main/windows/$MANIFEST"
        $lines = Get-Content "$MANIFEST"
        $URL = $lines[0]
        $split = -split $lines[1]
        $HASH = $split[0]
        $FILE = $split[1]
        curl --fail --retry 3 -L -o "$FILE" "$URL"
        $filehash = Get-FileHash "$FILE" -Algorithm SHA512
        if ( $filehash.hash -ne "$HASH" ) {
          exit 1
        }
        echo "archive=$FILE" >> $env:GITHUB_ENV

    - name: Extract archived prebuilt vcpkg packages
      working-directory: ${{ github.workspace }}/deps
      run: 7z x -y -ovcpkg-x64-${{ inputs.msvc }}-${{ inputs.vcpkg-deps-tag }} $env:archive

    - name: Cache Qt
      id: qt-cache
      uses: actions/cache@v4
      with:
        path: ${{ github.workspace }}/deps/Qt/6.6.3/msvc2019_64
        key: qt-cache-6.6.3-msvc2019_64-v1

    - name: Download aqt
      if: steps.qt-cache.outputs.cache-hit != 'true'
      working-directory: ${{ github.workspace }}/deps/Qt
      run: >
        curl --fail --retry 3 -L
        -o aqt_x64.exe
        https://github.com/miurahr/aqtinstall/releases/download/v3.1.15/aqt_x64.exe

    - name: Install Qt with aqt
      if: steps.qt-cache.outputs.cache-hit != 'true'
      working-directory: ${{ github.workspace }}/deps/Qt
      run: .\aqt_x64.exe install-qt windows desktop 6.6.3 win64_msvc2019_64

    - uses: ilammy/msvc-dev-cmd@v1

    - uses: seanmiddleditch/gha-setup-ninja@master

    - name: Configure OpenMW
      run: >
        cmake
        -S .
        -B ${{ github.workspace }}/build
        -G Ninja
        -D CMAKE_BUILD_TYPE=${{ inputs.build-type }}
        -D CMAKE_TOOLCHAIN_FILE='${{ github.workspace }}/deps/vcpkg-x64-${{ inputs.msvc }}-${{ inputs.vcpkg-deps-tag }}/scripts/buildsystems/vcpkg.cmake'
        -D CMAKE_PREFIX_PATH='${{ github.workspace }}/deps/Qt/6.6.3/msvc2019_64'
        ${{ inputs.package && '-D CMAKE_CXX_FLAGS_RELEASE="/O2 /Ob2 /DNDEBUG /Zi"' || '' }}
        ${{ inputs.package && '-D "CMAKE_EXE_LINKER_FLAGS_RELEASE=/DEBUG /INCREMENTAL:NO"' || '' }}
        -D LuaJit_INCLUDE_DIR='${{ github.workspace }}/deps/vcpkg-x64-${{ inputs.msvc }}-${{ inputs.vcpkg-deps-tag }}/installed/x64-windows/include/luajit'
        -D LuaJit_LIBRARY='${{ github.workspace }}/deps/vcpkg-x64-${{ inputs.msvc }}-${{ inputs.vcpkg-deps-tag }}/installed/x64-windows/lib/lua51.lib'
        -D BUILD_BENCHMARKS=${{ ! inputs.package }}
        -D BUILD_COMPONENTS_TESTS=${{ ! inputs.package }}
        -D BUILD_OPENMW_TESTS=${{ ! inputs.package }}
        -D BUILD_OPENCS_TESTS=${{ ! inputs.package }}
        -D OPENMW_USE_SYSTEM_SQLITE3=OFF
        -D OPENMW_USE_SYSTEM_YAML_CPP=OFF
        -D OPENMW_LTO_BUILD=ON
        ${{ inputs.package && '-D "VCREDIST64=$env:VCToolsRedistDir/vc_redist.x64.exe"' || '' }}

    - name: Build OpenMW
      run: cmake --build ${{ github.workspace }}/build

    - name: Copy missing DLLs
      run: |
        cp ${{ github.workspace }}/deps/vcpkg-x64-${{ inputs.msvc }}-${{ inputs.vcpkg-deps-tag }}/installed/x64-windows/bin/Release/MyGUIEngine.dll ${{ github.workspace }}/build
        cp -Filter *.dll -Recurse ${{ github.workspace }}/deps/vcpkg-x64-${{ inputs.msvc }}-${{ inputs.vcpkg-deps-tag }}/installed/x64-windows/bin/osgPlugins-3.6.5 ${{ github.workspace }}/build
        cp ${{ github.workspace }}/deps/vcpkg-x64-${{ inputs.msvc }}-${{ inputs.vcpkg-deps-tag }}/installed/x64-windows/bin/*.dll ${{ github.workspace }}/build

    - name: Copy Qt DLLs
      working-directory: ${{ github.workspace }}/deps/Qt/6.6.3/msvc2019_64
      run: |
        cp bin/Qt6Core.dll ${{ github.workspace }}/build
        cp bin/Qt6Gui.dll ${{ github.workspace }}/build
        cp bin/Qt6Network.dll ${{ github.workspace }}/build
        cp bin/Qt6OpenGL.dll ${{ github.workspace }}/build
        cp bin/Qt6OpenGLWidgets.dll ${{ github.workspace }}/build
        cp bin/Qt6Widgets.dll ${{ github.workspace }}/build
        cp bin/Qt6Svg.dll ${{ github.workspace }}/build
        mkdir ${{ github.workspace }}/build/styles
        cp plugins/styles/qwindowsvistastyle.dll ${{ github.workspace }}/build/styles
        mkdir ${{ github.workspace }}/build/platforms
        cp plugins/platforms/qwindows.dll ${{ github.workspace }}/build/platforms
        mkdir ${{ github.workspace }}/build/imageformats
        cp plugins/imageformats/qsvg.dll ${{ github.workspace }}/build/imageformats
        mkdir ${{ github.workspace }}/build/iconengines
        cp plugins/iconengines/qsvgicon.dll ${{ github.workspace }}/build/iconengines

    - name: Create symbol server directory structure
      working-directory: ${{ github.workspace }}/build
      run: |
        ${{ github.workspace }}\CI\Store-Symbols.ps1
        Move-Item ${{ github.workspace }}\build\SymStore ${{ github.workspace }}

    - name: Move pdb files
      run: |
        robocopy build pdb *.pdb /MOVE
        if ($lastexitcode -lt 8) {
          $global:LASTEXITCODE = $null
        }

    - name: Install OpenMW
      if: ${{ ! inputs.package }}
      run: cmake --install ${{ github.workspace }}/build --prefix ${{ github.workspace }}/install

    - name: Package OpenMW
      if: ${{ inputs.package }}
      run: |
        cpack --config "${{ github.workspace }}/build/CPackConfig.cmake" -B "${{ github.workspace }}/install"
        rm -r -Force "${{ github.workspace }}/install/_CPack_Packages"

    - name: Remove extra pdb files
      if: ${{ ! inputs.package }}
      shell: bash
      run: |
        rm -rf install/bin
        rm -rf install/_deps

    - name: Generate CI-ID.txt
      shell: bash
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        job_url=$(gh run --repo ${{ github.repository }} view ${{ github.run_id }} --json jobs --jq '.jobs[] | select(.name == "windows-${{ inputs.msvc }}") | .url')
        printf "Ref ${{ github.ref }}\nJob ${job_url}\nCommit ${{ github.sha }}\n" > install/CI-ID.txt
        cp install/CI-ID.txt pdb/CI-ID.txt
        cp install/CI-ID.txt SymStore/CI-ID.txt

    - name: Store OpenMW archived pdb files
      uses: actions/upload-artifact@v4
      with:
        name: openmw-windows-${{ inputs.msvc }}-pdb-${{ github.sha }}
        path: ${{ github.workspace }}/pdb/*

    - name: Store OpenMW build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: openmw-windows-${{ inputs.msvc }}-${{ github.sha }}
        path: ${{ github.workspace }}/install/*

    - name: Store symbol server artifacts
      uses: actions/upload-artifact@v4
      with:
        name: openmw-windows-${{ inputs.msvc }}-sym-store-${{ github.sha }}
        path: ${{ github.workspace }}/SymStore/*

    - name: Upload to symbol server
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        AWS_DEFAULT_REGION: eu-west-3
      if: ${{ env.AWS_ACCESS_KEY_ID != '' && env.AWS_SECRET_ACCESS_KEY != '' && inputs.package }}
      working-directory: ${{ github.workspace }}/SymStore
      run: |
        choco uninstall awscli -y
        choco install awscli -y --version=2.22.35
        aws --version
        aws --endpoint-url https://rgw.ctrl-c.liu.se s3 sync --size-only --exclude * --include *.ex_ --include *.dl_ --include *.pd_ . s3://openmw-sym

    - name: Add install directory to PATH
      shell: bash
      run: echo '${{ github.workspace }}/install' >> ${GITHUB_PATH}

    - name: Run components tests
      if: ${{ ! inputs.package }}
      run: build/components-tests.exe

    - name: Run OpenMW tests
      if: ${{ ! inputs.package }}
      run: build/openmw-tests.exe

    - name: Run OpenMW-CS tests
      if: ${{ ! inputs.package }}
      run: build/openmw-cs-tests.exe

    - name: Run detournavigator navmeshtilescache benchmark
      if: ${{ ! inputs.package }}
      run: build/openmw_detournavigator_navmeshtilescache_benchmark.exe

    - name: Run settings access benchmark
      if: ${{ ! inputs.package }}
      run: build/openmw_settings_access_benchmark.exe

    - name: Run esm refid benchmark
      if: ${{ ! inputs.package }}
      run: build/openmw_esm_refid_benchmark.exe

    - name: Create prerelease
      if: ${{ inputs.release }}
      uses: softprops/action-gh-release@v2
      with:
        files: ${{ github.workspace }}/install/*.exe
        prerelease: true
        draft: true
