name: Release underthesea_core to crates.io

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

env:
  CARGO_TERM_COLOR: always

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 crates.io
        id: check
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "User-Agent: underthesea-release-ci" "https://crates.io/api/v1/crates/underthesea_core/$VERSION")
          if [ "$STATUS" = "200" ]; then
            echo "underthesea_core $VERSION already on crates.io; nothing to release."
            echo "should_release=false" >> "$GITHUB_OUTPUT"
          else
            echo "underthesea_core $VERSION not yet on crates.io; will release."
            echo "should_release=true" >> "$GITHUB_OUTPUT"
          fi

  publish:
    name: Publish to crates.io
    needs: verify-version
    if: needs.verify-version.outputs.should_release == 'true'
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./extensions/underthesea_core
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Dry run
        run: cargo publish --dry-run
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Publish to crates.io
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
