# Publish to PyPI using Trusted Publishing (OIDC) - No API tokens needed!
# https://docs.pypi.org/trusted-publishers/

name: Publish to PyPI

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      publish_target:
        description: 'Publish target'
        required: true
        default: 'testpypi'
        type: choice
        options:
          - testpypi
          - pypi

jobs:
  build:
    name: Build distribution 📦
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - 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
        run: twine check dist/*
      
      - name: Upload distribution artifacts
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

  test:
    name: Test package 🧪
    runs-on: ubuntu-latest
    needs: build
    strategy:
      matrix:
        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      
      - name: Download distribution
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/
      
      - name: Install package
        run: pip install dist/*.whl
      
      - name: Install test dependencies
        run: pip install pytest pytest-asyncio pytest-cov
      
      - name: Run tests
        run: pytest tests/ -v --cov=amb_core --cov-report=xml
      
      - name: Upload coverage to Codecov
        if: matrix.python-version == '3.11'
        uses: codecov/codecov-action@v4
        with:
          file: ./coverage.xml
          fail_ci_if_error: false

  publish-testpypi:
    name: Publish to TestPyPI 🧪
    if: github.event_name == 'workflow_dispatch' && github.event.inputs.publish_target == 'testpypi'
    needs: [build, test]
    runs-on: ubuntu-latest
    environment:
      name: testpypi
      url: https://test.pypi.org/p/amb-core
    permissions:
      id-token: write  # Required for OIDC trusted publishing
    steps:
      - name: Download distribution
        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 📦
    if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish_target == 'pypi')
    needs: [build, test]
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/amb-core
    permissions:
      id-token: write  # Required for OIDC trusted publishing
    steps:
      - name: Download distribution
        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:
    name: Upload to GitHub Release 📎
    needs: publish-pypi
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    permissions:
      contents: write
    steps:
      - name: Download distribution
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/
      
      - name: Upload to GitHub Release
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: |
          gh release upload '${{ github.ref_name }}' dist/** --repo '${{ github.repository }}'
