# Render the course (single source of truth = modules/) into the Gitea wiki on
# every push to main. The wiki is generated BUILD OUTPUT; never hand-edit it.
#
# Runs on the stack's shared `docker` runners (Linux). To actually push the wiki it
# needs a repo secret WIKI_TOKEN with wiki write (a scoped PAT/deploy token, NOT a
# site-admin token). Until that secret exists the job skips cleanly (stays green).
name: Sync course wiki
on:
  push:
    branches: [main]
    paths:
      - 'modules/**'
      - 'capstone/**'
      - 'README.md'
      - 'tools/build_wiki.py'
      - '.gitea/workflows/sync-wiki.yml'
  workflow_dispatch: {}

jobs:
  sync-wiki:
    runs-on: docker
    steps:
      - uses: actions/checkout@v4
      - name: Render and push the wiki
        shell: bash
        env:
          WIKI_TOKEN: ${{ secrets.WIKI_TOKEN }}
        run: |
          set -euo pipefail
          echo "runner: $(uname -srm); $(python3 --version 2>/dev/null || echo 'python3 missing')"
          if [ -z "${WIKI_TOKEN:-}" ]; then
            echo "::warning::WIKI_TOKEN secret not set; skipping wiki sync. Add the secret to enable auto-sync."
            exit 0
          fi
          command -v python3 >/dev/null || { apt-get update && apt-get install -y --no-install-recommends python3; }
          base="git.jpaul.io/justin/ai-workflow-course"
          git clone "https://claude:${WIKI_TOKEN}@${base}.wiki.git" wiki
          python3 tools/build_wiki.py --repo-root . --out wiki \
            --web-base "https://${base}" --branch main --host gitea
          cd wiki
          git config user.name  "claude"
          git config user.email "claude@jpaul.io"
          git add -A
          if git diff --cached --quiet; then
            echo "wiki already up to date"; exit 0
          fi
          git commit -m "docs(wiki): sync from modules/ @ $(echo "$GITHUB_SHA" | cut -c1-8)"
          git push origin HEAD
