name: Code Check

on:
    push:
        branches:
            - dev
        paths-ignore:
            - "docs/badges/**"
    pull_request:
        branches:
            - dev
    workflow_dispatch:
        inputs:
            run_lint:
                description: "Run lint/format checks for Go, Flutter, adminfront, devfront, orgfront"
                required: true
                type: boolean
                default: true
            run_backend_tests:
                description: "Run backend Go tests"
                required: true
                type: boolean
                default: true
            run_userfront_tests:
                description: "Run userfront Flutter tests"
                required: true
                type: boolean
                default: true
            run_userfront_coverage:
                description: "Run userfront Flutter coverage and upload LCOV report"
                required: true
                type: boolean
                default: true
            run_userfront_e2e_tests:
                description: "Run userfront WASM Playwright E2E tests"
                required: true
                type: boolean
                default: true
            run_userfront_e2e_full:
                description: "Run full userfront E2E browser matrix instead of Chromium fast lane"
                required: true
                type: boolean
                default: false
            userfront_e2e_workers:
                description: "Playwright worker count for userfront E2E tests"
                required: true
                type: string
                default: "2"
            run_adminfront_tests:
                description: "Run adminfront Playwright tests"
                required: true
                type: boolean
                default: true
            run_devfront_tests:
                description: "Run devfront Playwright tests"
                required: true
                type: boolean
                default: true
            run_orgfront_tests:
                description: "Run orgfront Playwright tests"
                required: true
                type: boolean
                default: true
            run_front_coverage:
                description: "Run adminfront/devfront/orgfront Vitest coverage and upload reports"
                required: true
                type: boolean
                default: true

permissions:
    contents: write

jobs:
    changes:
        runs-on: ubuntu-latest
        outputs:
            any: ${{ steps.filter.outputs.any }}
            lint: ${{ steps.filter.outputs.lint }}
            biome: ${{ steps.filter.outputs.biome }}
            backend: ${{ steps.filter.outputs.backend }}
            userfront: ${{ steps.filter.outputs.userfront }}
            userfront_coverage: ${{ steps.filter.outputs.userfront_coverage }}
            userfront_e2e: ${{ steps.filter.outputs.userfront_e2e }}
            front_coverage: ${{ steps.filter.outputs.front_coverage }}
            adminfront: ${{ steps.filter.outputs.adminfront }}
            devfront: ${{ steps.filter.outputs.devfront }}
            orgfront: ${{ steps.filter.outputs.orgfront }}
        steps:
            - name: Checkout code
              uses: actions/checkout@v4
              with:
                  fetch-depth: 0

            - name: Detect changed areas
              id: filter
              run: |
                  set -euo pipefail

                  set_output() {
                    echo "$1=$2" >> "$GITHUB_OUTPUT"
                  }

                  if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
                    for key in any lint biome backend userfront userfront_coverage userfront_e2e front_coverage adminfront devfront orgfront; do
                      set_output "$key" true
                    done
                    exit 0
                  fi

                  base="${{ github.event.before }}"
                  if [ "${{ github.event_name }}" = "pull_request" ]; then
                    base="${{ github.event.pull_request.base.sha }}"
                  fi

                  if [ -z "$base" ] || ! git cat-file -e "$base^{commit}" 2>/dev/null; then
                    base="$(git rev-parse HEAD^ 2>/dev/null || true)"
                  fi

                  if [ -n "$base" ]; then
                    changed_files="$(git diff --name-only "$base" HEAD)"
                  else
                    changed_files="$(git ls-files)"
                  fi

                  echo "Changed files:"
                  printf '%s\n' "$changed_files"

                  matches() {
                    printf '%s\n' "$changed_files" | grep -Eq "$1"
                  }

                  global='^(\.gitea/workflows/code_check\.yml|Makefile|scripts/|tools/|test/code_check_)'
                  front_shared='^(common/|scripts/playwrightPackageVersion\.cjs|scripts/summarize_vitest_coverage\.mjs|scripts/run_adminfront_ci_tests\.sh|\.gitea/workflows/code_check\.yml|Makefile)'
                  i18n_shared='^(common/locales/|userfront/assets/translations/|scripts/sync_userfront_locales\.sh|tools/i18n-scanner/)'

                  backend=false
                  userfront=false
                  userfront_coverage=false
                  userfront_e2e=false
                  adminfront=false
                  devfront=false
                  orgfront=false
                  front_coverage=false
                  biome=false

                  if matches "$global|^backend/"; then backend=true; fi
                  if matches "$global|$i18n_shared|^userfront/"; then userfront=true; fi
                  if matches "$global|$i18n_shared|^userfront/|^scripts/summarize_flutter_coverage\.mjs"; then userfront_coverage=true; fi
                  if matches "$global|$i18n_shared|^userfront/|^userfront-e2e/"; then userfront_e2e=true; fi
                  if matches "$front_shared|^adminfront/"; then adminfront=true; fi
                  if matches "$front_shared|^devfront/"; then devfront=true; fi
                  if matches "$front_shared|^orgfront/"; then orgfront=true; fi
                  if matches "$front_shared|^adminfront/|^devfront/|^orgfront/"; then front_coverage=true; fi
                  if matches "$front_shared|^adminfront/|^devfront/|^orgfront/"; then biome=true; fi

                  lint=false
                  if [ "$backend" = true ] || [ "$userfront" = true ] || [ "$adminfront" = true ] || [ "$devfront" = true ] || [ "$orgfront" = true ] || matches "$i18n_shared"; then
                    lint=true
                  fi

                  any=false
                  for value in "$lint" "$biome" "$backend" "$userfront" "$userfront_coverage" "$userfront_e2e" "$front_coverage" "$adminfront" "$devfront" "$orgfront"; do
                    if [ "$value" = true ]; then any=true; fi
                  done

                  set_output any "$any"
                  set_output lint "$lint"
                  set_output biome "$biome"
                  set_output backend "$backend"
                  set_output userfront "$userfront"
                  set_output userfront_coverage "$userfront_coverage"
                  set_output userfront_e2e "$userfront_e2e"
                  set_output front_coverage "$front_coverage"
                  set_output adminfront "$adminfront"
                  set_output devfront "$devfront"
                  set_output orgfront "$orgfront"

    lint:
        needs: changes
        if: ${{ needs.changes.outputs.lint == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_lint == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: i18n resource check
              run: |
                  mkdir -p reports
                  node tools/i18n-scanner/index.js
                  node tools/i18n-scanner/report.js
                  cat reports/i18n-report.txt

            - name: i18n value quality check
              run: |
                  mkdir -p reports
                  node tools/i18n-scanner/value-check.js
                  cat reports/i18n-value-report.txt

            - name: Setup Go
              uses: actions/setup-go@v5
              with:
                  go-version: "1.25"
                  cache-dependency-path: backend/go.sum

            - name: Setup Flutter
              uses: subosito/flutter-action@v2
              with:
                  channel: "stable"
                  cache: true

            - name: Install adminfront dependencies
              run: |
                  cd adminfront
                  npx pnpm install -C ../common --no-frozen-lockfile
                  npx pnpm install --no-frozen-lockfile

            - name: Biome check adminfront (lint + format)
              run: |
                  cd adminfront
                  npx biome check . --formatter-enabled=false --assist-enabled=false
                  npx biome check . --linter-enabled=false --assist-enabled=false

            - name: Install devfront dependencies
              run: |
                  cd devfront
                  npx pnpm install -C ../common --no-frozen-lockfile
                  npx pnpm install --no-frozen-lockfile

            - name: Biome check devfront (lint + format)
              run: |
                  cd devfront
                  npx biome check . --formatter-enabled=false --assist-enabled=false
                  npx biome check . --linter-enabled=false --assist-enabled=false

            - name: Install orgfront dependencies
              run: |
                  cd orgfront
                  npx pnpm install -C ../common --no-frozen-lockfile
                  npx pnpm install --no-frozen-lockfile

            - name: Biome check orgfront (lint + format)
              run: |
                  cd orgfront
                  npx biome check . --formatter-enabled=false --assist-enabled=false
                  npx biome check . --linter-enabled=false --assist-enabled=false

            - name: Lint Go backend
              run: |
                  docker run --rm \
                    -v "${PWD}/backend:/app" \
                    -w /app \
                    golangci/golangci-lint:v2.10.1 \
                    golangci-lint fmt -E gofmt -E gofumpt -d

            - name: Sync userfront locales
              run: |
                  /bin/sh ./scripts/sync_userfront_locales.sh

            - name: Install Userfront dependencies
              run: |
                  cd userfront
                  flutter pub get

            - name: Format Flutter userfront
              run: |
                  cd userfront
                  dart format --output=none --set-exit-if-changed lib test

            - name: Analyze Flutter userfront
              run: |
                  cd userfront
                  flutter analyze --no-fatal-warnings --no-fatal-infos

    biome-check:
        needs: changes
        if: ${{ needs.changes.outputs.biome == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_lint == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: Install adminfront dependencies
              run: |
                  cd adminfront
                  npx pnpm install -C ../common --no-frozen-lockfile
                  npx pnpm install --no-frozen-lockfile

            - name: Biome check adminfront
              run: |
                  cd adminfront
                  npx biome check . --formatter-enabled=false --assist-enabled=false
                  npx biome check . --linter-enabled=false --assist-enabled=false

            - name: Install devfront dependencies
              run: |
                  cd devfront
                  npx pnpm install -C ../common --no-frozen-lockfile
                  npx pnpm install --no-frozen-lockfile

            - name: Biome check devfront
              run: |
                  cd devfront
                  npx biome check . --formatter-enabled=false --assist-enabled=false
                  npx biome check . --linter-enabled=false --assist-enabled=false

            - name: Install orgfront dependencies
              run: |
                  cd orgfront
                  npx pnpm install -C ../common --no-frozen-lockfile
                  npx pnpm install --no-frozen-lockfile

            - name: Biome check orgfront
              run: |
                  cd orgfront
                  npx biome check . --formatter-enabled=false --assist-enabled=false
                  npx biome check . --linter-enabled=false --assist-enabled=false

    backend-tests:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.backend == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_backend_tests == true) }}
        runs-on: ubuntu-latest
        services:
            redis:
                image: redis:7-alpine
                options: >
                    --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
            clickhouse:
                image: clickhouse/clickhouse-server:24.6
                options: >
                    --health-cmd "wget -qO- 'http://localhost:8123/ping'" --health-interval 10s --health-timeout 5s --health-retries 5

        env:
            REDIS_ADDR: redis:6379
            CLICKHOUSE_HOST: clickhouse
            CLICKHOUSE_PORT_NATIVE: 9000

        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Go
              uses: actions/setup-go@v5
              with:
                  go-version: "1.25"
                  cache-dependency-path: backend/go.sum

            - name: Run backend tests
              run: |
                  mkdir -p reports
                  set +e
                  cd backend
                  go test -v -coverprofile=../reports/backend-coverage.out -covermode=atomic \
                    ./internal/domain \
                    ./internal/pagination \
                    ./internal/response \
                    ./internal/utils \
                    ./internal/validator 2>&1 | tee ../reports/backend-coverage.log
                  coverage_exit_code=${PIPESTATUS[0]}

                  if [ "$coverage_exit_code" -eq 0 ]; then
                    coverage_percent="$(go tool cover -func=../reports/backend-coverage.out | awk '/^total:/ { gsub(/%/, "", $3); print $3 }')"
                    node -e "const fs = require('node:fs'); const statements = Number(process.argv[1]); fs.writeFileSync('../reports/backend-coverage-summary.json', JSON.stringify({ package: 'backend', statements }, null, 2) + '\n');" "$coverage_percent"
                    {
                      echo "# Backend Coverage Summary"
                      echo
                      echo "| Package | Statements |"
                      echo "| --- | ---: |"
                      printf '| backend | %.2f%% |\n' "$coverage_percent"
                      echo
                    } > ../reports/backend-coverage-summary.md
                    cat ../reports/backend-coverage-summary.md >> "$GITHUB_STEP_SUMMARY"
                  fi

                  go test -v ./... 2>&1 | tee ../reports/backend-test.log
                  test_exit_code=${PIPESTATUS[0]}
                  cd ..

                  if [ "$coverage_exit_code" -ne 0 ] || [ "$test_exit_code" -ne 0 ]; then
                    {
                      echo "# Backend Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`backend-tests\`"
                      echo "- Coverage Exit Code: \`$coverage_exit_code\`"
                      echo "- Test Exit Code: \`$test_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`go test -v ./...\`"
                      echo
                      if [ -f reports/backend-coverage.log ]; then
                        echo "## Coverage Log Tail (last 200 lines)"
                        echo '```text'
                        tail -n 200 reports/backend-coverage.log
                        echo '```'
                        echo
                      fi
                      echo "## Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/backend-test.log
                      echo '```'
                    } > reports/backend-test-failure-report.md
                  fi

                  if [ "$coverage_exit_code" -ne 0 ]; then
                    exit "$coverage_exit_code"
                  fi
                  exit "$test_exit_code"

            - name: Publish backend failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/backend-test-failure-report.md ]; then
                    cat reports/backend-test-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload backend failure report artifact
              if: ${{ always() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: backend-coverage-report
                  path: |
                      reports/backend-test-failure-report.md
                      reports/backend-test.log
                      reports/backend-coverage.log
                      reports/backend-coverage.out
                      reports/backend-coverage-summary.json
                      reports/backend-coverage-summary.md
                  if-no-files-found: ignore

    userfront-tests:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.userfront == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_userfront_tests == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Flutter
              uses: subosito/flutter-action@v2
              with:
                  channel: "stable"
                  cache: true

            - name: Sync userfront locales
              run: |
                  /bin/sh ./scripts/sync_userfront_locales.sh

            - name: Run userfront tests
              run: |
                  cd userfront
                  if [ -d test ]; then
                    mkdir -p ../reports
                    set +e
                    flutter test 2>&1 | tee ../reports/userfront-test.log
                    test_exit_code=${PIPESTATUS[0]}
                    set -e

                    if [ "$test_exit_code" -ne 0 ]; then
                      {
                        echo "# Userfront Test Failure Report"
                        echo
                        echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                        echo "- Job: \`userfront-tests\`"
                        echo "- Exit Code: \`$test_exit_code\`"
                        echo
                        echo "## Command"
                        echo "\`flutter test\`"
                        echo
                        if [ -f ../reports/userfront-test.log ]; then
                          echo "## Test Log Tail (last 200 lines)"
                          echo '```text'
                          tail -n 200 ../reports/userfront-test.log
                          echo '```'
                        fi
                      } > ../reports/userfront-test-failure-report.md
                      exit 1
                    fi
                  else
                    echo "No userfront tests: skipping (test/ directory not found)."
                  fi

            - name: Ensure userfront failure report exists
              if: ${{ failure() }}
              run: |
                  mkdir -p reports
                  if [ -f reports/userfront-test-failure-report.md ]; then
                    exit 0
                  fi

                  {
                    echo "# Userfront Test Failure Report"
                    echo
                    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                    echo "- Job: \`userfront-tests\`"
                    echo "- Reason: \`Job failed before detailed report generation\`"
                    echo
                    if [ -f reports/userfront-test.log ]; then
                      echo "## Test Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-test.log
                      echo '```'
                    fi
                  } > reports/userfront-test-failure-report.md

            - name: Publish userfront failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/userfront-test-failure-report.md ]; then
                    cat reports/userfront-test-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload userfront failure report artifact
              if: ${{ failure() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: userfront-test-failure-report
                  path: |
                      reports/userfront-test-failure-report.md
                      reports/userfront-test.log
                  if-no-files-found: ignore

    userfront-flutter-coverage:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.userfront_coverage == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_userfront_coverage == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Flutter
              uses: subosito/flutter-action@v2
              with:
                  channel: "stable"
                  cache: true

            - name: Sync userfront locales
              run: |
                  /bin/sh ./scripts/sync_userfront_locales.sh

            - name: Run userfront Flutter coverage
              run: |
                  cd userfront
                  if [ -d test ]; then
                    mkdir -p ../reports
                    set +e
                    flutter test --coverage 2>&1 | tee ../reports/userfront-flutter-coverage.log
                    test_exit_code=${PIPESTATUS[0]}
                    set -e

                    if [ "$test_exit_code" -ne 0 ]; then
                      {
                        echo "# Userfront Flutter Coverage Failure Report"
                        echo
                        echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                        echo "- Job: \`userfront-flutter-coverage\`"
                        echo "- Exit Code: \`$test_exit_code\`"
                        echo
                        echo "## Command"
                        echo "\`flutter test --coverage\`"
                        echo
                        if [ -f ../reports/userfront-flutter-coverage.log ]; then
                          echo "## Coverage Log Tail (last 200 lines)"
                          echo '```text'
                          tail -n 200 ../reports/userfront-flutter-coverage.log
                          echo '```'
                        fi
                      } > ../reports/userfront-flutter-coverage-failure-report.md
                      exit 1
                    fi
                  else
                    echo "No userfront tests: skipping coverage (test/ directory not found)."
                  fi

            - name: Generate userfront Flutter coverage summary
              run: |
                  node scripts/summarize_flutter_coverage.mjs userfront
                  cat reports/userfront-coverage-summary.md >> "$GITHUB_STEP_SUMMARY"

            - name: Publish userfront Flutter coverage failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/userfront-flutter-coverage-failure-report.md ]; then
                    cat reports/userfront-flutter-coverage-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload userfront Flutter coverage report artifact
              if: ${{ always() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: userfront-flutter-coverage-report
                  path: |
                      reports/package-coverage-summary.json
                      reports/userfront-coverage-summary.md
                      reports/userfront-flutter-coverage-failure-report.md
                      reports/userfront-flutter-coverage.log
                      userfront/coverage/lcov.info
                  if-no-files-found: ignore

    userfront-e2e-tests:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.userfront_e2e == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_userfront_e2e_tests == true) }}
        runs-on: ubuntu-latest
        timeout-minutes: 40
        env:
            USERFRONT_E2E_FULL: ${{ github.event_name == 'workflow_dispatch' && inputs.run_userfront_e2e_full == true }}
            USERFRONT_E2E_WORKERS: ${{ github.event_name == 'workflow_dispatch' && inputs.userfront_e2e_workers || '2' }}
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"
                  cache: "npm"
                  cache-dependency-path: userfront-e2e/package-lock.json

            - name: Get Playwright version
              id: playwright-version
              run: |
                node scripts/playwrightPackageVersion.cjs userfront-e2e >> "$GITHUB_OUTPUT"

            - name: Cache Playwright Browsers
              uses: actions/cache@v4
              id: playwright-cache
              with:
                path: ~/.cache/ms-playwright
                key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}
                restore-keys: |
                  ${{ runner.os }}-playwright-

            - name: Setup Flutter
              uses: subosito/flutter-action@v2
              with:
                  channel: "stable"
                  cache: true

            - name: Sync userfront locales
              run: |
                  /bin/sh ./scripts/sync_userfront_locales.sh

            - name: Install userfront-e2e dependencies
              run: |
                  mkdir -p reports
                  set +e
                  cd userfront-e2e
                  npm ci 2>&1 | tee ../reports/userfront-e2e-install.log
                  install_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$install_exit_code" -ne 0 ]; then
                    {
                      echo "# Userfront E2E Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`userfront-e2e-tests\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd userfront-e2e && npm ci\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-e2e-install.log
                      echo '```'
                    } > reports/userfront-e2e-test-failure-report.md
                    exit 1
                  fi

            - name: Build userfront WASM
              run: |
                  mkdir -p reports
                  set +e
                  cd userfront
                  rm -rf build/web
                  flutter build web --wasm --release 2>&1 | tee ../reports/userfront-e2e-build.log
                  build_exit_code=${PIPESTATUS[0]}
                  cd ..
                  if [ "$build_exit_code" -eq 0 ]; then
                    node userfront/scripts/optimize-web-build.mjs userfront/build/web 2>&1 | tee -a reports/userfront-e2e-build.log
                    build_exit_code=${PIPESTATUS[0]}
                  fi
                  set -e

                  if [ "$build_exit_code" -ne 0 ]; then
                    {
                      echo "# Userfront E2E Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`userfront-e2e-tests\`"
                      echo "- Reason: \`WASM build failed\`"
                      echo "- Exit Code: \`$build_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd userfront && flutter build web --wasm --release\`"
                      echo
                      echo "## Build Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-e2e-build.log
                      echo '```'
                    } > reports/userfront-e2e-test-failure-report.md
                    exit 1
                  fi

            - name: Provision browsers for userfront-e2e tests
              run: |
                  set +e
                  cd userfront-e2e
                  if [ "$USERFRONT_E2E_FULL" = "true" ]; then
                    provision_command="npx playwright install --with-deps"
                  else
                    provision_command="npx playwright install --with-deps chromium"
                  fi
                  echo "[userfront-e2e] $provision_command" | tee ../reports/userfront-e2e-provision.log
                  $provision_command 2>&1 | tee -a ../reports/userfront-e2e-provision.log
                  provision_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$provision_exit_code" -ne 0 ]; then
                    {
                      echo "# Userfront E2E Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`userfront-e2e-tests\`"
                      echo "- Reason: \`Browser provisioning failed\`"
                      echo "- Exit Code: \`$provision_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd userfront-e2e && $provision_command\`"
                      echo
                      echo "## Provision Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-e2e-provision.log
                      echo '```'
                    } > reports/userfront-e2e-test-failure-report.md
                    exit 1
                  fi

            - name: Run userfront-e2e tests
              run: |
                  mkdir -p reports
                  set +e
                  cd userfront-e2e
                  if [ "$USERFRONT_E2E_FULL" = "true" ]; then
                    test_command="npm test"
                  else
                    test_command="npm test -- --project=chromium-desktop --project=chromium-mobile-webapp"
                  fi
                  workers="${USERFRONT_E2E_WORKERS:-2}"
                  case "$workers" in
                    ''|*[!0-9]*|0) workers=2 ;;
                  esac
                  echo "[userfront-e2e] PLAYWRIGHT_WORKERS=$workers $test_command" | tee ../reports/userfront-e2e-test.log
                  PLAYWRIGHT_WORKERS="$workers" $test_command 2>&1 | tee -a ../reports/userfront-e2e-test.log
                  test_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$test_exit_code" -ne 0 ]; then
                    {
                      echo "# Userfront E2E Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`userfront-e2e-tests\`"
                      echo "- Exit Code: \`$test_exit_code\`"
                      echo
                      echo "## Commands"
                      echo "1. \`cd userfront-e2e\`"
                      echo "2. \`npm ci\`"
                      echo "3. \`cd ../userfront && flutter build web --wasm --release\`"
                      if [ "$USERFRONT_E2E_FULL" = "true" ]; then
                        echo "4. \`cd ../userfront-e2e && npx playwright install --with-deps\`"
                        echo "5. \`npm test\`"
                      else
                        echo "4. \`cd ../userfront-e2e && npx playwright install --with-deps chromium\`"
                        echo "5. \`npm test -- --project=chromium-desktop --project=chromium-mobile-webapp\`"
                      fi
                      echo
                      echo "## Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-e2e-test.log
                      echo '```'
                    } > reports/userfront-e2e-test-failure-report.md
                  fi

                  exit "$test_exit_code"

            - name: Ensure userfront-e2e failure report exists
              if: ${{ failure() }}
              run: |
                  mkdir -p reports
                  if [ -f reports/userfront-e2e-test-failure-report.md ]; then
                    exit 0
                  fi

                  {
                    echo "# Userfront E2E Test Failure Report"
                    echo
                    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                    echo "- Job: \`userfront-e2e-tests\`"
                    echo "- Reason: \`Job failed before detailed report generation\`"
                    echo
                    if [ -f reports/userfront-e2e-install.log ]; then
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-e2e-install.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/userfront-e2e-build.log ]; then
                      echo "## Build Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-e2e-build.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/userfront-e2e-provision.log ]; then
                      echo "## Provision Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-e2e-provision.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/userfront-e2e-test.log ]; then
                      echo "## Test Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/userfront-e2e-test.log
                      echo '```'
                    fi
                  } > reports/userfront-e2e-test-failure-report.md

            - name: Publish userfront-e2e failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/userfront-e2e-test-failure-report.md ]; then
                    cat reports/userfront-e2e-test-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload userfront-e2e failure report artifact
              if: ${{ failure() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: userfront-e2e-test-failure-report
                  path: |
                      reports/userfront-e2e-test-failure-report.md
                      reports/userfront-e2e-install.log
                      reports/userfront-e2e-build.log
                      reports/userfront-e2e-provision.log
                      reports/userfront-e2e-test.log
                      userfront-e2e/playwright-report
                      userfront-e2e/test-results
                  if-no-files-found: ignore

    adminfront-vitest-coverage:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.adminfront == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_front_coverage == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: Setup pnpm
              uses: pnpm/action-setup@v4
              with:
                  version: 10.5.2

            - name: Install front workspace dependencies
              run: |
                  mkdir -p reports
                  set +e
                  cd common
                  pnpm install --no-frozen-lockfile --shamefully-hoist 2>&1 | tee ../reports/front-coverage-install.log
                  install_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$install_exit_code" -ne 0 ]; then
                    {
                      echo "# Adminfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`adminfront-vitest-coverage\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd common && pnpm install --no-frozen-lockfile --shamefully-hoist\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/front-coverage-install.log
                      echo '```'
                    } > reports/adminfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

                  set +e
                  cd adminfront
                  pnpm install --no-frozen-lockfile --shamefully-hoist 2>&1 | tee -a ../reports/front-coverage-install.log
                  app_install_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$app_install_exit_code" -ne 0 ]; then
                    {
                      echo "# Adminfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`adminfront-vitest-coverage\`"
                      echo "- Package: \`adminfront\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$app_install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd adminfront && pnpm install --no-frozen-lockfile --shamefully-hoist\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/front-coverage-install.log
                      echo '```'
                    } > reports/adminfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

            - name: Run adminfront Vitest coverage
              run: |
                  set +e
                  cd adminfront
                  pnpm run test:coverage 2>&1 | tee ../reports/adminfront-vitest-coverage.log
                  test_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$test_exit_code" -ne 0 ]; then
                    {
                      echo "# Adminfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`adminfront-vitest-coverage\`"
                      echo "- Package: \`adminfront\`"
                      echo "- Exit Code: \`$test_exit_code\`"
                      echo
                      echo "## Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/adminfront-vitest-coverage.log
                      echo '```'
                    } > reports/adminfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

            - name: Generate adminfront Vitest coverage summary
              run: |
                  node scripts/summarize_vitest_coverage.mjs adminfront
                  cat reports/vitest-coverage-summary.md >> "$GITHUB_STEP_SUMMARY"

            - name: Publish adminfront Vitest coverage failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/adminfront-vitest-coverage-failure-report.md ]; then
                    cat reports/adminfront-vitest-coverage-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload adminfront Vitest coverage report artifact
              if: ${{ always() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: adminfront-vitest-coverage-report
                  path: |
                      reports/vitest-coverage-summary.md
                      reports/vitest-coverage-summary.json
                      reports/adminfront-vitest-coverage-failure-report.md
                      reports/front-coverage-install.log
                      reports/adminfront-vitest-coverage.log
                      adminfront/coverage
                  if-no-files-found: ignore

    devfront-vitest-coverage:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.devfront == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_front_coverage == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: Setup pnpm
              uses: pnpm/action-setup@v4
              with:
                  version: 10.5.2

            - name: Install front workspace dependencies
              run: |
                  mkdir -p reports
                  set +e
                  cd common
                  pnpm install --no-frozen-lockfile --shamefully-hoist 2>&1 | tee ../reports/front-coverage-install.log
                  install_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$install_exit_code" -ne 0 ]; then
                    {
                      echo "# Devfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`devfront-vitest-coverage\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd common && pnpm install --no-frozen-lockfile --shamefully-hoist\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/front-coverage-install.log
                      echo '```'
                    } > reports/devfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

                  set +e
                  cd devfront
                  pnpm install --no-frozen-lockfile --shamefully-hoist 2>&1 | tee -a ../reports/front-coverage-install.log
                  app_install_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$app_install_exit_code" -ne 0 ]; then
                    {
                      echo "# Devfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`devfront-vitest-coverage\`"
                      echo "- Package: \`devfront\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$app_install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd devfront && pnpm install --no-frozen-lockfile --shamefully-hoist\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/front-coverage-install.log
                      echo '```'
                    } > reports/devfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

            - name: Run devfront Vitest coverage
              run: |
                  set +e
                  cd devfront
                  pnpm run test:coverage 2>&1 | tee ../reports/devfront-vitest-coverage.log
                  test_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$test_exit_code" -ne 0 ]; then
                    {
                      echo "# Devfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`devfront-vitest-coverage\`"
                      echo "- Package: \`devfront\`"
                      echo "- Exit Code: \`$test_exit_code\`"
                      echo
                      echo "## Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/devfront-vitest-coverage.log
                      echo '```'
                    } > reports/devfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

            - name: Generate devfront Vitest coverage summary
              run: |
                  node scripts/summarize_vitest_coverage.mjs devfront
                  cat reports/vitest-coverage-summary.md >> "$GITHUB_STEP_SUMMARY"

            - name: Publish devfront Vitest coverage failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/devfront-vitest-coverage-failure-report.md ]; then
                    cat reports/devfront-vitest-coverage-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload devfront Vitest coverage report artifact
              if: ${{ always() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: devfront-vitest-coverage-report
                  path: |
                      reports/vitest-coverage-summary.md
                      reports/vitest-coverage-summary.json
                      reports/devfront-vitest-coverage-failure-report.md
                      reports/front-coverage-install.log
                      reports/devfront-vitest-coverage.log
                      devfront/coverage
                  if-no-files-found: ignore

    orgfront-vitest-coverage:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.orgfront == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_front_coverage == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: Setup pnpm
              uses: pnpm/action-setup@v4
              with:
                  version: 10.5.2

            - name: Install front workspace dependencies
              run: |
                  mkdir -p reports
                  set +e
                  cd common
                  pnpm install --no-frozen-lockfile --shamefully-hoist 2>&1 | tee ../reports/front-coverage-install.log
                  install_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$install_exit_code" -ne 0 ]; then
                    {
                      echo "# Orgfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`orgfront-vitest-coverage\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd common && pnpm install --no-frozen-lockfile --shamefully-hoist\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/front-coverage-install.log
                      echo '```'
                    } > reports/orgfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

                  set +e
                  cd orgfront
                  pnpm install --no-frozen-lockfile --shamefully-hoist 2>&1 | tee -a ../reports/front-coverage-install.log
                  app_install_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$app_install_exit_code" -ne 0 ]; then
                    {
                      echo "# Orgfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`orgfront-vitest-coverage\`"
                      echo "- Package: \`orgfront\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$app_install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd orgfront && pnpm install --no-frozen-lockfile --shamefully-hoist\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/front-coverage-install.log
                      echo '```'
                    } > reports/orgfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

            - name: Run orgfront Vitest coverage
              run: |
                  set +e
                  cd orgfront
                  pnpm run test:coverage 2>&1 | tee ../reports/orgfront-vitest-coverage.log
                  test_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$test_exit_code" -ne 0 ]; then
                    {
                      echo "# Orgfront Vitest Coverage Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`orgfront-vitest-coverage\`"
                      echo "- Package: \`orgfront\`"
                      echo "- Exit Code: \`$test_exit_code\`"
                      echo
                      echo "## Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/orgfront-vitest-coverage.log
                      echo '```'
                    } > reports/orgfront-vitest-coverage-failure-report.md
                    exit 1
                  fi

            - name: Generate orgfront Vitest coverage summary
              run: |
                  node scripts/summarize_vitest_coverage.mjs orgfront
                  cat reports/vitest-coverage-summary.md >> "$GITHUB_STEP_SUMMARY"

            - name: Publish orgfront Vitest coverage failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/orgfront-vitest-coverage-failure-report.md ]; then
                    cat reports/orgfront-vitest-coverage-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload orgfront Vitest coverage report artifact
              if: ${{ always() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: orgfront-vitest-coverage-report
                  path: |
                      reports/vitest-coverage-summary.md
                      reports/vitest-coverage-summary.json
                      reports/orgfront-vitest-coverage-failure-report.md
                      reports/front-coverage-install.log
                      reports/orgfront-vitest-coverage.log
                      orgfront/coverage
                  if-no-files-found: ignore

    adminfront-tests:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.adminfront == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_adminfront_tests == true) }}
        runs-on: ubuntu-latest
        timeout-minutes: 30
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: Get Playwright version
              id: playwright-version
              run: |
                node scripts/playwrightPackageVersion.cjs adminfront >> "$GITHUB_OUTPUT"

            - name: Cache Playwright Browsers
              uses: actions/cache@v4
              id: playwright-cache
              with:
                path: ~/.cache/ms-playwright
                key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}
                restore-keys: |
                  ${{ runner.os }}-playwright-

            - name: Run adminfront tests
              env:
                PLAYWRIGHT_WORKERS: 2
              run: |
                  scripts/run_adminfront_ci_tests.sh adminfront-tests

            - name: Ensure adminfront failure report exists
              if: ${{ failure() }}
              run: |
                  mkdir -p reports
                  if [ -f reports/adminfront-test-failure-report.md ]; then
                    exit 0
                  fi

                  {
                    echo "# Adminfront Test Failure Report"
                    echo
                    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                    echo "- Job: \`adminfront-tests\`"
                    echo "- Reason: \`Job failed before detailed report generation\`"
                    echo
                    if [ -f reports/adminfront-install.log ]; then
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/adminfront-install.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/adminfront-provision.log ]; then
                      echo "## Provision Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/adminfront-provision.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/adminfront-test.log ]; then
                      echo "## Test Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/adminfront-test.log
                      echo '```'
                    fi
                  } > reports/adminfront-test-failure-report.md

            - name: Publish adminfront failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/adminfront-test-failure-report.md ]; then
                    cat reports/adminfront-test-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload adminfront failure report artifact
              if: ${{ failure() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: adminfront-test-failure-report
                  path: |
                      reports/adminfront-test-failure-report.md
                      reports/adminfront-install.log
                      reports/adminfront-provision.log
                      reports/adminfront-test.log
                      adminfront/playwright-report
                      adminfront/test-results
                  if-no-files-found: ignore

    devfront-tests:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.devfront == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_devfront_tests == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: Get Playwright version
              id: playwright-version
              working-directory: devfront
              run: |
                node ../scripts/playwrightPackageVersion.cjs . >> "$GITHUB_OUTPUT"

            - name: Cache Playwright Browsers
              uses: actions/cache@v4
              id: playwright-cache
              with:
                path: ~/.cache/ms-playwright
                key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}
                restore-keys: |
                  ${{ runner.os }}-playwright-

            - name: Install devfront dependencies
              working-directory: devfront
              run: |
                  mkdir -p ../reports
                  set +e
                  {
                    pnpm install -C ../common --no-frozen-lockfile
                    pnpm install --no-frozen-lockfile
                  } 2>&1 | tee ../reports/devfront-install.log
                  install_exit_code=${PIPESTATUS[0]}
                  set -e

                  if [ "$install_exit_code" -ne 0 ]; then
                    {
                      echo "# Devfront Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`devfront-tests\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd devfront && pnpm install -C ../common --no-frozen-lockfile\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 ../reports/devfront-install.log
                      echo '```'
                    } > ../reports/devfront-test-failure-report.md
                    exit 1
                  fi

            - name: Provision browsers for devfront tests
              working-directory: devfront
              run: |
                  set +e
                  pnpm exec playwright install --with-deps 2>&1 | tee ../reports/devfront-provision.log
                  provision_exit_code=${PIPESTATUS[0]}
                  set -e

                  if [ "$provision_exit_code" -ne 0 ]; then
                    {
                      echo "# Devfront Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`devfront-tests\`"
                      echo "- Reason: \`Browser provisioning failed\`"
                      echo "- Exit Code: \`$provision_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd devfront && pnpm exec playwright install --with-deps\`"
                      echo
                      echo "## Provision Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 ../reports/devfront-provision.log
                      echo '```'
                    } > ../reports/devfront-test-failure-report.md
                    exit 1
                  fi

            - name: Run devfront tests
              working-directory: devfront
              env:
                PLAYWRIGHT_WORKERS: 2
              run: |
                  mkdir -p ../reports
                  set +e
                  pnpm test 2>&1 | tee ../reports/devfront-test.log
                  test_exit_code=${PIPESTATUS[0]}
                  set -e

                  if [ "$test_exit_code" -ne 0 ]; then
                    {
                      echo "# Devfront Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`devfront-tests\`"
                      echo "- Exit Code: \`$test_exit_code\`"
                      echo
                      echo "## Commands"
                      echo "1. \`cd devfront\`"
                      echo "2. \`pnpm install -C ../common --no-frozen-lockfile\`"
                      echo "3. \`pnpm exec playwright install --with-deps\`"
                      echo "4. \`pnpm test\`"
                      echo
                      echo "## Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 ../reports/devfront-test.log
                      echo '```'
                    } > ../reports/devfront-test-failure-report.md
                  fi

                  exit "$test_exit_code"

            - name: Ensure devfront failure report exists
              if: ${{ failure() }}
              run: |
                  mkdir -p reports
                  if [ -f reports/devfront-test-failure-report.md ]; then
                    exit 0
                  fi

                  {
                    echo "# Devfront Test Failure Report"
                    echo
                    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                    echo "- Job: \`devfront-tests\`"
                    echo "- Reason: \`Job failed before detailed report generation\`"
                    echo
                    if [ -f reports/devfront-install.log ]; then
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/devfront-install.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/devfront-provision.log ]; then
                      echo "## Provision Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/devfront-provision.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/devfront-test.log ]; then
                      echo "## Test Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/devfront-test.log
                      echo '```'
                    fi
                  } > reports/devfront-test-failure-report.md

            - name: Publish devfront failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/devfront-test-failure-report.md ]; then
                    cat reports/devfront-test-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload devfront failure report artifact
              if: ${{ failure() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: devfront-test-failure-report
                  path: |
                      reports/devfront-test-failure-report.md
                      reports/devfront-install.log
                      reports/devfront-provision.log
                      reports/devfront-test.log
                      devfront/playwright-report
                      devfront/test-results
                  if-no-files-found: ignore

    orgfront-tests:
        needs:
            - changes
            - lint
        if: ${{ always() && needs.changes.outputs.orgfront == 'true' && (github.event_name != 'workflow_dispatch' || inputs.run_orgfront_tests == true) }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: Get Playwright version
              id: playwright-version
              run: |
                node scripts/playwrightPackageVersion.cjs orgfront >> "$GITHUB_OUTPUT"

            - name: Cache Playwright Browsers
              uses: actions/cache@v4
              id: playwright-cache
              with:
                path: ~/.cache/ms-playwright
                key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}
                restore-keys: |
                  ${{ runner.os }}-playwright-

            - name: Install orgfront dependencies
              run: |
                  mkdir -p reports
                  set +e
                  cd orgfront
                  corepack enable
                  corepack prepare pnpm@10.5.2 --activate
                  {
                    pnpm install -C ../common --no-frozen-lockfile
                    pnpm install --no-frozen-lockfile
                  } 2>&1 | tee ../reports/orgfront-install.log
                  install_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$install_exit_code" -ne 0 ]; then
                    {
                      echo "# OrgFront Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`orgfront-tests\`"
                      echo "- Reason: \`Dependency install failed\`"
                      echo "- Exit Code: \`$install_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd orgfront && npm ci\`"
                      echo
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/orgfront-install.log
                      echo '```'
                    } > reports/orgfront-test-failure-report.md
                    exit 1
                  fi

            - name: Provision browsers for orgfront tests
              run: |
                  set +e
                  cd orgfront
                  pnpm exec playwright install --with-deps 2>&1 | tee ../reports/orgfront-provision.log
                  provision_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$provision_exit_code" -ne 0 ]; then
                    {
                      echo "# OrgFront Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`orgfront-tests\`"
                      echo "- Reason: \`Browser provisioning failed\`"
                      echo "- Exit Code: \`$provision_exit_code\`"
                      echo
                      echo "## Command"
                      echo "\`cd orgfront && pnpm exec playwright install --with-deps\`"
                      echo
                      echo "## Provision Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/orgfront-provision.log
                      echo '```'
                    } > reports/orgfront-test-failure-report.md
                    exit 1
                  fi

            - name: Run orgfront tests
              env:
                PLAYWRIGHT_WORKERS: 2
              run: |
                  mkdir -p reports
                  set +e
                  cd orgfront
                  pnpm run test 2>&1 | tee ../reports/orgfront-test.log
                  test_exit_code=${PIPESTATUS[0]}
                  cd ..
                  set -e

                  if [ "$test_exit_code" -ne 0 ]; then
                    {
                      echo "# OrgFront Test Failure Report"
                      echo
                      echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                      echo "- Job: \`orgfront-tests\`"
                      echo "- Exit Code: \`$test_exit_code\`"
                      echo
                      echo "## Commands"
                      echo "1. \`cd orgfront\`"
                      echo "2. \`npm ci\`"
                      echo "3. \`pnpm exec playwright install --with-deps\`"
                      echo "4. \`pnpm run test\`"
                      echo
                      echo "## Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/orgfront-test.log
                      echo '```'
                    } > reports/orgfront-test-failure-report.md
                  fi

                  exit "$test_exit_code"

            - name: Ensure orgfront failure report exists
              if: ${{ failure() }}
              run: |
                  mkdir -p reports
                  if [ -f reports/orgfront-test-failure-report.md ]; then
                    exit 0
                  fi

                  {
                    echo "# OrgFront Test Failure Report"
                    echo
                    echo "- Workflow: \`${GITHUB_WORKFLOW:-Code Check}\`"
                    echo "- Job: \`orgfront-tests\`"
                    echo "- Reason: \`Job failed before detailed report generation\`"
                    echo
                    if [ -f reports/orgfront-install.log ]; then
                      echo "## Install Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/orgfront-install.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/orgfront-provision.log ]; then
                      echo "## Provision Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/orgfront-provision.log
                      echo '```'
                      echo
                    fi
                    if [ -f reports/orgfront-test.log ]; then
                      echo "## Test Log Tail (last 200 lines)"
                      echo '```text'
                      tail -n 200 reports/orgfront-test.log
                      echo '```'
                    fi
                  } > reports/orgfront-test-failure-report.md

            - name: Publish orgfront failure summary
              if: ${{ failure() }}
              run: |
                  if [ -f reports/orgfront-test-failure-report.md ]; then
                    cat reports/orgfront-test-failure-report.md >> "$GITHUB_STEP_SUMMARY"
                  fi

            - name: Upload orgfront failure report artifact
              if: ${{ failure() }}
              uses: actions/upload-artifact@v3
              continue-on-error: true
              with:
                  name: orgfront-test-failure-report
                  path: |
                      reports/orgfront-test-failure-report.md
                      reports/orgfront-install.log
                      reports/orgfront-provision.log
                      reports/orgfront-test.log
                      orgfront/playwright-report
                      orgfront/test-results
                  if-no-files-found: ignore

    badge-updater:
        needs:
            - changes
            - lint
            - biome-check
            - backend-tests
            - userfront-tests
            - userfront-flutter-coverage
            - userfront-e2e-tests
            - adminfront-vitest-coverage
            - devfront-vitest-coverage
            - orgfront-vitest-coverage
            - adminfront-tests
            - devfront-tests
            - orgfront-tests
        if: ${{ always() && needs.changes.outputs.any == 'true' && github.event_name != 'pull_request' && github.ref == 'refs/heads/dev' }}
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v4
              with:
                  fetch-depth: 0

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: "24"

            - name: Download backend coverage report artifact
              uses: actions/download-artifact@v3
              continue-on-error: true
              with:
                  name: backend-coverage-report
                  path: badge-artifacts/backend

            - name: Download userfront Flutter coverage report artifact
              uses: actions/download-artifact@v3
              continue-on-error: true
              with:
                  name: userfront-flutter-coverage-report
                  path: badge-artifacts/userfront

            - name: Download adminfront Vitest coverage report artifact
              uses: actions/download-artifact@v3
              continue-on-error: true
              with:
                  name: adminfront-vitest-coverage-report
                  path: badge-artifacts/adminfront

            - name: Download devfront Vitest coverage report artifact
              uses: actions/download-artifact@v3
              continue-on-error: true
              with:
                  name: devfront-vitest-coverage-report
                  path: badge-artifacts/devfront

            - name: Download orgfront Vitest coverage report artifact
              uses: actions/download-artifact@v3
              continue-on-error: true
              with:
                  name: orgfront-vitest-coverage-report
                  path: badge-artifacts/orgfront

            - name: Restore published badge state
              run: |
                  git fetch origin "+refs/heads/badges:refs/remotes/origin/badges" || true
                  if git show-ref --verify --quiet refs/remotes/origin/badges && \
                     git cat-file -e refs/remotes/origin/badges:latest/badges.json 2>/dev/null; then
                    mkdir -p docs/badges
                    git archive --format=tar refs/remotes/origin/badges latest | tar -x
                    cp latest/* docs/badges/
                    rm -rf latest
                  else
                    echo "No published badge state found."
                  fi

            - name: Update badge files
              env:
                  LINT_RESULT: ${{ needs.lint.result }}
                  BIOME_RESULT: ${{ needs['biome-check'].result }}
                  BACKEND_RESULT: ${{ needs['backend-tests'].result }}
                  BACKEND_COVERAGE_RESULT: ${{ needs['backend-tests'].result }}
                  USERFRONT_RESULT: ${{ needs['userfront-tests'].result }}
                  USERFRONT_E2E_RESULT: ${{ needs['userfront-e2e-tests'].result }}
                  USERFRONT_E2E_FULL: ${{ github.event_name == 'workflow_dispatch' && inputs.run_userfront_e2e_full == true }}
                  USERFRONT_COVERAGE_RESULT: ${{ needs['userfront-flutter-coverage'].result }}
                  ADMINFRONT_COVERAGE_RESULT: ${{ needs['adminfront-vitest-coverage'].result }}
                  DEVFRONT_COVERAGE_RESULT: ${{ needs['devfront-vitest-coverage'].result }}
                  ORGFRONT_COVERAGE_RESULT: ${{ needs['orgfront-vitest-coverage'].result }}
                  ADMINFRONT_RESULT: ${{ needs['adminfront-tests'].result }}
                  DEVFRONT_RESULT: ${{ needs['devfront-tests'].result }}
                  ORGFRONT_RESULT: ${{ needs['orgfront-tests'].result }}
                  BADGE_SOURCE_BRANCH: dev
                  BADGE_SOURCE_SHA: ${{ github.sha }}
              run: |
                  node scripts/update_code_check_badges.mjs
                  cat docs/badges/badges.json

            - name: Publish badge assets
              run: |
                  if [ -z "$(git status --porcelain docs/badges)" ]; then
                    echo "No badge changes."
                    exit 0
                  fi

                  BADGE_BRANCH=badges
                  BADGE_WORKTREE="$(mktemp -d)"
                  BADGE_LATEST_DIR="${BADGE_WORKTREE}/latest"
                  BADGE_SHA_DIR="${BADGE_WORKTREE}/dev/${GITHUB_SHA}"
                  trap 'rm -rf "${BADGE_WORKTREE}"' EXIT

                  git config user.name "gitea-actions"
                  git config user.email "gitea-actions@hmac.kr"

                  git fetch origin "+refs/heads/${BADGE_BRANCH}:refs/remotes/origin/${BADGE_BRANCH}" || true
                  if git show-ref --verify --quiet "refs/remotes/origin/${BADGE_BRANCH}"; then
                    git worktree add --detach "${BADGE_WORKTREE}" "origin/${BADGE_BRANCH}"
                  else
                    git worktree add --detach "${BADGE_WORKTREE}"
                    git -C "${BADGE_WORKTREE}" checkout --orphan "${BADGE_BRANCH}"
                    git -C "${BADGE_WORKTREE}" rm -rf . || true
                  fi

                  find "${BADGE_WORKTREE}" -mindepth 1 -maxdepth 1 ! -name .git -exec rm -rf {} +
                  mkdir -p "${BADGE_LATEST_DIR}" "${BADGE_SHA_DIR}"
                  cp docs/badges/*.svg "${BADGE_LATEST_DIR}/"
                  cp docs/badges/badges.json "${BADGE_LATEST_DIR}/badges.json"
                  cp docs/badges/*.svg "${BADGE_SHA_DIR}/"
                  cp docs/badges/badges.json "${BADGE_SHA_DIR}/badges.json"

                  git -C "${BADGE_WORKTREE}" add .
                  if [ -z "$(git -C "${BADGE_WORKTREE}" status --porcelain)" ]; then
                    echo "No published badge changes."
                    exit 0
                  fi

                  git -C "${BADGE_WORKTREE}" commit -m "chore: publish code check badges [skip ci]"
                  git -C "${BADGE_WORKTREE}" push origin HEAD:${BADGE_BRANCH}
