name: CI
on:
  push:
    branches: [ main ]
    tags: [ 'v[0-9]+\.[0-9]+.*' ]
  pull_request: { branches: [main] }
  workflow_call:

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: "3.9"
      - uses: pre-commit/action@v2.0.0

  test1:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
    env:
      TEST_WITH_COVERAGE: ${{ matrix.python-version == '3.11' && matrix.os == 'ubuntu-latest' }}
    steps:
    - uses: actions/checkout@master
    - name: Setup Python
      uses: actions/setup-python@master
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install packages
      run: |
        pip install -U pip wheel
        pip install .
        pip install .[dev,plots]
        python -c 'import skopt; print(skopt.__version__)'
    - name: Run tests
      if: env.TEST_WITH_COVERAGE != 'true'
      run: pytest

    - name: Test with Coverage
      if: env.TEST_WITH_COVERAGE == 'true'
      run: |
        make test-coverage

    - name: Upload coverage reports to Codecov
      if: env.TEST_WITH_COVERAGE == 'true'
      uses: codecov/codecov-action@v4.0.1
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        slug: holgern/scikit-optimize

    - name: Build package
      if: env.TEST_WITH_COVERAGE == 'true'
      run: |
        pip install --upgrade build
        python -m build --sdist --wheel --outdir dist/

    - name: Upload artifact
      if: env.TEST_WITH_COVERAGE == 'true'
      uses: actions/upload-artifact@v4
      with:
        name: package
        path: dist

  test2:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        python-version: ["3.8", "3.9", "3.10", "3.11"]
        scikit-learn-version: ["1.5.0", "1.3.2", "1.2.2", "1.1.3", "1.0.2"]
        numpy-version: ["1.20.3", "1.22.4","1.24.4", "1.26.4"]
        exclude:
          - python-version: "3.8"
            scikit-learn-version: "1.5.0"
          - python-version: "3.8"
            numpy-version: "1.26.4"
          - python-version: "3.11"
            scikit-learn-version: "1.0.2"
          - python-version: "3.11"
            scikit-learn-version: "1.1.3"
          - python-version: "3.10"
            numpy-version: "1.20.3"
          - python-version: "3.11"
            numpy-version: "1.20.3"
          - python-version: "3.11"
            numpy-version: "1.22.4"
    steps:
    - uses: actions/checkout@master
    - name: Setup Python
      uses: actions/setup-python@master
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install packages
      run: |
        pip install -U pip wheel
        pip install numpy==${{ matrix.numpy-version }}
        pip install scikit-learn==${{ matrix.scikit-learn-version }}
        pip install .
        pip install .[dev,plots]
        python -c 'import skopt; print(skopt.__version__)'
    - name: Run tests
      run: pytest

  doctest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: "3.10"
      - name: Install packages
        run: |
            pip install -U pip wheel
            pip install .
            pip install .[dev,plots,doc]
            python -c 'import skopt; print(skopt.__version__)'

      - name: Install latex dependencies
        run: |
          sudo apt-get -yq update
          sudo apt-get -yq --no-install-recommends install \
              dvipng texlive-latex-base texlive-latex-extra \
              texlive-latex-recommended texlive-fonts-recommended \
              latexmk tex-gyre gsfonts ccache

      - name: Test docs
        run: |
          python -c 'import skopt; skopt.show_versions()'
          make test-doc
          make test-sphinxext

      - name: doctest
        run: |
          python -c 'import skopt; skopt.show_versions()'
          make -C doc doctest

      - name: linkcheck
        run: |
          python -c 'import skopt; skopt.show_versions()'
          make -C doc linkcheck

      - name: Build docs
        run: |
          .github/scripts/build_docs.sh

      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: docs
          path: doc/_build/html/stable
