name: Publish to pypi
on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build-and-publish:
    name: Build and publish
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install Release Tools
        run: |
          python -m pip install --upgrade pip
          pip install python-semantic-release build

      - name: Release and Build
        id: release_step
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # 1. Update version, commit, and tag
          semantic-release version
          
          # 2. Build the package (using the updated version)
          python -m build
          
          # 3. Create the GitHub Release
          semantic-release publish

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_TOKEN }}