name: Main workflow

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]
    tags:
      [
        "v[0-9]+",
        "v[0-9]+.[0-9]+",
        "v[0-9]+.[0-9]+.[0-9]+",
        "v[0-9]+.[0-9]+.[0-9]+-*",
      ]

jobs:
  # We have two separate build jobs:
  # - build-test-doc should run only once (one OS, one OCaml version), to avoid
  #   needlessly building the doc artifact repeatedly
  # - build-test can run on as many version as we want
  # - doc is also only run once, and only on push events, not on pull requests
  build-test-doc:
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          # - macos-latest
          # - windows-latest
        ocaml-compiler:
          - 4.14.x

    runs-on: ${{ matrix.os }}
    outputs:
      ref: ${{ steps.vars.outputs.ref }}

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

      - name: Use OCaml ${{ matrix.ocaml-compiler }}
        uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: ${{ matrix.ocaml-compiler }}

      - name: Install dependencies
        run: opam install . --deps-only --with-test

      - name: Build library
        run: opam exec -- dune build

      - name: Run unit tests
        run: opam exec -- dune test

      - name: Build documentation
        run: |
          opam install . --deps-only --with-doc
          opam exec -- dune build @doc

      # - id: vars
      #   shell: bash
      #   run: echo "ref=$(echo ${GITHUB_REF#refs/*/})" >> ${GITHUB_OUTPUT}

      # - uses: actions/upload-artifact@v4
      #   if: github.event_name == 'push' &&
      #     ( github.ref == 'refs/heads/main' || startsWith(github.ref,'refs/tags') )
      #   with:
      #     name: doc-${{ steps.vars.outputs.ref }}
      #     path: _build/default/_doc/_html/patricia-tree/

  build-test:
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          # - windows-latest
        ocaml-compiler:
          - 4.14.x
          - 5.1.x
          - 5.2.x
        exclude:
          - os: "ubuntu-latest"
            ocaml-compiler: 4.14.x

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

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

      - name: Use OCaml ${{ matrix.ocaml-compiler }}
        uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: ${{ matrix.ocaml-compiler }}

      - name: Install dependencies
        run: opam install . --deps-only --with-test

      - name: Build library
        run: opam exec -- dune build

      - name: Run unit tests
        run: opam exec -- dune test

  # doc:
  #   needs: build-test-doc
  #   if: github.event_name == 'push' &&
  #     ( github.ref == 'refs/heads/main' || startsWith(github.ref,'refs/tags') )
  #   permissions:
  #     contents: write
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: Checkout gh-pages
  #       uses: actions/checkout@v4
  #       with:
  #         ref: gh-pages
  #     - name: Remove previous doc
  #       run: rm -rf ${{ needs.build-test-doc.outputs.ref }}
  #     - name: Retrieve new documentation
  #       uses: actions/download-artifact@v4
  #       with:
  #         name: doc-${{ needs.build-test-doc.outputs.ref }}
  #         path: ${{ needs.build-test-doc.outputs.ref }}
  #     - name: Deploy documentation
  #       run: |
  #         git config user.email "${{ github.actor }}@users.noreply.github.com"
  #         git config user.name "${{ github.actor }}"
  #         git add ${{ needs.build-test-doc.outputs.ref }}
  #         if ! git diff --cached --quiet --exit-code;
  #         then
  #           git commit -m "Deploy ${GITHUB_SHA}"
  #           git push -f "https://${{ github.actor }}:${{ github.token }}@github.com/${{ github.repository }}.git" gh-pages
  #         else
  #           echo "No changes to push"
  #         fi
