name: Deploy Documentation to GitHub Pages

on:
  push:
    branches: [ main ]
    paths:
      - 'pmcp-book/**'
      - 'pmcp-course/**'
      - 'docs/**'
      - '.github/workflows/docs.yml'
  pull_request:
    branches: [ main ]
    paths:
      - 'pmcp-book/**'
      - 'pmcp-course/**'
      - 'docs/**'
      - '.github/workflows/docs.yml'
  workflow_dispatch:

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

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install mdBook
        run: |
          curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.40/mdbook-v0.4.40-x86_64-unknown-linux-gnu.tar.gz | tar -xz
          chmod +x mdbook
          sudo mv mdbook /usr/local/bin/

      - name: Install mdbook-quiz
        run: |
          cargo install mdbook-quiz --locked || echo "mdbook-quiz installation failed, continuing without quiz support"

      - name: Install mdbook-exercises
        run: |
          cargo install mdbook-exercises --locked || echo "mdbook-exercises installation failed, continuing without exercise support"

      - name: Build PMCP Guide (Book)
        run: |
          cd pmcp-book
          mdbook build

      - name: Build PMCP Course
        run: |
          cd pmcp-course
          mdbook build

      - name: Create combined site
        run: |
          mkdir -p site

          # Copy book to /book/ subdirectory
          mkdir -p site/book
          cp -r pmcp-book/book/* site/book/

          # Copy course to /course/ subdirectory
          mkdir -p site/course
          cp -r pmcp-course/book/* site/course/

          # Copy landing page (must be last to not be overwritten)
          cp docs/index.html site/
          cp -r docs/assets site/ 2>/dev/null || true

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v4
        with:
          path: site

  deploy:
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
