name: Run automated checks

on:
  pull_request:
  workflow_call:

permissions:
  contents: read

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: 'gradle'
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew
      - name: Lint checks
        run: bundle exec fastlane lint
      - name: Archive lint reports
        uses: actions/upload-artifact@v4
        if: ${{ always() }}
        with:
          name: lint-reports
          path: app/build/reports/*.html

  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        flavor: [Googleplay, Generic]
    steps:
      - name: checkout
        uses: actions/checkout@v4
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: 'gradle'
      - name: Enable KVM
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm
      - name: run tests
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 29
          script: ./gradlew -Pcoverage app:test${{ matrix.flavor }}DebugUnitTest app:connected${{ matrix.flavor }}DebugAndroidTest
      - name: Upload test reports
        uses: actions/upload-artifact@v4
        if: ${{ always() }}
        with:
          name: test-reports-${{ matrix.flavor }}
          path: app/build/reports/**
