# GitHub Actions workflow for publishing ATR to PyPI
# Uses Trusted Publishing (OIDC) for secure, tokenless authentication
# Docs: https://docs.pypi.org/trusted-publishers/

name: Publish to PyPI

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      test_pypi:
        description: 'Publish to TestPyPI instead of PyPI'
        required: false
        default: 'false'
        type: boolean

permissions:
  contents: read

jobs:
  build:
    name: Build distribution
    runs-on: ubuntu-latest
    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 build dependencies
        run: |
          python -m pip install --upgrade pip
          pip install build twine

      - name: Build package
        run: python -m build

      - name: Check package with twine
        run: twine check dist/*

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

  test:
    name: Run tests
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.9", "3.10", "3.11", "3.12"]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e ".[dev]"

      - name: Run tests with pytest
        run: pytest --cov=atr --cov-report=xml

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        with:
          file: ./coverage.xml
          fail_ci_if_error: false

  publish-testpypi:
    name: Publish to TestPyPI
    needs: [build, test]
    if: github.event_name == 'workflow_dispatch' && github.event.inputs.test_pypi == 'true'
    runs-on: ubuntu-latest
    environment:
      name: testpypi
      url: https://test.pypi.org/p/agent-tool-registry
    permissions:
      id-token: write  # Required for OIDC trusted publishing
    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

      - name: Publish to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          repository-url: https://test.pypi.org/legacy/

  publish-pypi:
    name: Publish to PyPI
    needs: [build, test]
    if: github.event_name == 'release' && github.event.action == 'published'
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/agent-tool-registry
    permissions:
      id-token: write  # Required for OIDC trusted publishing
    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

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

  github-release-assets:
    name: Upload assets to GitHub Release
    needs: publish-pypi
    runs-on: ubuntu-latest
    permissions:
      contents: write  # Required to upload release assets
    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

      - name: Upload to GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          files: dist/*
