# cognethics Python SDK — local CI / dryrun targets.
#
# These targets let developers validate publish-readiness WITHOUT requiring
# GitHub Actions billing or a PYPI_TOKEN. Run from /data/a7/clients/python.
#
# Usage:
#   make test            # run pytest
#   make lint            # run ruff (check + format-check)
#   make classify        # classify proposed bump magnitude (patch/minor/major)
#   make bump            # dry-run version bump (does NOT modify files)
#   make publish-dryrun  # full pipeline EXCEPT the actual `twine upload`
#
# The Python interpreter is taken from $(PYTHON), defaulting to the Cognethics
# backend venv on this machine (which already has pytest+respx+ruff installed).
PYTHON ?= /data/a7/backend/venv/bin/python
SDK_ROOT := $(CURDIR)
GENERATED_NEW := $(SDK_ROOT)/cognethics/generated
GENERATED_OLD := /tmp/old_cognethics_generated
REPO_ROOT := /data/a7

.PHONY: help test lint classify bump publish-dryrun build clean

help:
	@echo "Targets:"
	@echo "  test            Run the pytest suite (cognethics/tests + tests/)."
	@echo "  lint            Run ruff check + ruff format --check."
	@echo "  classify        Classify the codegen diff vs. \$$GENERATED_OLD."
	@echo "  bump            Dry-run a patch-level version bump."
	@echo "  build           Build sdist + wheel into dist/."
	@echo "  publish-dryrun  Run test + lint + bump + build (NO twine upload)."

test:
	$(PYTHON) -m pytest cognethics/tests/ tests/ -v

lint:
	@if $(PYTHON) -c "import ruff" 2>/dev/null; then \
		echo "ruff via Python module"; \
	fi
	@if command -v ruff >/dev/null 2>&1; then \
		ruff check cognethics/ scripts/; \
		ruff format --check cognethics/ scripts/; \
	else \
		echo "ruff not on PATH — install via 'pip install ruff' or use 'pip install -e .[dev]'"; \
		exit 1; \
	fi

classify:
	@if [ ! -d "$(GENERATED_OLD)" ]; then \
		echo "GENERATED_OLD ($(GENERATED_OLD)) does not exist — copy the previous codegen tree there first."; \
		echo "Skipping classify; treating as 'patch' for dryrun purposes."; \
		echo patch; \
	else \
		$(PYTHON) scripts/classify_bump.py \
			--old "$(GENERATED_OLD)" \
			--new "$(GENERATED_NEW)" \
			--repo-root "$(REPO_ROOT)"; \
		echo; \
	fi

bump:
	$(PYTHON) scripts/bump_version.py --dry-run
	@echo

build:
	$(PYTHON) -m build --sdist --wheel --outdir dist/ 2>/dev/null || \
		(echo "Falling back: pip install build && retry" && \
		 $(PYTHON) -m pip install --quiet build && \
		 $(PYTHON) -m build --sdist --wheel --outdir dist/)

publish-dryrun: test bump build
	@echo
	@echo "==> publish-dryrun complete."
	@echo "    Tests passed, bump computed (no files written), wheel + sdist in dist/."
	@echo "    To actually publish:"
	@echo "      1. python scripts/bump_version.py --level <patch|minor|major>"
	@echo "      2. git commit -am 'chore(sdk): bump version'"
	@echo "      3. python -m build && twine upload dist/*"

clean:
	rm -rf dist/ build/ *.egg-info cognethics/__pycache__
