name: Publish PyPI package

on:
  push:
    tags:
      - "v*"

jobs:
  publish:
    runs-on: ubuntu-latest
    env:
      TWINE_USERNAME: __token__
      TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Extract version from tag
        run: |
          echo "VERSION=$(echo $GITHUB_REF_NAME | sed 's/^v//')" >> $GITHUB_ENV

      - name: Update version in pyproject.toml
        run: |
          cd packages/sdk-python
          sed -i "s/^version = .*/version = \"$VERSION\"/" pyproject.toml

      - name: Build and publish to PyPI
        run: |
          cd packages/sdk-python
          python3 -m venv .venv
          . .venv/bin/activate
          pip install build twine
          python -m build
          twine upload dist/*
