name: Build

on:
  push:
    branches:
      - "**"
  pull_request:
    branches: [main]

permissions:
  contents: write

jobs:
  windows:
    runs-on: windows-latest

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

      - name: Install Qt6 and MinGW
        uses: jurplel/install-qt-action@v4
        with:
          version: "6.8.1"
          arch: "win64_mingw"
          tools: "tools_mingw1310,qt.tools.win64_mingw1310"
          cache: true

      - name: Configure
        shell: bash
        run: |
          export PATH="$IQTA_TOOLS/mingw1310_64/bin:$PATH"
          gcc --version
          cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_UI_TESTS=OFF \
            -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++

      - name: Build
        shell: bash
        run: |
          export PATH="$IQTA_TOOLS/mingw1310_64/bin:$PATH"
          cmake --build build

      - name: Test
        shell: bash
        run: |
          export PATH="$IQTA_TOOLS/mingw1310_64/bin:$PATH"
          ctest --test-dir build --output-on-failure

      - name: Package release zip
        shell: bash
        run: |
          export PATH="$IQTA_TOOLS/mingw1310_64/bin:$PATH"
          mkdir -p release
          cp build/Reclass.exe release/
          cp build/ReclassMcpBridge.exe release/
          cp build/*.dll release/ 2>/dev/null || true
          cp -r build/platforms release/ 2>/dev/null || true
          cp -r build/styles release/ 2>/dev/null || true
          cp -r build/imageformats release/ 2>/dev/null || true
          cp -r build/iconengines release/ 2>/dev/null || true
          windeployqt --no-translations --no-system-d3d-compiler --no-opengl-sw release/Reclass.exe
          mkdir -p release/Plugins
          cp build/Plugins/*.dll release/Plugins/ 2>/dev/null || true
          cp -r build/themes release/ 2>/dev/null || true
          cp -r build/examples release/ 2>/dev/null || true
          cp build/screenshot.png release/ 2>/dev/null || true
          cd release && 7z a ../Reclass-win64-qt6.zip *

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: Reclass-win64-qt6
          path: Reclass-win64-qt6.zip

  linux:
    runs-on: ubuntu-22.04

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

      - name: Install Qt6
        uses: jurplel/install-qt-action@v4
        with:
          version: "6.8.1"
          cache: true

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y ninja-build libgl1-mesa-dev libfuse2 libxcb-cursor0

      - name: Configure
        run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_UI_TESTS=OFF

      - name: Build
        run: cmake --build build

      - name: Test
        run: ctest --test-dir build --output-on-failure

      - name: Create AppImage
        run: |
          # Download linuxdeploy and Qt plugin
          wget -q https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage
          wget -q https://github.com/linuxdeploy/linuxdeploy-plugin-qt/releases/download/continuous/linuxdeploy-plugin-qt-x86_64.AppImage
          chmod +x linuxdeploy-x86_64.AppImage linuxdeploy-plugin-qt-x86_64.AppImage

          # Build AppDir structure
          mkdir -p AppDir/usr/bin AppDir/usr/share/icons/hicolor/256x256/apps
          cp build/Reclass AppDir/usr/bin/
          cp build/ReclassMcpBridge AppDir/usr/bin/
          cp -r build/themes AppDir/usr/bin/ 2>/dev/null || true
          cp -r build/examples AppDir/usr/bin/ 2>/dev/null || true
          mkdir -p AppDir/usr/bin/Plugins
          cp build/Plugins/*.so AppDir/usr/bin/Plugins/ 2>/dev/null || true
          cp src/icons/class.png AppDir/usr/share/icons/hicolor/256x256/apps/reclass.png

          # Create AppImage with Qt libs bundled
          # install-qt-action adds Qt bin to PATH; find qmake there
          QMAKE_BIN=$(which qmake 2>/dev/null || which qmake6 2>/dev/null || find "$RUNNER_WORKSPACE" -name qmake -path "*/bin/*" | head -1)
          echo "Found qmake at: $QMAKE_BIN"
          export QMAKE="$QMAKE_BIN"
          QT_ROOT=$(dirname "$(dirname "$QMAKE_BIN")")
          export LD_LIBRARY_PATH="$QT_ROOT/lib:$LD_LIBRARY_PATH"
          export EXTRA_QT_PLUGINS="svg;iconengines"
          ./linuxdeploy-x86_64.AppImage --appdir AppDir \
            --desktop-file deploy/Reclass.desktop \
            --icon-file AppDir/usr/share/icons/hicolor/256x256/apps/reclass.png \
            --plugin qt \
            --output appimage
          # Rename to final name
          ls Reclass-*.AppImage
          mv Reclass-*.AppImage Reclass-linux64-qt6.AppImage

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: Reclass-linux64-qt6
          path: Reclass-linux64-qt6.AppImage

  macos:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-15
            qt_arch: clang_arm64
            artifact_name: Reclass-macos-arm64-qt6
            zip_name: Reclass-macos-arm64-qt6.zip
          - os: macos-15-intel
            qt_arch: clang_64
            artifact_name: Reclass-macos-x86_64-qt6
            zip_name: Reclass-macos-x86_64-qt6.zip

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

      - name: Install dependencies
        run: |
          brew update
          brew install cmake ninja qt

      - name: Configure Qt paths
        run: |
          QT_PREFIX="$(brew --prefix qt)"
          echo "QT_PREFIX=$QT_PREFIX" >> "$GITHUB_ENV"
          echo "PATH=$QT_PREFIX/bin:$PATH" >> "$GITHUB_ENV"

      - name: Configure
        run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_UI_TESTS=OFF -DCMAKE_PREFIX_PATH="$QT_PREFIX"

      - name: Build
        run: cmake --build build

      - name: Test
        run: ctest --test-dir build --output-on-failure

      - name: Package app zip
        run: |
          MACDEPLOYQT_BIN="$QT_PREFIX/bin/macdeployqt"
          if [ ! -x "$MACDEPLOYQT_BIN" ]; then
            MACDEPLOYQT_BIN=$(which macdeployqt 2>/dev/null || find "$RUNNER_WORKSPACE" -name macdeployqt -path "*/bin/*" | head -1)
          fi
          echo "Found macdeployqt at: $MACDEPLOYQT_BIN"
          "$MACDEPLOYQT_BIN" build/Reclass.app -always-overwrite
          codesign --force --deep --sign - build/Reclass.app
          ditto -c -k --sequesterRsrc --keepParent build/Reclass.app "${{ matrix.zip_name }}"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact_name }}
          path: ${{ matrix.zip_name }}

  release:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    needs: [windows, linux, macos]
    runs-on: ubuntu-latest

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Get date tag
        id: date
        run: echo "tag=$(date +'%d-%m-%Y')" >> "$GITHUB_OUTPUT"

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: snapshot-${{ steps.date.outputs.tag }}
          name: Snapshot ${{ steps.date.outputs.tag }}
          body: |
            Automated snapshot from main branch.
            Commit: ${{ github.sha }}
          prerelease: false
          files: |
            artifacts/Reclass-win64-qt6/Reclass-win64-qt6.zip
            artifacts/Reclass-linux64-qt6/Reclass-linux64-qt6.AppImage
            artifacts/Reclass-macos-arm64-qt6/Reclass-macos-arm64-qt6.zip
            artifacts/Reclass-macos-x86_64-qt6/Reclass-macos-x86_64-qt6.zip
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
