name: Build and upload

on:
  pull_request:
  push:
    tags:
      - "*"
    branches:
      - "main"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-15-intel, macos-15]

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true

      - name: Setup Miniconda (Windows)
        if: runner.os == 'Windows'
        uses: conda-incubator/setup-miniconda@v3.0.3

      - name: Install Dependencies (Windows)
        if: runner.os == 'Windows'
        shell: powershell
        run: conda install -c conda-forge mpir zlib -y

      - name: Set env (Windows)
        if: runner.os == 'Windows'
        run: |
          echo "appdata=$env:LOCALAPPDATA" >> ${env:GITHUB_ENV}
          echo "GMP_INC=C:\Miniconda\envs\test\Library\include" >> ${env:GITHUB_ENV}
          echo "GMP_LIB=C:\Miniconda\envs\test\Library\lib" >> ${env:GITHUB_ENV}

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.2.0
        env:
          CIBW_ENVIRONMENT_MACOS: 'MACOSX_DEPLOYMENT_TARGET="15.0"'
          CIBW_ARCHS_MACOS: "auto"


      - name: List generated wheels
        run: ls ./wheelhouse/*

      - uses: actions/upload-artifact@v4
        with:
          path: ./wheelhouse/*.whl
          name: pytetwild-wheel-${{ matrix.os }}

  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install \
          libblas-dev \
          libboost-filesystem-dev \
          libboost-system-dev \
          libboost-thread-dev \
          libglu1-mesa-dev \
          libsuitesparse-dev \
          libxi-dev \
          libxcursor-dev \
          libxinerama-dev \
          libxrandr-dev \
          libx11-dev \
          libxcb1-dev \
          libxau-dev \
          libxdmcp-dev \
          xorg-dev \
          ccache

      - name: Build source distribution
        run: |
          pipx run build --sdist

      - name: Validate
        run: |
          pip install twine
          twine check dist/*

      - name: Install from dist/
        run: |
          sdist_file=$(ls dist/*.tar.gz)
          pip install "$sdist_file[dev]"

      - name: Test
        run: pytest -vv

      - uses: actions/upload-artifact@v4
        with:
          path: dist/*.tar.gz
          name: pytetwild-sdist

  release:
    name: Release
    if: github.event_name == 'push' && contains(github.ref, 'refs/tags')
    needs: [build_wheels, build_sdist]
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/pytetwild
    permissions:
      id-token: write  # this permission is mandatory for trusted publishing
    steps:
      - uses: actions/download-artifact@v4
      - name: Flatten directory structure
        run: |
          mkdir -p dist/
          find . -name '*.whl' -exec mv {} dist/ \;
          find . -name '*.tar.gz' -exec mv {} dist/ \;
      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          generate_release_notes: true
          files: |
            ./**/*.whl
