name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  test:
    name: "Tests Core"
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ["ubuntu-latest"]
        python-version: ["3.9", "3.12"]
        pytest_opts: ["--workers 4 --tests-per-worker 1"]

    steps:
      - name: Checkout source
        uses: actions/checkout@v4
      - name: Setup python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          architecture: x64

      - uses: prefix-dev/setup-pixi@v0.8.1
        with:
          pixi-version: v0.39.4
          cache: true

      - name: set numba parallel flags
        run: |
          echo "NUMBA_NUM_THREADS=1" >> $GITHUB_ENV

      - name: Setup r2u
        uses: eddelbuettel/github-actions/r2u-setup@master

      - name: Print R version
        run: |
          Rscript -e 'R.version'

      - name: Install R packages
        run: |
          R_LIB_PATH="${{ github.workspace }}/.pixi/envs/dev/lib/R/library"
          mkdir -p $R_LIB_PATH
          Rscript -e ".libPaths('$R_LIB_PATH'); install.packages(c('fixest', 'broom', 'did2s', 'wildrwolf', 'ivDiag', 'car'),  lib='/home/runner/work/pyfixest/pyfixest/.pixi/envs/dev/lib/R/library')"
          Rscript -e ".libPaths('$R_LIB_PATH'); install.packages('ritest', lib='/home/runner/work/pyfixest/pyfixest/.pixi/envs/dev/lib/R/library', repos = c('https://grantmcdermott.r-universe.dev'))"

      - name: Run 'regular 'tests
        run: |
          pixi run tests-regular

      # Run tests for PRs with the label "tests-extended"
      #- name: Run tests for plots (only on PRs with the 'tests-extended' label)
      #  if: github.event_name == 'pull_request' && contains(github.event.label.name, 'tests-extended')
      #  run: pixi run tests-extended

      # Run tests for push to master
      #- name: Run tests for push to master
      #  if: github.event_name == 'push' && github.ref == 'refs/heads/master'
      #  run: pixi run tests-extended

      - name: Upload to Codecov
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          partial: true
          flags: core-tests

  test_slow:
    name: "Tests vs fixest"
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ["ubuntu-latest"]
        python-version: ["3.9", "3.12"]
        pytest_opts: ["--workers 4 --tests-per-worker 1"]

    steps:
      - name: Checkout source
        uses: actions/checkout@v4
      - name: Setup python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          architecture: x64

      - uses: prefix-dev/setup-pixi@v0.8.1
        with:
          pixi-version: v0.39.4
          cache: true


      - name: set numba parallel flags
        run: |
          echo "NUMBA_NUM_THREADS=1" >> $GITHUB_ENV

      - name: Setup r2u
        uses: eddelbuettel/github-actions/r2u-setup@master

      - name: Install R packages
        run: |
          R_LIB_PATH="${{ github.workspace }}/.pixi/envs/dev/lib/R/library"
          mkdir -p $R_LIB_PATH
          Rscript -e ".libPaths('$R_LIB_PATH'); install.packages(c('fixest', 'broom','did2s', 'wildrwolf', 'ivDiag', 'car'),  lib='/home/runner/work/pyfixest/pyfixest/.pixi/envs/dev/lib/R/library')"
          Rscript -e ".libPaths('$R_LIB_PATH'); install.packages('ritest', lib='/home/runner/work/pyfixest/pyfixest/.pixi/envs/dev/lib/R/library', repos = c('https://grantmcdermott.r-universe.dev'))"

      - name: Run 'slow 'tests
        run: |
          pixi run tests-slow

      #- name: Upload to Codecov
      #  uses: codecov/codecov-action@v4
      #  with:
      #    token: ${{ secrets.CODECOV_TOKEN }}
      #    partial: true
      #    flags: tests-vs-fixest



  build-docs:
    name: "Build Docs"
    runs-on: ubuntu-latest
    needs: ["test"]
    steps:
      - name: Checkout source
        uses: actions/checkout@v4
      - name: Setup python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - uses: prefix-dev/setup-pixi@v0.8.1
        with:
          pixi-version: v0.39.4
          cache: true

      - name: Set up Quarto
        uses: quarto-dev/quarto-actions/setup@v2

      - name: install tinytex
        run: |
          quarto install tinytex

      - name: Compile docstrings with quartodoc
        run: |
          pixi run docs-build
      - name: Render docs
        run: |
          pixi run docs-render
      - name: Save docs artifact
        uses: actions/upload-artifact@v4
        with:
          name: docs-html
          path: docs/_site


  publish-docs:
    name: "Publish Docs"
    runs-on: ubuntu-latest
    permissions:
      contents: write
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    needs: ["build-docs", "test"]
    if: github.ref == 'refs/heads/master'
    steps:
      - name: Download docs artifact
        uses: actions/download-artifact@v4
        with:
          name: docs-html
          path: docs/_site
      - name: Publish docs to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: docs/_site
