name: Release underthesea

on:
  push:
    tags:
      - 'underthesea-v*'  # Trigger on tags like underthesea-v9.2.5

jobs:
  verify-version:
    name: Verify Version
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Verify tag matches pyproject.toml version
        run: |
          TAG_VERSION="${GITHUB_REF#refs/tags/underthesea-v}"
          PYPROJECT_VERSION=$(grep '^version = ' pyproject.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
          echo "Tag version: $TAG_VERSION"
          echo "pyproject.toml version: $PYPROJECT_VERSION"
          if [ "$TAG_VERSION" != "$PYPROJECT_VERSION" ]; then
            echo "ERROR: Tag version ($TAG_VERSION) does not match pyproject.toml version ($PYPROJECT_VERSION)"
            exit 1
          fi
          echo "✓ Versions match"

  build_and_publish:
    name: Build and Publish to PyPI
    needs: verify-version
    permissions:
      id-token: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Install pypa/build
        run: python -m pip install build --user
      - name: Build a binary wheel and a source tarball
        run: python -m build --sdist --wheel --outdir dist/
      - name: Publish distribution to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

  version-docs:
    name: Create Docs Version Snapshot
    needs: build_and_publish
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: main

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        working-directory: docs
        run: npm install

      - name: Extract version from tag
        id: version
        run: |
          TAG_VERSION="${GITHUB_REF#refs/tags/underthesea-v}"
          echo "patch=$TAG_VERSION" >> "$GITHUB_OUTPUT"

      - name: Create docs version snapshot
        working-directory: docs
        run: npx docusaurus docs:version ${{ steps.version.outputs.patch }}

      - name: Commit and push versioned docs
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add docs/versions.json docs/versioned_docs/ docs/versioned_sidebars/
          git commit -m "docs: Add version ${{ steps.version.outputs.patch }} snapshot"
          git push
