name: Python package

on:
  push:
    branches:
    - dev
  pull_request:
    branches:
    - main
    - dev

jobs:
  build:
    env:
      PYTHONIOENCODING: utf-8
    if: github.actor != 'github-actions[bot]'
    name: Build for (${{ matrix.python-version }}, ${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
        python-version: ['3.10', '3.11', '3.12']
    steps:
      - uses: actions/checkout@v4

      - name: Set up Miniconda
        uses: conda-incubator/setup-miniconda@v3
        with:
          python-version: ${{ matrix.python-version }}

      - name: Initialize Conda
        run: |
          conda init bash

      - name: Configure Conda retries
        shell: bash -l {0}
        run: |
          conda config --set remote_read_timeout_secs 60
          conda config --set remote_connect_timeout_secs 60
          conda config --set remote_max_retries 3

      - name: Create conda environment
        shell: bash -l {0}
        run: |
          conda env create -f environment.yml python=${{ matrix.python-version }} -y
          conda activate Paidiverpy
          pip install -e .
          pip install -e ".[test]"
          pip install -e ".[dev,publishing]"

      - name: Test with pytest
        shell: bash -l {0}
        run: |
          conda activate Paidiverpy
          pytest -ra -v -s \
            --cov \
            --cov-report xml:cov.xml \
            --cov-report term-missing \
            --junitxml=junit.xml -o junit_family=legacy

      - name: Upload coverage to Codecov
        if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12' && github.event_name == 'push'
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./cov.xml
          fail_ci_if_error: true

      - name: Verify that we can build the package
        run:  |
          pip install build twine wheel
          python -m build

      - name: Update conda meta file and commit
        if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12' && github.event_name == 'push'
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

          if [ "${{ github.event_name }}" = "pull_request" ]; then
            BRANCH="${{ github.head_ref }}"
          else
            BRANCH="${{ github.ref_name }}"
          fi

          git fetch origin $BRANCH
          git checkout $BRANCH

          sha256=$(sha256sum dist/*.tar.gz | cut -d ' ' -f 1)
          if grep -q '{% set sha256' conda_recipes/meta.yaml; then
              sed -i "s|{% set sha256 = .* %}|{% set sha256 = \"$sha256\" %}|" conda_recipes/meta.yaml
          else
              sed -i "1i{% set sha256 = \"$sha256\" %}" conda_recipes/meta.yaml
          fi

          if grep -q '{% set sha256' conda_recipes/meta_bioconda.yaml; then
              sed -i "s|{% set sha256 = .* %}|{% set sha256 = \"$sha256\" %}|" conda_recipes/meta_bioconda.yaml
          else
              sed -i "1i{% set sha256 = \"$sha256\" %}" conda_recipes/meta_bioconda.yaml
          fi

          git add conda_recipes/meta_bioconda.yaml conda_recipes/meta.yaml
          git commit -m "chore: update conda recipe" || true

          git stash --all || true
          git pull --rebase origin $BRANCH
          git stash drop || true
          git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git HEAD:$BRANCH
