name: Publish Python Package

on:
  push:
    tags:
      - "**"

jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.inspect_package.outputs.version }}
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v4
        with:
          version: "0.4.12"
          enable-cache: true

      - name: Set up Python
        run: uv python install 3.12

      - name: Build package
        run: uv build

      - name: Inspect package version
        id: inspect_package
        run: |
          version=$(uvx hatchling version)
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Upload package
        uses: actions/upload-artifact@v4
        with:
          name: package-distributions
          path: dist/

  pypi-publish:
    runs-on: ubuntu-latest
    needs: build

    permissions:
      id-token: write

    environment:
      name: pypi
      url: https://pypi.org/project/python-multipart/${{ needs.build.outputs.version }}

    steps:
    - name: Download package
      uses: actions/download-artifact@v4
      with:
        name: package-distributions
        path: dist/

    - name: Publish distribution 📦 to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
