name: Build

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
  workflow_dispatch:

jobs:
  build:
    name: OI-Wiki Page Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-python@v5
        with:
          python-version: '3.10'
      - name: Install uv
        uses: astral-sh/setup-uv@v6
        with:
          version: "latest"
      - uses: actions/setup-node@v4
        with:
          node-version: '20.x'
          check-latest: true
      - name: Install Python dependencies
        run: uv sync
      - name: Cache Node.js dependencies
        uses: actions/cache@v4
        id: cache-node
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('yarn.lock') }}
          restore-keys: ${{ runner.os }}-node-
      - name: Install Node.js dependencies
        if: steps.cache-node.outputs.cache-hit != 'true'
        run: yarn --frozen-lockfile --production
      - name: Page Build
        run: |
          ./scripts/pre-build/pre-build.sh
          uv run mkdocs build -v
      - name: Post-process HTML
        # https://github.com/TypeStrong/ts-node/issues/1997
        run: node --loader ts-node/esm scripts/post-build/html-postprocess.ts commits-info math external-links
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_OPTIONS: --max_old_space_size=3072
      - name: Minify html
        run: ./scripts/post-build/minify-html/minify-html.sh
      - name: Generate redirects
        run: uv run python scripts/post-build/redirect/generate-redirects.py
      - name: Check links
        uses: wjdp/htmltest-action@master
        with:
          skip_external: true
      - name: Fetch old site
        uses: actions/checkout@v4
        with:
          ref: gh-pages
          path: old-site
      - name: Check _redirects
        run: uv run python scripts/post-build/redirect/check-redirects.py
      - name: Deploy to gh-pages
        if: ${{ github.event_name == 'push' }}
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./site
          force_orphan: true
      - name: Push to Baidu
        if: ${{ github.event_name == 'push' && github.repository_owner == 'oi-wiki' }}
        env:
          SITE: oi-wiki.org
          BAIDU_TOKEN: ${{ secrets.BaiduToken }}
        run: ./scripts/post-deploy/baidu-push.sh

  update_feedback_sys_meta:
    name: Update OI-Wiki Feedback System Meta
    needs: build
    runs-on: ubuntu-latest
    if: ${{ github.ref == 'refs/heads/master' }}
    steps:
      - name: Checkout latest
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Get changed files
        uses: tj-actions/changed-files@v46
        with:
          files: docs/**/*.md
          include_all_old_new_renamed_files: true
          write_output_files: true
      - name: Checkout before
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.before }}
          path: before
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
            python-version: '3.10'
      - name: Install uv
        uses: astral-sh/setup-uv@v6
        with:
          version: "latest"
      - name: Install Python dependencies
        run: uv sync
      - name: Update meta
        run: |
          uv run python scripts/update-feedback-sys-meta.py --modified .github/outputs/modified_files.txt --renamed .github/outputs/renamed_files.txt  --all_old_new_renamed .github/outputs/all_old_new_renamed_files.txt --before_dir before
        env:
          ADMINISTRATOR_SECRET: ${{ secrets.FEEDBACK_SYS_ADMINISTRATOR_SECRET }}
