name: Reusable CI Workflow

on:
  workflow_call:
    inputs:
      upload-artifacts:
        type: boolean
        default: false
        required: false

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [22.x, 23.x]
    steps:
    - uses: actions/checkout@v4.1.7
    - uses: ./.github/actions/turbo-build 
      with:
        node-version: ${{ matrix.node-version }}

  playwright-test-extension:
    needs: [build]
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4.1.7
    - uses: ./.github/actions/turbo-build
    - name: Build extension with Manifest v3 for Chrome
      run: pnpm build:chrome
      working-directory: ./projects/extension
    - run: pnpm playwright:install
      working-directory: ./projects/extension
    - name: Test projects/extension
      run: pnpm playwright:chromium --trace on
      working-directory: ./projects/extension
    - uses: actions/upload-artifact@v4
      if: failure() || inputs.upload-artifacts
      with:
        name: playwright-report-substrate-connect-extension-mv3-chrome
        path: ./projects/extension/playwright-report

  playwright-test-examples:
    needs: [build]
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4.1.7
    - uses: ./.github/actions/turbo-build
    - run: pnpm playwright:install
      working-directory: ./examples/light-client-extension-helpers-extension
    - name: Test examples/light-client-extension-helpers-extension
      run: pnpm playwright:chromium --trace on
      working-directory: ./examples/light-client-extension-helpers-extension
    - uses: actions/upload-artifact@v4
      if: failure() || inputs.upload-artifacts
      with:
        name: playwright-report-light-client-extension-helpers-extension-mv3-chrome
        path: ./examples/light-client-extension-helpers-extension/playwright-report

  playwright-test-wallet-template:
    needs: [build]
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4.1.7
    - uses: ./.github/actions/turbo-build
    - name: Build extension with Manifest v3 for Chrome
      run: pnpm build:chrome
      working-directory: ./projects/wallet-template
    - run: pnpm playwright:install
      working-directory: ./projects/wallet-template
    - name: Test projects/wallet-template
      run: pnpm playwright:chromium --trace on
      working-directory: ./projects/wallet-template
    - uses: actions/upload-artifact@v4
      if: failure() || inputs.upload-artifacts
      with:
        name: playwright-report-substrate-connect-wallet-template-mv3-chrome
        path: ./projects/wallet-template/playwright-report

  all:
    needs: [build, playwright-test-extension, playwright-test-examples, playwright-test-wallet-template]
    runs-on: ubuntu-latest
    if: always()
    steps:
      - name: Check job status
        if: contains(needs.*.result, 'failure')
        run: exit 1
      - name: All jobs completed successfully
        if: success()
        run: echo "All jobs completed successfully!"
