name: Build Android

on:
    push:
        branches: [main]
        paths-ignore:
            - "**/*.md"
            - "./fastline/**"
            - "./materialious/src/lib/i18n/**"

    workflow_dispatch:

permissions:
    contents: write

jobs:
    android-build:
        runs-on: ubuntu-latest
        defaults:
            run:
                working-directory: ./materialious/android

        steps:
            - uses: actions/checkout@v3

            - uses: actions/setup-node@v3
              with:
                  node-version: latest

            - name: Install modules
              run: cd ../ && npm ci

            - name: Build android nodejs packages
              run: cd ../static/nodejsAndroid && npm ci

            - name: Build package
              run: cd ../ && npm run build && npm prune --omit=dev

            - name: Sync package
              run: cd ../ && npx cap sync

            - name: Setup JDK
              uses: actions/setup-java@v3
              with:
                  distribution: "zulu"
                  java-version: "21"
                  cache: "gradle"

            - name: Build App
              run: |
                  bash gradlew assembleRelease
                  bash gradlew bundleRelease

            - name: Move files
              run: |
                  mv app/build/outputs/apk/release/*.apk app/build/outputs/
                  mv app/build/outputs/bundle/release/*.aab app/build/outputs/

            - name: Sign App
              id: sign_app
              uses: ilharp/sign-android-release@v2
              with:
                  releaseDir: ./materialious/android/app/build/outputs/
                  signingKey: ${{ secrets.ANDROID_SIGNING_KEY }}
                  keyAlias: ${{ secrets.ANDROID_KEY_ALIAS }}
                  keyStorePassword: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
                  keyPassword: ${{ secrets.ANDROID_KEY_PASSWORD }}
                  buildToolsVersion: 36.0.0

            - name: Make release dir
              run: mkdir app/release

            - name: Delete temp apks
              run: rm app/build/outputs/*temp*
              continue-on-error: true

            - name: Move files to release
              run: |
                  mv app/build/outputs/*-signed.aab app/release/
                  mv app/build/outputs/*-signed.apk app/release/
              continue-on-error: true

            - name: Remove unsigned from file name
              run: cd app/release/ && for file in *-unsigned*; do mv "$file" "${file//-unsigned/}"; done
              continue-on-error: true

            - name: get-npm-version
              id: package-version
              uses: martinbeentjes/npm-get-version-action@v1.3.1
              with:
                  path: ./materialious

            - name: Upload APKs to release
              uses: svenstaro/upload-release-action@v2
              with:
                  repo_token: ${{ secrets.GITHUB_TOKEN }}
                  file: ./materialious/android/app/release/*
                  tag: ${{ steps.package-version.outputs.current-version }}
                  release_name: ${{ steps.package-version.outputs.current-version }}
                  overwrite: true
                  file_glob: true
                  draft: false
