name: Publish to PyPI

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version to publish (must match pyproject.toml)"
        required: true
        type: string

jobs:
  publish:
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      contents: read
      id-token: write

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install build dependencies
        run: |
          python -m pip install --upgrade pip
          pip install build

      - name: Verify version matches
        run: |
          VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])")
          if [ "$VERSION" != "${{ inputs.version }}" ]; then
            echo "Error: Input version (${{ inputs.version }}) does not match pyproject.toml version ($VERSION)"
            exit 1
          fi
          echo "Version verified: $VERSION"

      - name: Build package
        run: python -m build

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