name: CI
on:
  push:
    branches:
      # - develop
      - master
    paths:
      - '**'
      - '!**.md'
  # pull_request:
  #   paths-ignore:
  #     - '**.md'
  # Dispatch or Manual triggers
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  build:
    name: Build app
    runs-on: 'ubuntu-24.04'

    steps:
      - name: Clone repo
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          fetch-depth: 0

      - name: Set up JDK
        uses: actions/setup-java@1bcf9fb12cf4aa7d266a90ae39939e61372fe520 # v5.4.0
        with:
          java-version: 17
          distribution: temurin

      - name: Write google-services.json
        uses: DamianReeves/write-file-action@6929a9a6d1807689191dcc8bbe62b54d70a32b42 # v1.3
        with:
          path: app/google-services.json
          contents: ${{ secrets.GOOGLE_SERVICES_JSON }}
          write-mode: overwrite

      - name: Write client_secrets.json
        uses: DamianReeves/write-file-action@6929a9a6d1807689191dcc8bbe62b54d70a32b42 # v1.3
        with:
          path: app/src/main/assets/client_secrets.json
          contents: ${{ secrets.GOOGLE_CLIENT_SECRETS_JSON }}
          write-mode: overwrite

      - name: Set up gradle
        uses: gradle/actions/setup-gradle@0723195856401067f7a2779048b490ace7a47d7c # v5

      - name: Check code format
        run: ./gradlew spotlessCheck

      - name: Build app
        run: ./gradlew assemblePreview -Pinclude-telemetry -Penable-updater

      - name: Run unit tests
        id: unit_tests
        run: ./gradlew testReleaseUnitTest

      - name: Upload test report
        if: steps.unit_tests.outcome == 'failure'
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: test-report-${{ github.sha }}
          path: app/build/reports/tests/testReleaseUnitTest

      - name: Sign APK
        uses: r0adkll/sign-android-release@f30bdd30588842ac76044ecdbd4b6d0e3e813478
        with:
          releaseDirectory: app/build/outputs/apk/preview
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: '35.0.1'

      - name: Rename apk
        id: current_commit
        run: |
          set -e
          version_tag=$(echo ${GITHUB_REF/refs\/heads\//} | sed -r 's/^refs\/(heads|tags)\///' | sed -r 's/[-\/]+/_/g')
          commit_count=$(git rev-list --count HEAD)
          echo "VERSION_TAG=$version_tag"
          echo "VERSION_TAG=$version_tag" >> $GITHUB_OUTPUT
          echo "COMMIT_COUNT=$commit_count"
          echo "COMMIT_COUNT=$commit_count" >> $GITHUB_OUTPUT
          mv app/build/outputs/apk/preview/app-universal-preview-signed.apk Komikku-$version_tag-r$commit_count.apk

      - name: Upload APK
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: Komikku-${{ steps.current_commit.outputs.VERSION_TAG }}-r${{ steps.current_commit.outputs.COMMIT_COUNT }}.apk
          path: ./*.apk

      - name: Upload mapping
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: mapping-${{ github.sha }}
          path: app/build/outputs/mapping/preview
