name: Publish to PyPI

on:
  push:
    tags:
      - "v[0-9]*"   # triggers on tags like v0.2.0

permissions:
  contents: write  # needed to create GitHub Release

jobs:
  ci:
    name: CI gate
    uses: ./.github/workflows/ci.yml

  build:
    name: Build distribution
    needs: ci
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Build package
        run: uv build

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

  release:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ github.token }}
        run: gh release create "$GITHUB_REF_NAME" --generate-notes

  publish:
    name: Publish to PyPI
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: pypi
      # Update the URL below to match your PyPI package name
      url: https://pypi.org/p/az-scout-plugin-odcr-coverage
    permissions:
      id-token: write  # trusted publishing (OIDC)
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/

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