# Publishes arksim to PyPI.
#
# Triggered by pushing a version tag (e.g. v1.2.3).

name: Publish to PyPI
run-name: Publish ${{ github.ref_name }} to PyPI

on:
  push:
    tags: ["v*"]

jobs:
  # ---------------------------------------------------------------------------
  # 1. Build wheel and source distribution.
  # ---------------------------------------------------------------------------
  build:
    name: Build distribution
    runs-on: ubuntu-latest

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

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

      - name: Build wheel and sdist
        run: |
          pip install build
          python -m build

      - name: Verify distribution
        run: |
          pip install twine
          twine check --strict dist/*

      - name: Upload dist artifact
        uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/
          if-no-files-found: error

  # ---------------------------------------------------------------------------
  # 2. Create GitHub Release with changelog and built dist files.
  # ---------------------------------------------------------------------------
  create-release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    needs: build
    permissions:
      contents: write

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

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

      - name: Extract changelog for version
        id: changelog
        run: |
          version="${{ github.ref_name }}"
          # Strip leading "v" to match CHANGELOG.md section headers (e.g. v1.2.3 → 1.2.3)
          version="${version#v}"
          # Extract the block between this version's heading and the next one.
          body=$(awk "/^## \[${version}\]/{found=1; next} found && /^## \[/{exit} found{print}" CHANGELOG.md)
          # Pass the multi-line body to subsequent steps via $GITHUB_OUTPUT.
          {
            echo "body<<EOF"
            echo "$body"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          name: ${{ github.ref_name }}
          body: ${{ steps.changelog.outputs.body }}
          files: dist/*
          # Mark as pre-release when the version contains "rc".
          prerelease: ${{ contains(github.ref_name, 'rc') }}

  # ---------------------------------------------------------------------------
  # 3. Publish to PyPI via OIDC trusted publisher
  # ---------------------------------------------------------------------------
  publish-pypi:
    name: Publish to PyPI
    runs-on: ubuntu-latest
    needs: [build, create-release]
    environment: pypi
    permissions:
      id-token: write

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

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