name: Build and Test
on:
    pull_request: {}
    push:
        branches: [develop, staging, master]
concurrency:
    group: ${{ github.workflow }}-${{ github.ref }}
    cancel-in-progress: true
permissions: {} # No permissions required
jobs:
    fetch:
        uses: ./.github/workflows/build_prepare.yaml
        permissions:
            contents: read
        with:
            config: ${{ (github.event.pull_request.base.ref || github.ref_name) == 'develop' && 'element.io/nightly' || 'element.io/release' }}
            version: ${{ (github.event.pull_request.base.ref || github.ref_name) == 'develop' && 'develop' || '' }}
            branch-matching: true

    windows:
        needs: fetch
        name: Windows
        uses: ./.github/workflows/build_windows.yaml
        strategy:
            matrix:
                arch: [x64, ia32, arm64]
        with:
            arch: ${{ matrix.arch }}
            blob_report: true

    linux:
        needs: fetch
        name: "Linux (${{ matrix.arch }}) (sqlcipher: ${{ matrix.sqlcipher }})"
        uses: ./.github/workflows/build_linux.yaml
        strategy:
            matrix:
                sqlcipher: [system, static]
                arch: [amd64, arm64]
        with:
            sqlcipher: ${{ matrix.sqlcipher }}
            arch: ${{ matrix.arch }}
            blob_report: true

    macos:
        needs: fetch
        name: macOS
        uses: ./.github/workflows/build_macos.yaml
        with:
            blob_report: true

    tests-done:
        needs: [windows, linux, macos]
        runs-on: ubuntu-24.04
        if: always()
        steps:
            - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5

            - uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5
              with:
                  cache: "yarn"
                  node-version: "lts/*"

            - name: Install dependencies
              run: yarn install --frozen-lockfile

            - name: Download blob reports from GitHub Actions Artifacts
              uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5
              with:
                  pattern: blob-report-*
                  path: all-blob-reports
                  merge-multiple: true

            - name: Merge into HTML Report
              run: yarn playwright merge-reports -c ./playwright.config.ts --reporter=html ./all-blob-reports

            - name: Upload HTML report
              if: always()
              uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
              with:
                  name: html-report
                  path: playwright-report
                  retention-days: 14

            - if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
              run: exit 1
