name: Publish to PyPI

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      dry_run:
        description: 'Dry run (build but do not publish)'
        required: false
        default: false
        type: boolean

permissions:
  contents: read
  id-token: write  # Required for trusted publishing

jobs:
  quality:
    name: Quality Checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Set up Python
        run: uv python install 3.12

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

      - name: Run ruff check
        run: uv run ruff check .

      - name: Run ruff format check
        run: uv run ruff format --check .

      - name: Run tests
        run: uv run pytest -m "not e2e and not network and not slow"

  build:
    name: Build Package
    needs: quality
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Set up Python
        run: uv python install 3.12

      - name: Build package
        run: uv build

      - name: Verify package
        run: |
          uv venv .venv
          uv pip install dist/*.whl
          .venv/bin/python -c "import agr; print(f'Built version: {agr.__version__}')"

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

  publish:
    name: Publish to PyPI
    needs: build
    runs-on: ubuntu-latest
    if: github.event_name == 'push' || !inputs.dry_run
    environment:
      name: pypi
      url: https://pypi.org/project/agr/
    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
        # Uses trusted publishing (OIDC) - no API token needed
        # Configure at: https://pypi.org/manage/project/agr/settings/publishing/
