name: Release APK

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 60

    steps:
      - uses: actions/checkout@v4

      - name: Setup Java 17
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          channel: stable

      - name: Setup Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-linux-android,armv7-linux-androideabi,x86_64-linux-android

      - name: Install Android NDK
        run: echo "y" | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "ndk;27.0.12077973"

      - name: Install dependencies
        run: flutter pub get

      - name: Generate localizations
        run: flutter gen-l10n

      - name: Build release APKs (split per ABI)
        run: flutter build apk --release --split-per-abi

      - name: Build universal APK
        run: flutter build apk --release

      - name: Rename APKs
        run: |
          cd build/app/outputs/flutter-apk
          mv app-arm64-v8a-release.apk elcaju-${{ github.ref_name }}-arm64.apk
          mv app-armeabi-v7a-release.apk elcaju-${{ github.ref_name }}-armeabi-v7a.apk
          mv app-x86_64-release.apk elcaju-${{ github.ref_name }}-x86_64.apk
          mv app-release.apk elcaju-${{ github.ref_name }}-universal.apk
          cp elcaju-${{ github.ref_name }}-universal.apk elcaju-universal.apk

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            build/app/outputs/flutter-apk/elcaju-${{ github.ref_name }}-arm64.apk
            build/app/outputs/flutter-apk/elcaju-${{ github.ref_name }}-armeabi-v7a.apk
            build/app/outputs/flutter-apk/elcaju-${{ github.ref_name }}-x86_64.apk
            build/app/outputs/flutter-apk/elcaju-${{ github.ref_name }}-universal.apk
            build/app/outputs/flutter-apk/elcaju-universal.apk
          generate_release_notes: true
