name: Build Release App

on:
  workflow_call:
    inputs:
      version:
        required: true
        type: string

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

      - uses: actions/setup-java@v4
        with:
          java-version: 21
          distribution: zulu

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

      - name: Inject version
        run: |
          sed -i 's/versionName = ".*"/versionName = "${{ inputs.version }}"/' Manager/app/build.gradle.kts
          sed -i "s/versionCode = .*/versionCode = ${{ github.run_number }}/" Manager/app/build.gradle.kts
          grep -E "versionCode|versionName" Manager/app/build.gradle.kts

      - name: Build
        working-directory: Manager
        env:
          SIGNING_KEY_ALIAS: ${{ secrets.keyAlias }}
          SIGNING_KEY_PASSWORD: ${{ secrets.keyPassword }}
          SIGNING_STORE_PASSWORD: ${{ secrets.keystorePassword }}
          SIGNING_STORE_FILE: ${{ github.workspace }}/release.keystore
          RELEASE: true
        run: |
          echo "${{ secrets.keystore }}" | base64 -d > ${{ github.workspace }}/release.keystore
          ./gradlew :app:packageRelease --stacktrace
          rm ${{ github.workspace }}/release.keystore

      - uses: actions/upload-artifact@v4
        with:
          name: rl-mobile-artifacts
          if-no-files-found: error
          path: Manager/app/build/outputs/apk/release/app-release.apk
