name: tests

on:
  push:
  workflow_dispatch:

env:
  POETRY_VERSION: "1.7.0"
  OCAML_COMPILER: "5.1"

jobs:
  deploy:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - macos-11 # x86_64
          - macos-14 # aarch64
          - ubuntu-latest
          # - windows-latest  # ocaml5 will be coming with opam 2.2
        python-version:
          - "3.10"
          - "3.11"
          - "3.12"
        include:
          # Python 3.9 is not available on macos14
          - python-version: "3.9"
            os: ubuntu-latest
          - python-version: "3.9"
            os: macos-11

    steps:
      - name: Checkout Python code
        uses: actions/checkout@v4

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

      - name: Install and configure Poetry
        uses: snok/install-poetry@v1.4.1
        with:
          version: ${{ env.POETRY_VERSION }}
          virtualenvs-create: true
          virtualenvs-in-project: true

      - name: Install Ocaml ${{ env.OCAML_COMPILER }}
        uses: ocaml/setup-ocaml@v3
        with:
          ocaml-compiler: ${{ env.OCAML_COMPILER }}
          dune-cache: true

      - name: Compile and install Ocaml facile library
        working-directory: ./ocaml/facile
        run: |
          opam install . --deps-only
          opam exec -- dune build
          opam exec -- dune install

      - name: Compile Ocaml binding for Facile
        working-directory: ./python/binding
        run: |
          opam exec -- dune build

      - name: Install Python facile library
        working-directory: ./python
        run: |
          opam exec -- poetry install

      - name: Style checking
        working-directory: ./python
        run: |
          poetry run ruff check facile examples tests
          poetry run ruff format --check facile examples tests

          # - name: Type checking
          #   working-directory: ./python
          #   run: |
          #     poetry run mypy facile examples tests

      - name: Run Pytest and examples
        working-directory: ./python
        run: |
          poetry run pytest
          # for file in examples; do poetry run python $file; done

      - name: Build wheel
        working-directory: ./python
        run: |
          opam exec -- poetry build

      - name: Upload the build artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.os }}-${{ matrix.python-version }}-facile-wheel
          path: ./python/dist/*.whl
