name: Pull Request CI
on:
  pull_request:
    branches: ["dev"]

jobs:
  job_armv8:
    runs-on: macos-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          submodules: 'recursive'

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'
          cache: gradle

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Setup NPM Dependencies
        run: npm install typescript -g

      - name: Add Android targets for Rust
        run: rustup target add armv7-linux-androideabi aarch64-linux-android

      - name: Build
        run: ./gradlew --configuration-cache assembleArmv8Debug

      - name: Get current build version
        id: version-env
        run: |
          ./gradlew --configuration-cache getVersion
          echo "version=$(cat app/build/version.txt)" >> $GITHUB_ENV

      - name: Rename APK file
        run: |
          mv app/build/outputs/apk/armv8/debug/*.apk app/build/outputs/apk/armv8/debug/purrfectsnap-${{ env.version }}-armv8-${GITHUB_SHA::7}.apk

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: purrfectsnap-armv8-debug
          path: app/build/outputs/apk/armv8/debug/*.apk
          
  job_armv7:
    runs-on: macos-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          submodules: 'recursive'

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'
          cache: gradle

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Setup NPM Dependencies
        run: npm install typescript -g

      - name: Add Android targets for Rust
        run: rustup target add armv7-linux-androideabi aarch64-linux-android

      - name: Build
        run: ./gradlew --configuration-cache assembleArmv7Debug

      - name: Get current build version
        id: version-env
        run: |
          ./gradlew --configuration-cache getVersion
          echo "version=$(cat app/build/version.txt)" >> $GITHUB_ENV

      - name: Rename APK file
        run: |
          mv app/build/outputs/apk/armv7/debug/*.apk app/build/outputs/apk/armv7/debug/purrfectsnap-${{ env.version }}-armv7-${GITHUB_SHA::7}.apk

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: purrfectsnap-armv7-debug
          path: app/build/outputs/apk/armv7/debug/*.apk

  job_manager:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          submodules: 'recursive'

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'
          cache: gradle

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Build
        run: ./gradlew --configuration-cache manager:assembleDebug

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: manager
          path: manager/build/outputs/apk/debug/*.apk

  job_core:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          submodules: 'recursive'

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'
          cache: gradle

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Add Android targets for Rust
        run: rustup target add armv7-linux-androideabi aarch64-linux-android

      - name: Build
        run: ./gradlew --configuration-cache assembleCoreDebug

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: core
          path: app/build/outputs/apk/core/debug/*.apk

