name: Build mobile artifacts on merge

on:
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17

      - uses: gradle/actions/setup-gradle@v4

      - name: Install Fitbit deps
        working-directory: path-a-companion
        run: npm install

      - name: Build Fitbit .fba
        working-directory: path-a-companion
        run: npx fitbit-build
        env:
          FITBIT_QA_COMMANDS: 1

      - name: Build HevyBridge APK
        working-directory: phone-bridge
        run: gradle assembleDebug --no-daemon

      - name: Copy APK artifact
        run: cp phone-bridge/app/build/outputs/apk/debug/app-debug.apk hevy-bridge.apk

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: hevy-sense2-artifacts
          path: |
            path-a-companion/build/app.fba
            hevy-bridge.apk
          retention-days: 30

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          name: "Build ${{ github.sha }}"
          tag_name: "build-${{ github.run_number }}"
          files: |
            path-a-companion/build/app.fba
            hevy-bridge.apk
          prerelease: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
