# ============================================================================
# dev-framework Makefile
# ============================================================================
# Entry point for running tests, linting, and validation.
#
# Usage:
#   make test          Run all tests (bats + pytest)
#   make test-bats     Run only bats (bash) tests
#   make test-python   Run only pytest (Python) tests
#   make lint          Run shellcheck + python syntax check
#   make smoke         Quick validation: syntax + compilation + core tests
#   make ci            Full CI pipeline (lint + test)
# ============================================================================

.PHONY: test test-bats test-python lint smoke ci check-deps

BATS := test/bats/bin/bats
BATS_FILES := $(wildcard test/*.bats)
PYTHON_TESTS := scripts/tests

# ── Prerequisites check ────────────────────────────────────────────────────

check-deps:
	@command -v python3 >/dev/null 2>&1 || { echo "ERROR: python3 required"; exit 1; }
	@command -v bash >/dev/null 2>&1 || { echo "ERROR: bash required"; exit 1; }
	@test -x $(BATS) || { echo "ERROR: bats-core not found. Run: git submodule update --init --recursive"; exit 1; }

# ── Test targets ───────────────────────────────────────────────────────────

test: check-deps test-bats test-python
	@echo ""
	@echo "All tests passed."

test-bats: check-deps
	@echo "Running bats tests..."
	@$(BATS) $(BATS_FILES)

test-python: check-deps
	@echo "Running Python tests..."
	@python3 -m pytest $(PYTHON_TESTS) -v --tb=short

# ── Lint targets ───────────────────────────────────────────────────────────

lint:
	@echo "Checking shell syntax..."
	@for f in scripts/*.sh; do bash -n "$$f" || exit 1; done
	@echo "  All shell scripts OK"
	@echo "Checking Python compilation..."
	@for f in .claude/hooks/*.py; do python3 -m py_compile "$$f" || exit 1; done
	@echo "  All Python hooks OK"
	@if command -v shellcheck >/dev/null 2>&1; then \
		echo "Running shellcheck..."; \
		shellcheck scripts/*.sh || true; \
	else \
		echo "  shellcheck not installed (skipping)"; \
	fi

# ── Smoke test (fast validation) ──────────────────────────────────────────

smoke: check-deps lint
	@echo "Running core tests..."
	@$(BATS) test/safety_gate.bats test/framework_config.bats test/template_instantiation.bats
	@echo ""
	@echo "Smoke test passed."

# ── CI target (full pipeline) ─────────────────────────────────────────────

ci: lint test
	@echo ""
	@echo "CI pipeline passed."
