name: build

on:
  push:
    branches: [main]
    tags: ['v*']     # Triggers for tags like v1.0, v2.1.4, etc.
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write # Crucial for creating GitHub Releases
    steps:
      - name: Checkout code
        uses: actions/checkout@v4 # Note: @v4 is the current stable version

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: "21"
          distribution: "temurin"
          cache: gradle

      - name: Setup Gradle cache cleanup
        uses: gradle/gradle-build-action@v2
        with:
          gradle-home-cache-cleanup: true

      # - name: Setup Gradle
      #   uses: gradle/actions/setup-gradle@v3
      #   with:
      #       gradle-home-cache-cleanup: true

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

      - name: Build Debug APK
        if: github.event_name == 'pull_request' || (github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/'))
        run: ./gradlew assembleDebug --stacktrace

      - name: Upload Debug APK
        if: github.event_name == 'pull_request' || (github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/'))
        uses: actions/upload-artifact@v4
        with:
          name: debug-apk
          path: app/build/exported-apks/**/*.apk

      # --- RELEASE SECTION (Runs ONLY when a tag is pushed) ---
      - name: Decode Keystore
        if: startsWith(github.ref, 'refs/tags/')
        env:
          KEYSTORE_BASE64: ${{ secrets.KEYSTORE_BASE64 }}
        run: echo "$KEYSTORE_BASE64" | base64 --decode > release.jks

      # - name: Lint
      #   if: startsWith(github.ref, 'refs/tags/')
      #   run: ./gradlew lintFreeRelease lintPlayRelease -PstrictLint

      - name: Build Release APK
        if: startsWith(github.ref, 'refs/tags/')
        run: >
          ./gradlew assembleRelease
          -Pandroid.injected.signing.store.file=$(pwd)/release.jks
          -Pandroid.injected.signing.store.password='${{ secrets.KEYSTORE_PASSWORD }}'
          -Pandroid.injected.signing.key.alias='${{ secrets.KEY_ALIAS }}'
          -Pandroid.injected.signing.key.password='${{ secrets.KEY_PASSWORD }}'

      - name: Upload Release Artifact
        if: startsWith(github.ref, 'refs/tags/')
        uses: actions/upload-artifact@v4
        with:
          name: release-apk
          path: app/build/exported-apks/**/*.apk

      - name: Create GitHub Release
        if: startsWith(github.ref, 'refs/tags/')
        uses: softprops/action-gh-release@v2
        with:
          files: |
            app/build/exported-apks/freeRelease/*.apk
            app/build/exported-apks/playRelease/*.apk
          name: "Release ${{ github.ref_name }}"
          tag_name: ${{ github.ref_name }}
          generate_release_notes: true
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Clean up Keystore
        if: always() && startsWith(github.ref, 'refs/tags/')
        run: rm -f release.jks
