name: Docs (GitHub Pages)

on:
  push:
    branches:
      - main

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  deploy:
    # Only run on the original repository, not on forks
    if: github.repository == 'MoonshotAI/kimi-cli'
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deploy.outputs.page_url }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Configure GitHub Pages
        id: pages
        uses: actions/configure-pages@v5

      - name: Set VitePress base
        shell: bash
        env:
          BASE_PATH: ${{ steps.pages.outputs.base_path }}
        run: |
          set -euo pipefail
          if [[ -z "${BASE_PATH}" ]]; then
            base="/"
          else
            base="${BASE_PATH%/}/"
          fi
          echo "VITEPRESS_BASE=${base}" >> "$GITHUB_ENV"

      - name: Install docs dependencies
        working-directory: docs
        run: npm install --no-package-lock

      - name: Build docs
        working-directory: docs
        env:
          VITEPRESS_BASE: ${{ env.VITEPRESS_BASE }}
        run: npm run build

      - name: Add .nojekyll
        run: touch docs/.vitepress/dist/.nojekyll

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: docs/.vitepress/dist

      - name: Deploy to GitHub Pages
        id: deploy
        uses: actions/deploy-pages@v4
