name: Deploy to Pages

# Builds the full docs site and publishes to GitHub Pages:
#   /                  — landing page (index.md via Jekyll)
#   /docs/             — documentation (docs/*.md via Jekyll + raw HTML files)
#   /configurator/     — standalone Svelte configurator app
#
# Jekyll processes docs/*.md into HTML using the default layout.
# The built CSS bundles (dist/) and optional CSS are injected afterwards so
# that demo.html's relative paths resolve correctly in the browser.
#
# Runs on merges to main that touch any site-relevant path, and on demand.

on:
  push:
    branches: [main]
    paths:
      - 'configurator/**'
      - 'core/**'
      - 'optional/**'
      - 'docs/**'
      - 'index.md'
      - '_layouts/**'
      - '_config.yml'
      - 'CNAME'
      - '.github/workflows/deploy-configurator.yml'
  workflow_dispatch:
    inputs:
      git_ref:
        description: 'Exact git SHA or ref to build from (leave blank to use HEAD of the triggered ref)'
        required: false
        default: ''

permissions:
  contents: read

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

jobs:
  build:
    name: Build site
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0
        with:
          persist-credentials: false
          # When dispatched from release.yml, git_ref is the exact post-sync SHA
          # so the build always uses the version-bumped commit, not a racy HEAD.
          ref: ${{ inputs.git_ref || github.sha }}

      - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020  # v7.0.0
        with:
          node-version: 22
          cache: npm
          cache-dependency-path: |
            package-lock.json
            configurator/package-lock.json

      - name: Configure Pages
        uses: actions/configure-pages@45bfe0192ca1faeb007ade9deae92b16b8254a0d  # v6

      - name: Install root dependencies
        run: npm ci

      - name: Build CSS bundles and docs
        run: npm run build

      - name: Install configurator dependencies
        working-directory: configurator
        run: npm ci

      - name: Build configurator
        working-directory: configurator
        run: npm run build

      - name: Build Jekyll site
        uses: actions/jekyll-build-pages@v1
        with:
          source: ./
          destination: ./_site

      - name: Fix _site permissions after Jekyll
        run: sudo chown -R "$USER" _site/

      - name: Inject CSS bundles, optional CSS, and configurator
        run: |
          set -euo pipefail
          mkdir -p _site/dist _site/optional _site/configurator
          cp -r dist/. _site/dist/
          cp optional/*.css _site/optional/
          cp -r configurator/dist/. _site/configurator/

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9  # v5
        with:
          path: _site

  deploy:
    name: Deploy to Pages
    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
        id: deployment
        uses: actions/deploy-pages@cd2ce8fcbc39b97be8ca5fce6e763baed58fa128  # v5
