name: Release underthesea_core

on:
  push:
    branches: [main]
    paths:
      - 'extensions/underthesea_core/Cargo.toml'
      - '.github/workflows/release-pypi-core.yml'
    tags:
      - 'underthesea-core-v*'  # Manual tag release (e.g. underthesea-core-v3.1.0)

env:
  PYPI_TOKEN: ${{ secrets.PYPI_UNDERTHESEA_CORE_API_TOKEN }}

jobs:
  verify-version:
    name: "Verify Version"
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
      should_release: ${{ steps.check.outputs.should_release }}
    steps:
      - uses: actions/checkout@v4
      - name: Read version from Cargo.toml
        id: version
        run: |
          CARGO_VERSION=$(grep '^version = ' extensions/underthesea_core/Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
          echo "version=$CARGO_VERSION" >> "$GITHUB_OUTPUT"
          echo "Cargo.toml version: $CARGO_VERSION"
      - name: Verify tag matches Cargo.toml version
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          TAG_VERSION="${GITHUB_REF#refs/tags/underthesea-core-v}"
          if [ "$TAG_VERSION" != "${{ steps.version.outputs.version }}" ]; then
            echo "ERROR: Tag version ($TAG_VERSION) does not match Cargo.toml version (${{ steps.version.outputs.version }})"
            exit 1
          fi
          echo "✓ Tag matches Cargo.toml"
      - name: Check whether version is already on PyPI
        id: check
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          STATUS=$(curl -s -o /dev/null -w "%{http_code}" "https://pypi.org/pypi/underthesea-core/$VERSION/json")
          if [ "$STATUS" = "200" ]; then
            echo "underthesea-core $VERSION already on PyPI; nothing to release."
            echo "should_release=false" >> "$GITHUB_OUTPUT"
          else
            echo "underthesea-core $VERSION not yet on PyPI; will release."
            echo "should_release=true" >> "$GITHUB_OUTPUT"
          fi

  sdist:
    name: "Source Distribution"
    needs: verify-version
    if: needs.verify-version.outputs.should_release == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Remove target-cpu=native for portable binary
        run: rm -f ./extensions/underthesea_core/.cargo/config.toml
      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist
          working-directory: ./extensions/underthesea_core
      - name: Upload sdist
        uses: actions/upload-artifact@v4
        with:
          name: wheels-sdist
          path: ./extensions/underthesea_core/dist

  linux:
    name: "Linux ${{ matrix.target }}"
    needs: verify-version
    if: needs.verify-version.outputs.should_release == 'true'
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        target: [x86_64, aarch64]
    steps:
      - uses: actions/checkout@v4
      - name: Remove target-cpu=native for portable binary
        run: rm -f ./extensions/underthesea_core/.cargo/config.toml
      - uses: actions/setup-python@v5
        with:
          python-version: 3.x
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist -i 3.10 3.11 3.12 3.13 3.14
          manylinux: auto
          working-directory: ./extensions/underthesea_core
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-linux-${{ matrix.target }}
          path: ./extensions/underthesea_core/dist

  musllinux:
    name: "Linux musl ${{ matrix.target }}"
    needs: verify-version
    if: needs.verify-version.outputs.should_release == 'true'
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        target: [x86_64, aarch64]
    steps:
      - uses: actions/checkout@v4
      - name: Remove target-cpu=native for portable binary
        run: rm -f ./extensions/underthesea_core/.cargo/config.toml
      - uses: actions/setup-python@v5
        with:
          python-version: 3.x
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist -i 3.10 3.11 3.12 3.13 3.14
          manylinux: musllinux_1_2
          working-directory: ./extensions/underthesea_core
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-musllinux-${{ matrix.target }}
          path: ./extensions/underthesea_core/dist

  windows:
    name: "Windows x64"
    needs: verify-version
    if: needs.verify-version.outputs.should_release == 'true'
    runs-on: windows-latest  # windows-2025
    steps:
    - uses: actions/checkout@v4
    - name: Remove target-cpu=native for portable binary
      run: Remove-Item -Force -ErrorAction SilentlyContinue .\extensions\underthesea_core\.cargo\config.toml
    - uses: actions/setup-python@v5
      with:
        python-version: 3.x
    - name: Build wheels
      uses: PyO3/maturin-action@v1
      with:
        target: x64
        args: --release --out dist -i 3.10 3.11 3.12 3.13 3.14
        working-directory: ./extensions/underthesea_core
    - name: Upload wheels
      uses: actions/upload-artifact@v4
      with:
        name: wheels-windows-x64
        path: ./extensions/underthesea_core/dist

  macos:
    name: "macOS ${{ matrix.target }}"
    needs: verify-version
    if: needs.verify-version.outputs.should_release == 'true'
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: macos-15-intel  # Intel x64
            target: x86_64
          - runner: macos-15  # Apple Silicon ARM64
            target: aarch64
    steps:
    - uses: actions/checkout@v4
    - name: Remove target-cpu=native for portable binary
      run: rm -f ./extensions/underthesea_core/.cargo/config.toml
    - uses: actions/setup-python@v5
      with:
        python-version: 3.x
    - name: Build wheels
      uses: PyO3/maturin-action@v1
      with:
        target: ${{ matrix.target }}
        args: --release --out dist -i 3.10 3.11 3.12 3.13 3.14
        working-directory: ./extensions/underthesea_core
    - name: Upload wheels
      uses: actions/upload-artifact@v4
      with:
        name: wheels-macos-${{ matrix.target }}
        path: ./extensions/underthesea_core/dist

  release:
    name: "Publish to PyPI"
    runs-on: ubuntu-latest
    needs: [sdist, linux, musllinux, windows, macos]
    permissions:
      id-token: write
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true
      - name: List wheels
        run: ls -la dist/
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_UNDERTHESEA_CORE_API_TOKEN }}
          packages-dir: dist/
          skip-existing: true
