name: Python Package and Tests

on:
  push:
    branches:
      - main
      - develop
  pull_request:
    branches:
      - main
      - develop

jobs:
  test:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    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 }}
          cache: 'pip'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e .
          pip install -r requirements-dev.txt

      - name: Lint with flake8 (if available)
        run: |
          pip install flake8 || true
          # Stop the build if there are Python syntax errors or undefined names
          flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude=.venv,venv,build,dist || true
          # Exit-zero treats all errors as warnings
          flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude=.venv,venv,build,dist || true

      - name: Test with pytest
        run: |
          pytest iatp/tests/ -v --tb=short

      - name: Test CLI installation
        run: |
          iatp --version
          iatp --help

      - name: Test CLI verify with valid manifests
        run: |
          iatp verify examples/manifests/secure_bank.json
          iatp verify examples/manifests/standard_agent.json

      - name: Test CLI verify with invalid manifest
        run: |
          # This should fail - untrusted agent with no reversibility
          if iatp verify examples/manifests/untrusted_honeypot.json; then
            echo "ERROR: Expected validation to fail for untrusted manifest"
            exit 1
          else
            echo "✓ Validation correctly failed for untrusted manifest"
          fi

  build:
    runs-on: ubuntu-latest
    needs: test
    permissions:
      contents: read

    steps:
      - name: Checkout repository
        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 artifacts
        uses: actions/upload-artifact@v4.5.0
        with:
          name: python-package-distributions
          path: dist/

  publish:
    runs-on: ubuntu-latest
    needs: build
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    permissions:
      id-token: write  # For trusted publishing

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4.1.8
        with:
          name: python-package-distributions
          path: dist/

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