name: BTP Enterprise Release & Provenance Pipeline

on:
  push:
    tags:
      - 'v4.*'
      - 'v*.*.*'
  workflow_dispatch:

permissions:
  contents: write
  id-token: write

jobs:
  test-matrix:
    name: Regression Test Matrix
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.10', '3.11', '3.12']

    steps:
      - name: Checkout Code
        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 & Frameworks
        run: |
          python -m pip install --upgrade pip
          pip install -e .
          pip install pytest cryptography web3

      - name: Run Full Test Suite
        run: |
          python -m pytest tests/test_universal_model_compatibility.py
          python -m pytest tests/test_v44_chaos_benchmark.py
          python -m pytest tests/test_v43_evm_deployer.py
          python -m pytest tests/test_v42_settlement_gateway.py
          python -m pytest tests/test_v41_framework_adapters.py
          python -m pytest tests/test_cli_milestones.py

  build-and-release:
    name: Build Distributions & Create Release
    needs: test-matrix
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Build Python Distributions
        run: |
          python -m pip install --upgrade pip build twine
          python -m build
          twine check dist/*

      - name: Build NPM Package
        run: |
          cd npm_package
          npm install
          npm pack
          cd ..

      - name: Generate SLSA Provenance Hashes
        run: |
          sha256sum dist/* > dist/SHA256SUMS.txt
          cat dist/SHA256SUMS.txt

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: |
            dist/*
            npm_package/*.tgz
          generate_release_notes: true
