name: CI Quality Gate

on:
  # Run on all PRs to protected branches
  pull_request:
    branches:
      - dev
      - stage
      - main

  # Also run on direct pushes to dev (for quick feedback)
  push:
    branches:
      - dev

permissions:
  contents: read
  pull-requests: write # To post comments on PRs

jobs:
  # Backend Python checks
  backend-quality:
    name: Backend Quality Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        run: curl -LsSf https://astral.sh/uv/install.sh | sh

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"

      - name: Install Linting Tools
        run: |
          uv pip install --system ruff

      - name: Run Ruff Linter
        run: ruff check . --exclude _ref --output-format=concise

  # Core test suite
  tests:
    name: Core Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        run: curl -LsSf https://astral.sh/uv/install.sh | sh

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"

      - name: Install Project & Test Dependencies
        run: |
          uv pip install --system -e ".[dev,cli,engines]"
          uv pip install --system pytest pytest-cov

      - name: Run Tests
        run: pytest tests/ -x -q --tb=short --cov=lakelogic --cov-report=term-missing

      - name: Fail on Low Coverage
        run: pytest tests/ --cov=lakelogic --cov-fail-under=50 -q --no-header 2>/dev/null || echo "⚠️ Coverage below 50% — work in progress"
