# Composite action: shared quality contract used by both ci-cd.yml and release.yml.
# Encapsulates the full uv + just bootstrap so callers stay DRY.
# Runs `just quality` which includes: lint, typecheck, tests with coverage,
# coverage no-regression gate, dependency audit, security scan (bandit),
# dead-code detection (vulture), complexity gate (xenon), and docs drift check.
name: Run Quality Contract
description: Bootstrap uv/Python and run the shared local quality contract.

inputs:
  python-version:
    description: Python version to install and use for the quality contract.
    required: false
    default: "3.13"
  run-diff-coverage:
    description: Whether to run diff coverage after the main quality contract.
    required: false
    default: "false"

runs:
  using: composite
  steps:
    - name: Install uv
      uses: astral-sh/setup-uv@v4
      with:
        enable-cache: true

    - name: Install just
      uses: taiki-e/install-action@just

    - name: Set up Python ${{ inputs.python-version }}
      shell: bash
      run: uv python install "${{ inputs.python-version }}"

    - name: Verify Python version
      shell: bash
      run: uv run --python "${{ inputs.python-version }}" python -c "import sys; print(sys.version)"

    - name: Install dependencies
      shell: bash
      run: uv sync --group dev --python "${{ inputs.python-version }}"

    # `just quality` = lint + typecheck + test (with branch-coverage ≥85%) +
    # coverage-no-regression + deps + audit + docs-check + slurm-check +
    # dead-code + complexity + complexity-gate + security.
    - name: Run local quality contract
      shell: bash
      run: PYTHON_VERSION="${{ inputs.python-version }}" just quality

    # diff-coverage compares coverage.xml against origin/main and requires ≥80%
    # coverage on *changed lines only*. Only meaningful on PRs — on main pushes
    # there is no diff to gate against.
    - name: Diff coverage gate
      if: ${{ inputs.run-diff-coverage == 'true' }}
      shell: bash
      run: PYTHON_VERSION="${{ inputs.python-version }}" just diff-coverage
