name: Build
run-name: ${{ gitea.actor }} build 🚀

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout source
        uses: actions/checkout@v5

      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: zulu
          java-version: 17

      - name: Setup Android SDK
        uses: amyu/setup-android@v5
        with:
          cache-disabled: true

      - name: Grant execute permissions
        run: |
          chmod +x ./gradlew
          chmod +x ./script/release-uploader

      - name: Download AAR manually
        run: |
          FILE="app/libs/firefly-go.aar"
          URL="https://git.kain.io.vn/Firefly-Shelter/FireflyGo_Xposed/media/branch/master/app/libs/firefly-go.aar"
          echo "📥 Downloading $FILE from $URL"
          curl -L -o "$FILE" "$URL"

      - name: Build debug APK
        run: ./gradlew assembleDebug

      - name: Find and rename APK
        run: |
          APK_FILE=$(ls app/build/outputs/apk/debug/*.apk | head -n 1)
          echo "Found APK: $APK_FILE"
          mv "$APK_FILE" app/build/outputs/apk/debug/firefly_go_android.apk

      - name: Upload release
        env:
          REPO_TOKEN: ${{ secrets.REPO_TOKEN }}
        run: |
          script/release-uploader \
            -token=$REPO_TOKEN \
            -release-url="https://git.kain.io.vn/api/v1/repos/Firefly-Shelter/FireflyGo_Xposed/releases" \
            -files="app/build/outputs/apk/debug/firefly_go_android.apk"