name: Deploy documentation

on:
  push:
    branches: [master]
    paths:
      - '.github/workflows/docs-pages.yml'
      - 'docs/**'
      - 'package.json'
      - 'pnpm-lock.yaml'
      - 'pnpm-workspace.yaml'
      - 'scripts/project-doc-site.ts'
      - 'scripts/project-doc-site.spec.ts'
      - 'website/**'
  workflow_dispatch:

concurrency:
  group: github-pages
  cancel-in-progress: false

permissions:
  contents: read

env:
  PRIMARY_NODE_VERSION: '24'

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pages: read
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-node@v6
        with:
          node-version: ${{ env.PRIMARY_NODE_VERSION }}

      - name: Enable corepack (pnpm)
        run: corepack enable

      - name: Resolve pnpm store path
        id: pnpm-store
        run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"

      - uses: actions/cache@v4
        with:
          path: ${{ steps.pnpm-store.outputs.path }}
          key: ${{ runner.os }}-node-${{ env.PRIMARY_NODE_VERSION }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-node-${{ env.PRIMARY_NODE_VERSION }}-pnpm-

      - name: Install (immutable)
        run: pnpm install --frozen-lockfile

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

      - name: Verify and build documentation
        env:
          DOCS_BASE: ${{ steps.pages.outputs.base_path }}/
        run: pnpm run doc-sync

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v4
        with:
          path: website/.dist

  deploy:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
