name: Publish

on:
  release:
    types: [published]

jobs:
  publish:
    name: Publish to PyPI
    # Only run on releases starting with 'v'
    if: github.event_name == 'release' && startsWith(github.event.release.tag_name, 'v')
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      contents: read

    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

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

    - name: Install uv
      uses: astral-sh/setup-uv@v4

    - name: Install dependencies
      run: uv sync --all-extras --dev

    - name: Create version fallback
      run: echo '__version__ = "0.0.0.dev0"' > mumla/app/_version.py

    - name: Run tests
      env:
        MOORCHEH_API_KEY: test-api-key
      run: uv run pytest tests/ -v --tb=short

    - name: Build package
      run: uv run python -m build

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