.DEFAULT_GOAL := help
SOURCE_DIR = ./src
PY_VERSIONS = 3.10 3.11 3.12 3.13 3.14 3.15
export UV_MANAGED_PYTHON ?= 1

##@ CI/CD
.PHONY: build
build: ## Build
	uv build

.PHONY: test-cov
test-cov: ## Run tests with coverage (both SQLite backends, merged report)
	rm -f .coverage .coverage.*; \
	DISKSTORE_BACKEND=apsw uv run pytest -n auto --cov=diskstore --cov-config=pyproject.toml --cov-append --cov-report=; \
	DISKSTORE_BACKEND=sqlite3 uv run pytest -n auto --cov=diskstore --cov-config=pyproject.toml --cov-append --cov-report=; \
	uv run coverage report

##@ Quality
.PHONY: test-lf
test-lf: ## Run tests in current Python
	uv run pytest --lf -n auto

.PHONY: test-all
test-all: ## Run tests in all supporte Python versions
	for py_v in $(PY_VERSIONS); do \
		DISKSTORE_BACKEND=sqlite3 uv run --isolated -p $$py_v pytest -n auto; \
		DISKSTORE_BACKEND=apsw uv run --isolated -p $$py_v pytest -n auto; \
	done

.PHONY: test
test: ## run tests against both sqlite drivers
	DISKSTORE_BACKEND=sqlite3 uv run pytest -n auto
	DISKSTORE_BACKEND=apsw uv run pytest -n auto

.PHONY: update-python
update-python: ## Reinstall managed Python versions to latest release
	for py_v in $(PY_VERSIONS); do \
		uv python install --reinstall $$py_v ; \
	done
	uv python install --reinstall 3.14t
	uv python install --reinstall 3.15
	uv python install --reinstall 3.15t

.PHONY: test-latest
test-latest: ## Run tests on free-threaded builds, both backends
	PYTHON_GIL=0 DISKSTORE_BACKEND=apsw uv run --isolated -p 3.14t pytest -n auto; \
	PYTHON_GIL=0 DISKSTORE_BACKEND=sqlite3 uv run --isolated -p 3.14t pytest -n auto; \
	PYTHON_GIL=0 DISKSTORE_BACKEND=apsw uv run --pre --isolated -p 3.15t pytest -n auto; \
	PYTHON_GIL=0 DISKSTORE_BACKEND=sqlite3 uv run --pre --isolated -p 3.15t pytest -n auto

.PHONY: check
check: ## Run all checks
	-uvx ty check ${SOURCE_DIR}
	uvx ruff check ${SOURCE_DIR}

.PHONY: ruff-check
ruff-check: ## Lint using ruff
	uvx ruff check ${SOURCE_DIR}

.PHONY: type-check
type-check: ## Type check with
	uvx ty check ${SOURCE_DIR}

.PHONY: format
format: ## Format files using ruff format
	uvx ruff format ${SOURCE_DIR}

.PHONY: bench
bench: ## run benchmarks
	uv run scripts/benchmark_core.py -p 1

.PHONY: bench-kv
bench-kv: ## run benchmarks kv
	uv run scripts/benchmark_kv_store.py

.PHONY: bench-all
bench-all: ## run benchmark (get/set/delete/update)
	uv run scripts/benchmark.py

.PHONY: bench-configs
bench-configs: ## run benchmark (BaseConfig vs StructtypeConfig vs PydanticConfig)
	uv run scripts/benchmark_configs.py

.PHONY: bench-ab
bench-ab: ## run A/B benchmark (apsw vs stdlib sqlite3)
	uv run scripts/bench_ab.py

.PHONY: docs
docs: ## build docs
	uv run --group docs zensical build

##@ Utility
.PHONY: clean
clean: ## Delete all temporary files
	rm -rf .pytest_cache
	rm -rf **/.pytest_cache
	rm -rf .mypy_cache
	rm -rf .ruff_cache
	rm -rf __pycache__
	rm -rf **/__pycache__
	rm -rf build
	rm -rf dist
	rm -f .coverage

.PHONY: install
install: install-uv ## Install virtual environment
	uv sync --frozen

.PHONY: install-uv
install-uv: ## Install uv
	@command -v uv >/dev/null 2>&1 || curl -LsSf https://astral.sh/uv/install.sh | sh

.PHONY: update-uv
update-uv: ## Update uv
	uv self update

.PHONY: update-lock
update-lock: ## Update lockfile
	uv lock --upgrade


.PHONY: help
help:  ## Display this help
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make <target>\033[36m\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
