name: agent-verify

on:
  pull_request:
  push:
    branches: [main, master, develop]

jobs:
  verify:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - uses: actions/setup-node@v5
        with:
          node-version: 22

      - name: Detect package manager
        id: pm
        run: |
          if [ -f pnpm-lock.yaml ]; then
            echo "manager=pnpm" >> "$GITHUB_OUTPUT"
          elif [ -f yarn.lock ]; then
            echo "manager=yarn" >> "$GITHUB_OUTPUT"
          else
            echo "manager=npm" >> "$GITHUB_OUTPUT"
          fi

      - name: Enable corepack (pnpm)
        if: steps.pm.outputs.manager == 'pnpm'
        run: corepack enable

      - name: Install dependencies (npm)
        if: steps.pm.outputs.manager == 'npm'
        run: npm ci

      - name: Install dependencies (yarn)
        if: steps.pm.outputs.manager == 'yarn'
        run: yarn install --frozen-lockfile

      - name: Install dependencies (pnpm)
        if: steps.pm.outputs.manager == 'pnpm'
        run: pnpm install --frozen-lockfile

      - name: OpenSpec validate
        run: npx openspec validate --all --strict

      - name: Gate check (review gate)
        run: npx agent-orchestrator-kit gate-check --base "${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || 'HEAD~1' }}"

      - name: Lint (npm)
        if: steps.pm.outputs.manager == 'npm'
        run: npm run lint --if-present

      - name: Lint (yarn)
        if: steps.pm.outputs.manager == 'yarn'
        run: yarn lint

      - name: Lint (pnpm)
        if: steps.pm.outputs.manager == 'pnpm'
        run: pnpm run lint

      - name: Build (npm)
        if: steps.pm.outputs.manager == 'npm'
        run: npm run build --if-present

      - name: Build (yarn)
        if: steps.pm.outputs.manager == 'yarn'
        run: yarn build

      - name: Build (pnpm)
        if: steps.pm.outputs.manager == 'pnpm'
        run: pnpm run build

      - name: Test (npm)
        if: steps.pm.outputs.manager == 'npm'
        run: npm test --if-present

      - name: Test (yarn)
        if: steps.pm.outputs.manager == 'yarn'
        run: yarn test

      - name: Test (pnpm)
        if: steps.pm.outputs.manager == 'pnpm'
        run: pnpm test

  # Opt-in: archive merged changes from CI instead of a terminal (a terminal stays the default).
  # OFF unless the repository variable AOK_ARCHIVE_ON_MERGE is "true"
  # (Settings → Secrets and variables → Actions → Variables).
  # Pushing the archive commit needs a token that may push to the protected default branch:
  # store it as the secret AOK_ARCHIVE_TOKEN (branch-protection bypass); without it the
  # workflow token is used and the push works only on an unprotected branch.
  # The commit message carries [skip ci], so the push does not start another run.
  # `archive --if-ready` decides readiness (archive_after_merge: true, review APPROVE,
  # all tasks done, not already archived); a change that is not ready is skipped.
  archive:
    needs: verify
    if: github.event_name == 'push' && github.ref_name == github.event.repository.default_branch && vars.AOK_ARCHIVE_ON_MERGE == 'true'
    runs-on: ubuntu-latest
    permissions:
      contents: write
    concurrency:
      group: agent-archive
      cancel-in-progress: false
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
          token: ${{ secrets.AOK_ARCHIVE_TOKEN || github.token }}

      - uses: actions/setup-node@v5
        with:
          node-version: 22

      - name: Install dependencies
        run: |
          if [ -f pnpm-lock.yaml ]; then
            corepack enable
            pnpm install --frozen-lockfile
          elif [ -f yarn.lock ]; then
            yarn install --frozen-lockfile
          else
            npm ci
          fi

      - name: Archive ready changes
        run: |
          set -eu
          for dir in openspec/changes/*/; do
            [ -d "$dir" ] || continue
            name=$(basename "$dir")
            [ "$name" = "archive" ] && continue
            npx agent-orchestrator-kit archive "$name" --sync --if-ready
          done

      - name: Commit and push the archive
        run: |
          set -eu
          git config user.name "agent-orchestrator-kit[bot]"
          git config user.email "agent-orchestrator-kit@users.noreply.github.com"
          git add -A openspec
          if git diff --cached --quiet; then
            echo "nothing archived"
            exit 0
          fi
          git commit -m "chore(openspec): archive merged change [skip ci]"
          git push origin HEAD:"$GITHUB_REF_NAME"
