name: Build Android
on:
  push:
    branches:
      - '**'

jobs:
  build:
    runs-on: [self-hosted, Linux, X64, android]
    steps:
    - name: Set up github user
      env:
        PRV_REPO_USER: ${{ secrets.PRV_REPO_USER }}
        PRV_REPO_PAT: ${{ secrets.PRV_REPO_PAT }}
      run: |
        git config --global --replace-all "url.https://$PRV_REPO_USER:$PRV_REPO_PAT@github.com.insteadOf" ssh://git@github.com
        git config --global --add "url.https://$PRV_REPO_USER:$PRV_REPO_PAT@github.com.insteadOf" git@github.com
        git config --global --add "url.https://$PRV_REPO_USER:$PRV_REPO_PAT@github.com.insteadOf" https://github.com
    - uses: actions/checkout@v3
      with:
        token: ${{ secrets.PRV_REPO_PAT }}
        submodules: recursive
        fetch-depth: 0

    - name: Set up JDK
      uses: actions/setup-java@v3
      with:
        distribution: 'temurin'
        java-version: 17

    - name: Decode Keystore
      env:
        ENCODED_STRING: ${{ secrets.KEYSTORE_BASE_64 }}
        RELEASE_KEYSTORE_PASSWORD: ${{ secrets.RELEASE_KEYSTORE_PASSWORD }}
        RELEASE_KEYSTORE_ALIAS: ${{ secrets.RELEASE_KEYSTORE_ALIAS }}
        RELEASE_KEY_PASSWORD: ${{ secrets.RELEASE_KEY_PASSWORD }}

      run: |
        echo $ENCODED_STRING > keystore-b64.txt
        base64 -d keystore-b64.txt > public/keystore.jks

    - name: Test
      run: ./gradlew testDebugUnitTest jvmTest lintDebug --stacktrace --no-daemon
      env:
        GITHUB_ACTOR: ${{ secrets.PRV_REPO_USER }}
        GITHUB_TOKEN: ${{ secrets.PRV_REPO_PAT }}

    - name: Build Release apk/bundle
      env:
        RELEASE_KEYSTORE_PASSWORD: ${{ secrets.RELEASE_KEYSTORE_PASSWORD }}
        RELEASE_KEYSTORE_ALIAS: ${{ secrets.RELEASE_KEYSTORE_ALIAS }}
        RELEASE_KEY_PASSWORD: ${{ secrets.RELEASE_KEY_PASSWORD }}
        GITHUB_ACTOR: ${{ secrets.PRV_REPO_USER }}
        GITHUB_TOKEN: ${{ secrets.PRV_REPO_PAT }}
      run: ./gradlew :composeApp:assembleRelease :composeApp:bundleRelease --stacktrace --no-daemon

    - name: Upload Release apk
      uses: actions/upload-artifact@v4
      with:
          name: app-release
          path: public/composeApp/build/outputs/apk/release/*.apk

    - name: Upload Release bundle
      uses: actions/upload-artifact@v4
      with:
          name: app-release-bundle
          path: public/composeApp/build/outputs/bundle/release/composeApp-release.aab

    - name: Upload Mapping file
      uses: actions/upload-artifact@v4
      with:
        name: mapping.txt
        path: public/composeApp/build/outputs/mapping/release/mapping.txt

    - name: Build public apk
      run: |
        ./gradlew clean --no-daemon
        cd public
        ./gradlew :composeApp:assembleDebug --stacktrace --no-daemon
