name: batoid CI

on:
  push:
    branches:
      - main
      - releases/*

  pull_request:
    branches:
      - main
      - releases/*
  workflow_dispatch:

jobs:
  build:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ ubuntu-latest ]
        py: [3.8, 3.9, '3.10', 3.11, 3.12]
        CC: [ gcc ]
        CXX: [ g++ ]
        include:
          - os: macos-latest
            py: 3.8
            CC: cc
            CXX: c++
          - os: ubuntu-latest
            py: 3.8
            CC: clang
            CXX: clang++
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Print github context properties
        run: |
          echo 'event: ${{ github.event_name }}'
          echo 'sha: ${{ github.sha }}'
          echo 'ref: ${{ github.ref }}'
          echo 'head_ref: ${{ github.head_ref }}'
          echo 'base_ref: ${{ github.base_ref }}'
          echo 'event.before: ${{ github.event.before }}'
          echo 'event.after: ${{ github.event.after }}'

      - name: Set up Python ${{ matrix.py }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.py }}

      - name: Install libfftw, etc. on linux (needed for GalSim)
        if: matrix.os == 'ubuntu-latest'
        run: |
          echo ${{ matrix.os }}
          sudo -H apt-get -qq update
          sudo -H apt-get install -y libfftw3-dev libeigen3-dev

      - name: Install libfftw, etc. on MacOS
        if: matrix.os == 'macos-latest'
        run: |
          # brew update takes forever to run.  Hopefully won't need to enable this too often.
          # Note: The || true bit here is because brew returns an error code if everything is
          #       already installed, which is goofy.  So || true means these always give
          #       non-error values.  If there really is an error, we should notice later
          #       when trying to use whatever package failed to install.
          # brew update || true
          brew install fftw wget || true
          brew upgrade wget || true
          brew link --overwrite fftw gcc wget || true
          brew install eigen || true
          brew link --overwrite eigen || true

      - name: Install dependencies
        env:
          LIBRARY_PATH: /opt/homebrew/lib:$LIBRARY_PATH
          LD_LIBRARY_PATH: /opt/homebrew/lib:$LD_LIBRARY_PATH
        run: |
          python -m pip install -U pip

          # Do these first to clarify potential conflicts
          pip install -U setuptools numpy

          # Standard dependencies
          pip install -U -r requirements.txt

          # Use pybind11-global to make CMake files visible
          pip install -U pybind11-global

      - name: Try numpy 2 # Delete me after this is default
        if: matrix.py == 3.12
        run: pip install numpy==2.0.0rc2

      - name: Install remaining dependencies
        run: |
          pip install -U -r test_requirements.txt

      - name: List all installed packages for reference
        run: pip list

      - name: Build batoid
        run: CMAKE_VERBOSE_MAKEFILE=1 pip install .

      - name: Run unit tests
        run: pytest --cov=batoid --cov-report=xml --cov-config tests/.coveragerc --skip_gha --durations=8 -ra

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          verbose: true
