name: Build TinyTorch PDF

# Build PDF version of the book
# Runs manually or on release tags
on:
  workflow_dispatch:
    inputs:
      method:
        description: 'Build method (simple or latex)'
        required: true
        default: 'simple'
        type: choice
        options:
          - simple
          - latex
  release:
    types: [published]

jobs:
  build-pdf:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

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

    - name: Install base dependencies
      run: |
        pip install --upgrade pip
        pip install "jupyter-book<1.0"
        pip install -r book/requirements.txt

    - name: Install LaTeX (if latex method)
      if: github.event.inputs.method == 'latex' || github.event_name == 'release'
      run: |
        sudo apt-get update
        sudo apt-get install -y \
          texlive-latex-extra \
          texlive-fonts-recommended \
          texlive-latex-recommended \
          latexmk

    - name: Install pyppeteer (if simple method)
      if: github.event.inputs.method == 'simple' || github.event.inputs.method == ''
      run: |
        pip install pyppeteer
        # Pre-download Chromium to avoid timeout during build
        python -c "from pyppeteer import chromium_downloader; chromium_downloader.download_chromium()"

    - name: Build PDF (simple method)
      if: github.event.inputs.method == 'simple' || github.event.inputs.method == ''
      run: |
        cd book
        jupyter-book clean . --all
        jupyter-book build . --builder pdfhtml
        # Copy to standard location
        mkdir -p _build/pdf-output
        cp _build/pdf/book.pdf _build/pdf-output/tinytorch-course.pdf

    - name: Build PDF (LaTeX method)
      if: github.event.inputs.method == 'latex' || github.event_name == 'release'
      run: |
        cd book
        jupyter-book clean . --all
        jupyter-book build . --builder pdflatex
        # Copy to standard location
        mkdir -p _build/pdf-output
        cp _build/latex/tinytorch-course.pdf _build/pdf-output/tinytorch-course.pdf

    - name: Upload PDF artifact
      uses: actions/upload-artifact@v4
      with:
        name: tinytorch-pdf-${{ github.event.inputs.method || 'latex' }}-${{ github.sha }}
        path: book/_build/pdf-output/tinytorch-course.pdf
        retention-days: 90

    - name: Upload to release (if release event)
      if: github.event_name == 'release'
      uses: softprops/action-gh-release@v1
      with:
        files: book/_build/pdf-output/tinytorch-course.pdf
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Build summary
      run: |
        echo "## 📚 PDF Build Complete" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "**Method:** ${{ github.event.inputs.method || 'latex' }}" >> $GITHUB_STEP_SUMMARY
        echo "**File:** tinytorch-course.pdf" >> $GITHUB_STEP_SUMMARY
        echo "**Size:** $(du -h book/_build/pdf-output/tinytorch-course.pdf | cut -f1)" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "Download the PDF from the artifacts section above." >> $GITHUB_STEP_SUMMARY

