name: Build and Publish Wheels

on:
  push:
    tags:
      - 'v*.*.*'

jobs:
  build-linux:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Set up Python (controller)
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install build tools
        run: |
          python -m pip install --upgrade pip
          pip install cibuildwheel==2.* build

      - name: Get version from tag
        id: get_version
        shell: bash
        run: |
          VERSION="${GITHUB_REF#refs/tags/v}"
          echo "VERSION=$VERSION" >> "$GITHUB_ENV"

      - name: Update version in __init__.py
        shell: bash
        run: |
          tmp=python/magnetron/__init__.py.tmp
          sed '/^__version__\s*=.*/d' python/magnetron/__init__.py > "$tmp"
          mv "$tmp" python/magnetron/__init__.py
          echo "__version__ = \"$VERSION\"" >> python/magnetron/__init__.py

      - name: Build wheels (Linux x86_64, Python ≥3.11)
        env:
          CIBW_ARCHS_LINUX: "x86_64"
          CIBW_BUILD: "cp3*-manylinux_x86_64"
          CIBW_SKIP: "cp310-* *-musllinux_*"
        run: |
          python -m cibuildwheel --output-dir wheelhouse .

      - name: Upload Linux wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-linux-x86_64
          path: wheelhouse/*.whl

  build-macos-arm64:
    runs-on: macos-14   # Apple Silicon runner (arm64)
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Set up Python (controller)
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install build tools
        run: |
          python -m pip install --upgrade pip
          pip install cibuildwheel==2.* build

      - name: Get version from tag
        id: get_version
        shell: bash
        run: |
          VERSION="${GITHUB_REF#refs/tags/v}"
          echo "VERSION=$VERSION" >> "$GITHUB_ENV"

      - name: Update version in __init__.py
        shell: bash
        run: |
          tmp=python/magnetron/__init__.py.tmp
          sed '/^__version__\s*=.*/d' python/magnetron/__init__.py > "$tmp"
          mv "$tmp" python/magnetron/__init__.py
          echo "__version__ = \"$VERSION\"" >> python/magnetron/__init__.py

      - name: Build wheels (macOS arm64, Python ≥3.11)
        env:
          CIBW_ARCHS_MACOS: "arm64"
          CIBW_BUILD: "cp311-* cp312-* cp313-*"
          CIBW_BEFORE_ALL_MACOS: |
            set -eux
            brew update
            brew install cmake ninja || true
        run: |
          python -m cibuildwheel --output-dir wheelhouse .

      - name: Upload macOS wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-macos-arm64
          path: wheelhouse/*.whl

  publish:
    runs-on: ubuntu-latest
    needs: [build-linux, build-macos-arm64]
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install build/publish tools
        run: |
          python -m pip install --upgrade pip
          pip install build

      - name: Get version from tag
        id: get_version
        shell: bash
        run: |
          VERSION="${GITHUB_REF#refs/tags/v}"
          echo "VERSION=$VERSION" >> "$GITHUB_ENV"

      - name: Update version in __init__.py (for sdist)
        shell: bash
        run: |
          tmp=python/magnetron/__init__.py.tmp
          sed '/^__version__\s*=.*/d' python/magnetron/__init__.py > "$tmp"
          mv "$tmp" python/magnetron/__init__.py
          echo "__version__ = \"$VERSION\"" >> python/magnetron/__init__.py

      - name: Build sdist
        run: |
          mkdir -p dist
          python -m build --sdist . --outdir dist

      - name: Download built wheels
        uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          merge-multiple: true
          path: dist

      - name: Publish to PyPI
        if: startsWith(github.ref, 'refs/tags/v')
        uses: pypa/gh-action-pypi-publish@v1.10.3
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          packages_dir: dist
          skip_existing: true
