name: Deploy Documentation

on:
  push:
    branches: [main, master]
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  RUST_MIN_STACK: 8388608

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

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

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      
      - name: Setup Pages
        uses: actions/configure-pages@v5
        
      - name: Build Documentation
        run: |
          # Build docs with same features as docs.rs (excluding python-ast)
          cargo doc --no-deps --features "default,rust-ast,typescript-ast,c-ast,cpp-ast,kotlin-ast,demo" --manifest-path Cargo.toml
          echo '<meta http-equiv="refresh" content="0; url=pmat/index.html">' > target/doc/index.html
          # Only copy rust-docs if it exists
          if [ -d "rust-docs" ]; then
            cp -r rust-docs target/doc/
          fi
          
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: './target/doc'

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