name: Run Jupyter Notebooks

on:
  pull_request:
    branches:
      - main
      - dev

jobs:
  run-notebooks:
    if: github.actor != 'github-actions[bot]'
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4


      - name: Set up Miniconda
        uses: conda-incubator/setup-miniconda@v3
        with:
          python-version: ${{ matrix.python-version }}

      - name: Initialize Conda
        run: |
          conda init bash

      - name: Create conda environment
        run: |
          conda env create -f environment.yml
          source ~/.bashrc
          conda activate Paidiverpy
          pip install --upgrade pip
          pip install nbconvert nbclient
          pip install .

      - name: Run Notebooks in "examples/example_notebooks"
        run: |
          source ~/.bashrc
          conda activate Paidiverpy

          for notebook in examples/example_notebooks/*.ipynb; do
            echo "Running $notebook..."
            notebook_name=$(basename "$notebook")
            executed_notebook="executed_$notebook_name"
            # Execute the notebook and save it as executed_<name>
            (cd examples/example_notebooks && jupyter nbconvert --to notebook --execute "$notebook_name" --output "$executed_notebook") || exit 1
            # Rename the executed notebook to the original notebook name, overwriting it if necessary
            mv -f examples/example_notebooks/"$executed_notebook" examples/example_notebooks/"$notebook_name"
          done
