name: CI
on:
  push:
    branches: [ master ]
  pull_request:
  workflow_dispatch:
  schedule:
    - cron: '0 0 * * 0' # https://crontab.guru/every-week

permissions:
  contents: read

jobs:
  build:
    permissions:
      contents: read
      actions: write
    runs-on: ubuntu-latest
    steps:
    - uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
      with:
        node-version: 24
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
    - run: npm install
    - run: npm test
    - run: npm run lint
    - if: github.ref == 'refs/heads/master' && github.event_name == 'push'
      run: npm run perf:baseline
    - if: github.ref == 'refs/heads/master' && github.event_name == 'push'
      uses: actions/cache/save@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
      with:
        path: .reassure
        key: reassure-baseline-${{ github.sha }}

  performance:
    permissions:
      contents: read
      actions: read
      issues: write
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
    - run: npm install
    - uses: actions/cache/restore@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
      id: cache-reassure
      with:
        path: .reassure
        key: reassure-baseline-${{ github.sha }}
        restore-keys: reassure-baseline-
        # TODO uncomment after merge to master and remove cache-hit == 'true' below
        # fail-on-cache-miss: true
    - if: steps.cache-reassure.outputs.cache-hit == 'true'
      run: npm run perf:compare
    - if: steps.cache-reassure.outputs.cache-hit == 'true'
      uses: hCaptcha/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e
      id: find_comment
      with:
        issue-number: ${{ github.event.pull_request.number }}
        body-includes: Performance Comparison Report
    - if: steps.cache-reassure.outputs.cache-hit == 'true'
      uses: hCaptcha/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043
      with:
        edit-mode: replace
        body-path: .reassure/output.md
        comment-id: ${{ steps.find_comment.outputs.comment-id }}
        issue-number: ${{ github.event.pull_request.number }}

  test:
    needs: build
    permissions:
      contents: read
    runs-on: ${{ matrix.os }}
    outputs:
      rn-version: ${{ steps.rn-version.outputs.value }}
    concurrency:
      group: '${{ github.workflow }}-${{ matrix.platform }}-${{ matrix.pm }}-${{ github.head_ref || github.ref_name }}'
      cancel-in-progress: true
    strategy:
      fail-fast: false
      max-parallel: 5
      matrix:
        os: [ ubuntu-latest ]
        platform: [ android ]
        pm: [ npm, yarn ]
        include:
        - os: macos-latest
          platform: ios
          pm: npm
        - os: macos-latest
          platform: ios
          pm: yarn
    steps:
    - uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
      with:
        node-version: 24
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      with:
        path: react-native-hcaptcha
    - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
      with:
        node-version: 22
    - if: matrix.platform == 'android'
      uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12 # v4.7.0
      with:
        java-version: 17
        distribution: adopt
    - if: matrix.platform == 'ios'
      run: sudo xcode-select -s /Applications/Xcode_16.4.app
    - run: npm run example -- --pm ${{ matrix.pm }}
      working-directory: react-native-hcaptcha
      env:
        YARN_ENABLE_IMMUTABLE_INSTALLS: false
    - id: rn-version
      working-directory: react-native-hcaptcha-example
      run: |
        RN_VERSION=$(jq -r ".dependencies.\"react-native\"" package.json)
        echo "value=${RN_VERSION}" >> $GITHUB_OUTPUT
    - run: yarn test --config ./jest.config.js
      working-directory: react-native-hcaptcha-example
    - run: npx react-native build-${{ matrix.platform }}
      working-directory: react-native-hcaptcha-example
    - run: npx --yes check-peer-dependencies --yarn --runOnlyOnRootDependencies
      working-directory: react-native-hcaptcha-example

  e2e:
    needs: build
    permissions:
      contents: read
    runs-on: ${{ matrix.os }}
    concurrency:
      group: 'e2e-${{ matrix.platform }}-${{ github.head_ref || github.ref_name }}'
      cancel-in-progress: true
    strategy:
      fail-fast: false
      matrix:
        include:
        - os: ubuntu-latest
          platform: android
        - os: macos-latest
          platform: ios
    steps:
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
    - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
      with:
        node-version: 22
    - if: matrix.platform == 'android'
      uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12 # v4.7.0
      with:
        java-version: 17
        distribution: adopt
    - run: npm install
    - name: Generate E2E host app
      run: npm run test:e2e:setup -- --pm npm --platform ${{ matrix.platform }}

    - if: matrix.platform == 'android'
      name: Run Android E2E tests
      uses: hCaptcha/hcaptcha-android-sdk/.github/actions/android-emulator-run@dc2e0fc978424322c977b68ae06f8dd54e571e22
      with:
        target: google_apis
        api-level: '34'
        profile: pixel_7
        script: npm run test:e2e:android

    - if: matrix.platform == 'ios'
      name: Boot iOS simulator
      run: |
        UDID=$(xcrun simctl list devices available --json | \
          jq -r '.devices["com.apple.CoreSimulator.SimRuntime.iOS-18-5"][] | select(.name=="iPhone 16") | .udid')
        echo "Booting iPhone 16 (iOS 18.5): $UDID"
        xcrun simctl bootstatus "$UDID" -b

    - if: matrix.platform == 'ios'
      name: Run iOS E2E tests
      run: npm run test:e2e:ios

    - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
      if: always()
      with:
        name: e2e-results-${{ matrix.platform }}
        path: __e2e__/__image_snapshots__/
        retention-days: 14

  create-an-issue:
    permissions:
      contents: read
      issues: write
    runs-on: ubuntu-latest
    needs: test
    if: always() && github.event_name == 'schedule' && needs.test.result == 'failure'
    steps:
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
    - run: |
        RN_VERSION="${{ needs.test.outputs.rn-version }}"
        GHA_RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
        echo "RN_VERSION=${RN_VERSION}" >> $GITHUB_ENV
        echo "GHA_RUN_URL=${GHA_RUN_URL}" >> $GITHUB_ENV
    - uses: hCaptcha/create-an-issue@56fdd2d6f960e970fa9d5ca3cf3884b6ba5af477
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        assignees: CAMOBAP
        update_existing: true
        filename: .github/examples-issue-template.md
