name: Deploy Documentation

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build-docs:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pages: write
      id-token: write
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'
          cache: 'pip'
          
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip setuptools wheel
          pip install -r requirements.txt
          pip install -e ".[dev]"
          pip install sphinx sphinx-rtd-theme sphinx-autodoc-typehints
          
      - name: Build documentation
        run: |
          cd docs
          echo "Cleaning previous builds..."
          make clean
          echo "Building documentation..."
          make html
          # Check if build was successful
          if [ ! -d "_build/html" ]; then
            echo "Documentation build failed"
            echo "Current directory contents:"
            ls -la
            echo "Build directory contents:"
            ls -la _build || true
            exit 1
          fi
          echo "Documentation build successful"
          echo "Checking build directory contents:"
          ls -la _build/html
          
      - name: Check documentation
        run: |
          cd docs
          echo "Running strict build check..."
          make build-strict || true  # Don't fail on warnings
          
      - name: Setup Pages
        id: pages
        uses: actions/configure-pages@v4
        
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: './docs/_build/html'
          
      - name: Deploy to GitHub Pages
        if: github.ref == 'refs/heads/main'
        uses: actions/deploy-pages@v4
