name: Build Shamrock APK

on:
  workflow_dispatch:
  push:
    branches: [ master ]
    paths-ignore:
      - '**.md'
      - '**.txt'
      - '.github/**'
      - '.idea/**'
      - '!.github/workflows/**'

jobs:
  build:
    name: Build Shamrock
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup JDK 17
        uses: actions/setup-java@v4
        with:
          java-version: 17
          distribution: "adopt"

      - name: Cache Gradle Dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
            !~/.gradle/caches/build-cache-*
          key: gradle-deps-core-${{ hashFiles('**/build.gradle.kts') }}
          restore-keys: gradle-deps

      - name: Cache Gradle Build
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches/build-cache-*
            ~/.gradle/buildOutputCleanup/cache.properties
          key: gradle-builds-core-${{ github.sha }}
          restore-keys: gradle-builds

      - name: Build with Gradle
        run: |
          echo ${{ secrets.SIGN_KEYSTORE_BASE64 }} | base64 -d > keystore.jks
          chmod +x ./gradlew
          ./gradlew :app:assembleRelease --build-cache --parallel --daemon --warning-mode all --stacktrace
          echo "APK_FILE_ALL=$(find app/build/outputs/apk/app/release -name '*.apk')" >> $GITHUB_ENV
          echo "APK_FILE_ARM64=$(find app/build/outputs/apk/arm64/release -name '*.apk')" >> $GITHUB_ENV
          echo "APK_FILE_X86_64=$(find app/build/outputs/apk/x64/release -name '*.apk')" >> $GITHUB_ENV
        env:
          KEYSTORE_PATH: "../keystore.jks"
          KEYSTORE_PASSWORD: ${{ secrets.SIGN_KEYSTORE_PASSWORD }}
          KEY_ALIAS: ${{ secrets.SIGN_ALIAS }}
          KEY_PASSWORD: ${{ secrets.SIGN_KEY_PASSWORD }}

      - name: Set Shamrock Version
        run: |
          version_name_all=$(basename -s .apk "${{ env.APK_FILE_ALL }}")
          version_name_arm64=$(basename -s .apk "${{ env.APK_FILE_ARM64 }}")
          version_name_x86_64=$(basename -s .apk "${{ env.APK_FILE_X86_64 }}")
          echo "SHAMROCK_VERSION_ALL=$version_name_all" >> $GITHUB_ENV
          echo "SHAMROCK_VERSION_ARM64=$version_name_arm64" >> $GITHUB_ENV
          echo "SHAMROCK_VERSION_x86_64=$version_name_x86_64" >> $GITHUB_ENV

      - name: Show Artifacts SHA256
        run: |
          echo "### Build Success :rocket:" >> $GITHUB_STEP_SUMMARY
          echo "|ABI|SHA256|" >> $GITHUB_STEP_SUMMARY
          echo "|:--------:|:----------|" >> $GITHUB_STEP_SUMMARY
          all=($(sha256sum "${{ env.APK_FILE_ALL }}"))
          echo "|all|$all" >> $GITHUB_STEP_SUMMARY
          arm64=($(sha256sum "${{ env.APK_FILE_ARM64 }}"))
          echo "|arm64|$arm64" >> $GITHUB_STEP_SUMMARY
          x86_64=($(sha256sum "${{ env.APK_FILE_X86_64 }}"))
          echo "|x86_64|$x86_64" >> $GITHUB_STEP_SUMMARY

      - name: Upload ALL APK RELEASE
        uses: actions/upload-artifact@v4
        with:
          name: "${{ env.SHAMROCK_VERSION_ALL }}"
          path: "${{ env.APK_FILE_ALL }}"

      - name: Upload ARM64 APK RELEASE
        uses: actions/upload-artifact@v4
        with:
          name: "${{ env.SHAMROCK_VERSION_ARM64 }}"
          path: "${{ env.APK_FILE_ARM64 }}"

      - name: Upload X86_64 APK RELEASE
        uses: actions/upload-artifact@v4
        with:
          name: "${{ env.SHAMROCK_VERSION_x86_64 }}"
          path: "${{ env.APK_FILE_X86_64 }}"