name: Release Python SDK

on:
  push:
    tags:
      - "sdk-py-v*"
  workflow_dispatch:

permissions:
  contents: read
  id-token: write  # required for PyPI Trusted Publishing

jobs:
  build:
    name: Build wheel + sdist
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - uses: actions/setup-go@v5
        with:
          go-version-file: go.mod
          cache: true

      - uses: actions/setup-node@v4
        with:
          node-version: "20"

      - uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

      - name: Install swag
        run: go install github.com/swaggo/swag/cmd/swag@latest

      - name: Verify tag matches pyproject version
        if: startsWith(github.ref, 'refs/tags/sdk-py-v')
        run: |
          tag_version="${GITHUB_REF_NAME#sdk-py-v}"
          pyproject_version=$(grep -E '^version = ' sdk/python/pyproject.toml | sed -E 's/version = "(.*)"/\1/')
          if [ "$tag_version" != "$pyproject_version" ]; then
            echo "Tag version ($tag_version) does not match pyproject.toml ($pyproject_version)" >&2
            exit 1
          fi

      - name: Generate client + build
        run: make sdk-py-build

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist
          path: sdk/python/dist/

  publish-pypi:
    name: Publish to PyPI
    needs: build
    if: startsWith(github.ref, 'refs/tags/sdk-py-v')
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/marmot-sdk
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/
      - uses: pypa/gh-action-pypi-publish@release/v1
