name: Build and publish wheels

on:
  push:
    tags:
      - 'v*'

jobs:
  # Build wheels for x86_64 platforms
  build-x86:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-15-intel]
        python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14']

    steps:
      - uses: actions/checkout@v4
      
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          command: build
          args: --release --out dist --features python,parallel
          maturin-version: latest

      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-${{ matrix.os }}-${{ matrix.python-version }}
          path: dist

      - name: Verify wheel integrity
        run: |
          python3 -m pip install wheel
          for whl in dist/*.whl; do
            if [ -f "$whl" ]; then
              python3 -m wheel unpack "$whl" -d /tmp/wheel_check && echo "✓ $whl is valid"
              rm -rf /tmp/wheel_check/*
            fi
          done
        shell: bash

  # Build wheels for Linux ARM64
  build-linux-arm:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14']

    steps:
      - uses: actions/checkout@v4
      
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      
      - name: Build wheels (Linux ARM64)
        uses: PyO3/maturin-action@v1
        with:
          target: aarch64-unknown-linux-gnu
          command: build
          args: --release --out dist --features python,parallel -i python${{ matrix.python-version }}
          maturin-version: latest

      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-linux-arm64-${{ matrix.python-version }}
          path: dist

      - name: Verify wheel integrity
        run: |
          python3 -m pip install wheel
          for whl in dist/*.whl; do
            if [ -f "$whl" ]; then
              python3 -m wheel unpack "$whl" -d /tmp/wheel_check && echo "✓ $whl is valid"
              rm -rf /tmp/wheel_check/*
            fi
          done
        shell: bash

  # Build wheels for macOS ARM64 (Apple Silicon)
  build-macos-arm:
    runs-on: macos-latest
    strategy:
      matrix:
        python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14']

    steps:
      - uses: actions/checkout@v4
      
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      
      - name: Build wheels (macOS ARM64)
        uses: PyO3/maturin-action@v1
        with:
          target: aarch64-apple-darwin
          command: build
          args: --release --out dist --features python,parallel
          maturin-version: latest

      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-macos-arm64-${{ matrix.python-version }}
          path: dist

      - name: Verify wheel integrity
        run: |
          python3 -m pip install wheel
          for whl in dist/*.whl; do
            if [ -f "$whl" ]; then
              python3 -m wheel unpack "$whl" -d /tmp/wheel_check && echo "✓ $whl is valid"
              rm -rf /tmp/wheel_check/*
            fi
          done
        shell: bash

  # Build source distribution (sdist)
  build-sdist:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist
          maturin-version: latest

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

  # Publish to PyPI
  publish:
    needs: [build-x86, build-linux-arm, build-macos-arm, build-sdist]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')

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

      - uses: actions/download-artifact@v4
        with:
          name: sdist
          path: dist

      - name: Publish to PyPI
        uses: PyO3/maturin-action@v1
        env:
          MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
        with:
          command: upload
          args: --skip-existing dist/*

  # Publish to crates.io
  publish-crates:
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    continue-on-error: true
    
    steps:
      - uses: actions/checkout@v4
      
      - uses: dtolnay/rust-toolchain@stable
      
      - name: Publish to crates.io
        run: cargo publish --no-verify
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  # Create GitHub Release with artifacts
  github-release:
    needs: [build-x86, build-linux-arm, build-macos-arm, build-sdist]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4

      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          pattern: wheels-*
          merge-multiple: true

      - uses: actions/download-artifact@v4
        with:
          name: sdist
          path: artifacts

      - name: Extract changelog for this version
        run: |
          VERSION="${GITHUB_REF#refs/tags/v}"
          echo "Extracting changelog for version $VERSION"
          # Extract section between "## [VERSION]" and next "## [" or EOF
          awk "/^## \[$VERSION\]/{flag=1; next} /^## \[/{flag=0} flag" CHANGELOG.md > release_notes.md
          echo "--- Release notes ---"
          cat release_notes.md

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          files: artifacts/*
          body_path: release_notes.md

