name: Build and Deploy Jupyter Book

on:
  # Trigger on push to main branch
  push:
    branches:
      - main
    paths:
      - '**.ipynb'          # Any notebook in any lab folder
      - 'book/_config.yml'
      - 'book/_toc.yml'
      - 'book/intro.md'
      - 'book/ADDING_A_NOTEBOOK.md'
      - 'requirements.txt'
      - '.github/workflows/deploy-book.yml'

  # Allow manual trigger
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow one concurrent deployment
concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  # Fork-only guard: this repository is a fork of bcgov/wps-tutorials, and the
  # same deploy-book.yml may be merged upstream. The job-level `if` skips every
  # run unless the repo is exactly this fork, so only ONE published book exists
  # (https://vanislekahuna.github.io/wps-labs/). In the parent repo the run is
  # skipped (grey), never failed (red). Note: the check must live here — the
  # `on:` trigger cannot filter by repository.
  build:
    if: github.repository == 'vanislekahuna/wps-labs'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
          cache: 'pip'

      - name: Remove Jupyter Book 2.x
        run: pip uninstall -y jupyter-book || true

      - name: Install dependencies
        run: |
          pip install --upgrade pip
          pip install "jupyter-book<2.0"
          pip install -r requirements.txt || echo "No additional requirements to install"

      - name: Check jupyter-book version
        run: |
          which jupyter-book
          jupyter-book --version

      - name: Build Jupyter Book
        run: |
          jupyter-book build book

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: './book/_build/html'

  deploy:
    if: github.repository == 'vanislekahuna/wps-labs'
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
