name: build-mailadler-windows

on:
  workflow_dispatch:
  push:
    branches: [main, master]
    paths:
      - 'src/**'
      - 'CMakeLists.txt'
      - '.github/workflows/build-windows.yml'
  pull_request:
    branches: [main, master]

env:
  QT_VERSION: 6.6.3

jobs:
  build:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Qt
        uses: jurplel/install-qt-action@v4
        with:
          version: ${{ env.QT_VERSION }}
          arch: win64_msvc2019_64
          modules: 'qtwebsockets'
          cache: true

      - name: Configure
        run: |
          cmake -B build -G "Visual Studio 17 2022" -A x64 `
            -DCMAKE_BUILD_TYPE=Release

      - name: Build
        run: cmake --build build --config Release --parallel

      - name: Prepare version
        shell: bash
        run: |
          VERSION=$(date +"%y.%-m.%-d-%H%M")
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Package
        shell: pwsh
        run: |
          $version = "${{ env.VERSION }}"
          New-Item -ItemType Directory -Force -Path "mailadler-win64"
          Copy-Item -Path "build\src\Release\mailadler.exe" -Destination "mailadler-win64\" -ErrorAction SilentlyContinue
          # Deploy Qt dependencies
          & "$env:Qt6_DIR\bin\windeployqt.exe" "mailadler-win64\mailadler.exe" 2>$null
          Compress-Archive -Path "mailadler-win64\*" -DestinationPath "mailadler-win64-$version.zip"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: windows-portable
          path: mailadler-win64-*.zip

      - name: Upload to release (on tag)
        if: startsWith(github.ref, 'refs/tags/')
        uses: softprops/action-gh-release@v1
        with:
          files: mailadler-win64-*.zip
