LIBKVCS_SO ?= ../../kvcs-efc/build/libkvcs.so

# CI images based on AliOS 7 ship system python3 = 3.6, but the kvcs-sdk
# requires Python >= 3.9 (pyproject.toml).  The CI image installs multiple
# Python versions via uv (3.9, 3.10, 3.11, 3.12 at /usr/local/bin/python3.x).
# Prefer python3.9 — the minimum supported version — to catch compatibility
# regressions early.  Fall back to python3 for local dev.
PYTHON ?= $(shell command -v python3.9 >/dev/null 2>&1 && echo python3.9 || echo python3)
PIP    ?= $(PYTHON) -m pip

# Mock library paths
MOCK_SO ?= $(abspath ../../kvcs-efc/build/libkvcsmock.so)

.PHONY: unittest coverage wheel clean

wheel: kvcs/libkvcs.so ## Build Python wheel with bundled libkvcs.so
	$(PIP) wheel --no-deps -w dist .

kvcs/libkvcs.so: $(LIBKVCS_SO)
	cp $< $@

unittest: ## Run ALL unit tests with mock library
	KVCS_LIB_PATH=$(MOCK_SO) PYTHONPATH=. $(PYTHON) -m pytest tests/unittest/ -v

coverage: ## Run ALL unit tests with mock library, produce cobertura + junit
	KVCS_LIB_PATH=$(MOCK_SO) PYTHONPATH=. \
		$(PYTHON) -m coverage run --branch --source=kvcs -m pytest tests/unittest/ --junitxml=report.xml
	$(PYTHON) -m coverage xml -o coverage.xml

clean: ## Remove build artifacts
	rm -rf dist/ build/ wheelhouse/ kvcs.egg-info/ kvcs/libkvcs.so
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true

help: ## Show this help
	@grep -E '^[a-z].*:.*##' $(MAKEFILE_LIST) | sed 's/:.*## /\t/' | sort
