name: Deploy Sphinx Documentation

on:
  push:
    branches:
      - main
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install sphinx sphinx_rtd_theme myst-parser
          # Add any additional dependencies your docs need
          # pip install -r datamule/docs/requirements.txt

      - name: Clean and Build Documentation
        run: |
          cd datamule/docs
          rm -rf _build/
          rm -rf build/
          make clean
          make html
          ls -la
          ls -la _build/ || true
          ls -la build/ || true
          
      - name: Check Build Directory
        run: |
          pwd
          ls -la datamule/docs/_build/html || true
          ls -la datamule/docs/build/html || true
          
      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./datamule/docs/build/html
          force_orphan: true
          user_name: 'github-actions[bot]'
          user_email: 'github-actions[bot]@users.noreply.github.com'
          commit_message: 'Deploy Sphinx documentation'