name: Android CI

on:
  push:
    branches: [ "master", "theme" ]
jobs:
  build:
    permissions: write-all
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    steps:
      - uses: actions/checkout@v4
      - name: set up JDK 17
        uses: actions/setup-java@v4
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: gradle

      - name: Write key
        if: github.event_name != 'pull_request'
        run: |
          if [ ! -z "${{ secrets.KEY_STORE }}" ]; then
          echo androidStorePassword='${{ secrets.KEY_STORE_PASSWORD }}' >> gradle.properties
          echo androidKeyAlias='${{ secrets.ALIAS }}' >> gradle.properties
          echo androidKeyPassword='${{ secrets.KEY_PASSWORD }}' >> gradle.properties
          echo androidStoreFile='key.jks' >> gradle.properties
          echo ${{ secrets.KEY_STORE }} | base64 --decode > key.jks
          fi

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

      - name: Build and test app
        run: |
          ./gradlew assembleDebug -Pminify=true

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: debug-app ${{ steps.version.outputs.builddate }}
          path: app/build/outputs/apk/debug/app-debug.apk

      - name: Post to channel
        if: ${{ success() && github.ref == 'refs/heads/master' && github.ref_type != 'tag' }}
        env:
          CHANNEL_ID: ${{ secrets.CHANNEL_ID }}
          BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
          COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
          COMMIT_URL: ${{ github.event.head_commit.url }}
        run: |
          if [ ! -z "${{ secrets.BOT_TOKEN }}" ]; then
            GITHUB_SHA_SHORT=$(git rev-parse --short HEAD)
            export output=app/WaEnhancer_debug_$GITHUB_SHA_SHORT.apk
            cp app/build/outputs/apk/debug/app-debug.apk $output
            export LOG=$(cat changelog.txt)
            ESCAPED=$(python3 -c 'import json,os,urllib.parse; msg = json.dumps(os.getenv("LOG")); print(urllib.parse.quote(msg if len(msg) <= 1024 else json.dumps(os.getenv("COMMIT_URL"))))')
            curl "https://api.telegram.org/bot${BOT_TOKEN}/sendMediaGroup?chat_id=${CHANNEL_ID}&media=%5B%7B%22type%22%3A%22document%22%2C%20%22media%22%3A%22attach%3A%2F%2Foutput%22%2C%22caption%22:${ESCAPED}%7D%5D" -F output="@$output"
          fi

      - name: Upload to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: app/build/outputs/apk/debug/app-debug.apk
          asset_name: WaEnhancer_debug_${{ github.sha }}.apk
          tag: pre-release-${{ github.sha }}