# Tier 3 gate and publication. The act-runner registration is stored in an
# emptyDir: after a runner pod restart, re-register it before debugging CI.
name: verify-and-publish

on:
  push:
    branches: [main]
  pull_request:
  # Publication is deliberately manual for the duration of the Astro migration.
  # See the `publish` job below for why.
  workflow_dispatch:
    inputs:
      publish:
        description: 'Force-push dist/ to the pages branch'
        type: boolean
        default: false

jobs:
  gate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      # Node 25 dropped the bundled corepack, and this self-hosted act-runner
      # is not a good place to discover whether it can fetch a third-party
      # action. Bootstrapping pnpm with npm needs neither. Keep the version in
      # step with package.json's `packageManager`.
      - run: npm install --global pnpm@11.25.0
      - run: pnpm install --frozen-lockfile
      - run: pnpm run lint
      - run: ./.agents/scripts/gate.sh
      # The visual-regression step is intentionally absent. `visual-regression.mjs`
      # only *captures* baselines — it has no compare mode — so in CI it would
      # overwrite .agents/snapshots/before/ and pass unconditionally. It also
      # imports `playwright`, which is not a dependency of this project. Wire it
      # back in once it can actually diff. See task 03's report.

  publish:
    # `dist/` now holds all ten routes, so the stub hazard that forced this to
    # manual dispatch is gone. It stays manual anyway: the step below is a
    # force-push over the live `pages` branch, and making it fire on every push
    # to main means every merge republishes with no human in the loop. Flipping
    # it to `push` on main is a deliberate decision, not a leftover TODO.
    if: github.event_name == 'workflow_dispatch' && inputs.publish
    needs: gate
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npm install --global pnpm@11.25.0
      - run: pnpm install --frozen-lockfile
      - run: pnpm run build
      - name: Publish generated site to pages
        run: |
          git config user.name 'gitea-actions[bot]'
          git config user.email 'gitea-actions[bot]@users.noreply.local'
          git switch --orphan pages
          git rm -rf .
          cp -a dist/. .
          git add --all
          git commit -m 'chore: publish site'
          git push --force origin HEAD:pages
