# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: PR check

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

jobs:
  analyze-and-test:
    name: Analyze & test
    runs-on: macos-14
    timeout-minutes: 10
    env:
      EMPTY_CONSTANTS: Y29uc3QgU3RyaW5nIGlhcFNlcnZlclVybCA9ICcnOwpjb25zdCBTdHJpbmcgaXNzdWVzUmVwb3J0VXJsID0gJyc7CmNvbnN0IFN0cmluZyBzb3VyY2VDb2RlVXJsID0gJyc7CmNvbnN0IFN0cmluZyBjb250YWN0RW1haWwgPSAnJzsK
      EMPTY_FIREBASE_OPTIONS: Ly8gaWdub3JlX2Zvcl9maWxlOiB0eXBlPWxpbnQKaW1wb3J0ICdwYWNrYWdlOmZpcmViYXNlX2NvcmUvZmlyZWJhc2VfY29yZS5kYXJ0JyBzaG93IEZpcmViYXNlT3B0aW9uczsKY2xhc3MgRGVmYXVsdEZpcmViYXNlT3B0aW9ucyB7c3RhdGljIEZpcmViYXNlT3B0aW9ucyBnZXQgY3VycmVudFBsYXRmb3JtID0+RmlyZWJhc2VPcHRpb25zKGFwaUtleTogJycsIGFwcElkOiAnJywgbWVzc2FnaW5nU2VuZGVySWQ6ICcnLCBwcm9qZWN0SWQ6ICcnKTt9Cg==
    steps:
      - uses: 8BitJonny/gh-get-current-pr@2.2.0
        id: PR

      - uses: actions/checkout@v3
        with:
          submodules: recursive

      - name: Connect private iap package
        uses: webfactory/ssh-agent@v0.8.0
        id: fetch-iap
        if: steps.PR.outputs.number == 'null' || github.event.pull_request.head.repo.full_name == github.repository
        with:
          ssh-private-key: ${{ secrets.M3_LIGHTMETER_IAP_KEY }}

      - name: Override iap package with stub
        id: override-iap
        if: steps.fetch-iap.conclusion != 'success'
        run: bash ./.github/scripts/stub_iap.sh

      - name: Restore secrets
        env:
          CONSTANTS: ${{ steps.fetch-iap.conclusion == 'success' && secrets.CONSTANTS || env.EMPTY_CONSTANTS }}
          FIREBASE_OPTIONS: ${{ steps.fetch-iap.conclusion == 'success' && secrets.FIREBASE_OPTIONS || env.EMPTY_FIREBASE_OPTIONS }}
        run: |
          bash .github/scripts/restore_from_base64.sh "${{ env.CONSTANTS }}" "lib/constants.dart"
          bash .github/scripts/restore_from_base64.sh "${{ env.FIREBASE_OPTIONS }}" "lib/firebase_options.dart"

      - uses: subosito/flutter-action@v2
        with:
          channel: "stable"
          flutter-version: "3.24.5"

      - name: Prepare flutter project
        run: |
          flutter --version
          flutter pub get
          flutter pub run intl_utils:generate

      - name: Analyze project source
        run: flutter analyze lib --fatal-infos

      - name: Run tests
        run: |
          defaults -currentHost write -g AppleFontSmoothing -int 0
          flutter test --dart-define cameraStubImage=assets/camera_stub_image.jpg

      - name: Analyze project source with stub
        if: steps.override-iap.conclusion != 'success'
        run: |
          bash ./.github/scripts/stub_iap.sh
          flutter pub get
          flutter analyze lib --fatal-infos

  platform-changes:
    name: Checks for platform changes
    runs-on: ubuntu-latest
    outputs:
      android-changed: ${{ steps.platform-changes.outputs.android-changed }}
      ios-changed: ${{ steps.platform-changes.outputs.ios-changed }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive

      - id: platform-changes
        uses: dorny/paths-filter@v2
        with:
          filters: |
            android-changed:
              - 'android/**'
              - 'pubspec.yaml'
            ios-changed:
              - 'ios/**'
              - 'pubspec.yaml'

  build-android:
    name: Build Android
    needs: platform-changes
    if: needs.platform-changes.outputs.android-changed == 'true'
    uses: ./.github/workflows/build_apk.yml
    secrets: inherit
    with:
      binary-type: apk
      flavor: prod
      upload-artifact: false
      stage-backend: false
      version: "1.0.0"

  build-ios:
    name: Build iOS
    needs: platform-changes
    if: needs.platform-changes.outputs.ios-changed == 'true'
    uses: ./.github/workflows/build_ipa.yml
    secrets: inherit
    with:
      upload-artifact: false
      stage-backend: false
      version: "1.0.0"
