name: Build and Release Android APK

on:
  push:
    branches: [ main, master ]
    tags:
      - 'v*'
  workflow_dispatch:

# Add permissions needed to create releases
permissions:
  contents: write
  packages: read

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

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

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

      - name: Get version name from build.gradle
        id: version
        run: |
          VERSION_NAME=$(grep -o 'versionName = "[^"]*' app/build.gradle.kts | cut -d'"' -f2)
          echo "VERSION_NAME=$VERSION_NAME" >> $GITHUB_ENV
          echo "APP_VERSION=$VERSION_NAME" >> $GITHUB_OUTPUT

      - name: Build Release APK
        run: ./gradlew assembleRelease

      - name: Rename APK for tag release
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          mkdir -p renamed_apk
          cp app/build/outputs/apk/release/app-release-unsigned.apk renamed_apk/TetriStats-${{ env.VERSION_NAME }}.apk

      - name: Rename APK for push release
        if: ${{ !startsWith(github.ref, 'refs/tags/') && github.event_name == 'push' }}
        run: |
          mkdir -p renamed_apk
          cp app/build/outputs/apk/release/app-release-unsigned.apk renamed_apk/TetriStats-${{ env.VERSION_NAME }}-${{ github.run_number }}.apk

      - name: Upload APK as artifact for tag release
        if: startsWith(github.ref, 'refs/tags/')
        uses: actions/upload-artifact@v4
        with:
          name: TetriStats-${{ env.VERSION_NAME }}
          path: renamed_apk/TetriStats-${{ env.VERSION_NAME }}.apk
      
      - name: Upload APK as artifact for push release
        if: ${{ !startsWith(github.ref, 'refs/tags/') && github.event_name == 'push' }}
        uses: actions/upload-artifact@v4
        with:
          name: TetriStats-${{ env.VERSION_NAME }}-${{ github.run_number }}
          path: renamed_apk/TetriStats-${{ env.VERSION_NAME }}-${{ github.run_number }}.apk
      
      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          name: Release ${{ env.VERSION_NAME }}
          draft: false
          prerelease: false
          files: renamed_apk/TetriStats-${{ env.VERSION_NAME }}.apk
          body: |
            TetriStats Android App Release v${{ env.VERSION_NAME }}
            
            Automatically generated release from GitHub Actions.
            
            Download the APK from the assets below.
          
      - name: Create Release on Push
        id: create_release_on_push
        if: ${{ !startsWith(github.ref, 'refs/tags/') && github.event_name == 'push' }}
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ env.VERSION_NAME }}-${{ github.run_number }}
          name: Release v${{ env.VERSION_NAME }}-${{ github.run_number }}
          draft: false
          prerelease: false
          files: renamed_apk/TetriStats-${{ env.VERSION_NAME }}-${{ github.run_number }}.apk
          body: |
            TetriStats Android App Release v${{ env.VERSION_NAME }}-${{ github.run_number }}
            
            Automatically generated release from GitHub Actions.
            
            Download the APK from the assets below. 