# RUN prefixes every command: empty, they run in the activated
# environment; or name a runner, e.g. `RUN="conda run -n otaku"`.
RUN ?=

.PHONY: lint format typecheck tests scenarios regressions integrations mock-model share stable lines

lint:
	$(RUN) ruff check otaku/ tests/ scenarios/

format:
	$(RUN) ruff format otaku/ tests/ scenarios/

typecheck:
	$(RUN) mypy

tests:
	$(RUN) pytest

# Every scenario but the `slow` ones, which only this selector leaves out,
# and the `integration` ones, which ask the real services.
scenarios:
	$(RUN) pytest scenarios/ -m "not slow and not integration"

# The terminal's screen played turn by turn against its own echo
# (scenarios/cli/test_screen.py): minutes, so left out of `scenarios`.
regressions:
	$(RUN) pytest scenarios/ -m slow

# The integrations against the real services they ask (scenarios/integrations):
# the card sites, web search under the keys set, the support desk.
integrations:
	$(RUN) pytest scenarios/ -m integration

# A mock llama.cpp on llama.cpp's own port, for looking at the transcript
# by hand: its lines' leading digits choose the blocks (scripts/mock_model.py).
# PORT=… moves it, PACE=… sets the seconds between streamed pieces. It runs
# on the project's conda env's own interpreter, not through `conda run`, so
# Ctrl+C reaches it alone and it ends quietly.
MOCK_MODEL_PYTHON ?= $(shell conda run -n otaku python -c 'import sys; print(sys.executable)')
mock-model:
	@$(MOCK_MODEL_PYTHON) scripts/mock_model.py $(if $(PORT),--port $(PORT)) $(if $(PACE),--pace $(PACE))

# The working tree as a wheel, served on this machine's network until
# Ctrl+C, for another machine to install with the line it prints. Built
# into build/share, never dist/ (the releases'). SHARE_PORT=… moves it.
SHARE_PORT ?= 8765
share:
	@rm -rf build/share
	@uv build --wheel --out-dir build/share
	@address=$$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || hostname -I 2>/dev/null | cut -d' ' -f1); \
	test -n "$$address" || { echo "No network address found."; exit 1; }; \
	wheel=$$(basename build/share/*.whl); \
	echo; echo "  uv tool install --reinstall \"otaku @ http://$$address:$(SHARE_PORT)/$$wheel\""; echo; \
	python3 -m http.server $(SHARE_PORT) --bind $$address --directory build/share

# Announce a release to every installed otaku's update check: the `stable`
# tag, at which they read otaku/__init__.py (`otaku.update`), moved to
# vVERSION — a release already tagged on GitHub. Force-pushes that one tag
# and nothing else; GitHub may serve the previous file for 5 minutes.
stable:
	@echo "$(VERSION)" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$$' || { echo "Usage: make stable VERSION=X.Y.Z"; exit 1; }
	@git ls-remote --exit-code --tags origin "refs/tags/v$(VERSION)" >/dev/null || { echo "v$(VERSION) is not on GitHub: release it first."; exit 1; }
	@git fetch --quiet origin tag "v$(VERSION)"
	git tag -f stable "v$(VERSION)^{}"
	git push -f origin refs/tags/stable
	@folder=$$(mktemp -d) && echo "$(VERSION)" > "$$folder/stable" && gh release upload update "$$folder/stable" -R enclavum/otaku --clobber && rm -r "$$folder"
	@echo "Clients now read: $$(curl -fsS https://raw.githubusercontent.com/enclavum/otaku/stable/otaku/__init__.py | grep '^__version__')"

# Lines per language under otaku/, and the code lines among them — neither
# blank nor comment, Python's docstrings and lone strings counted as comment
# (scripts/count_lines.py). Standard library only: any python3 runs it.
lines:
	@python3 scripts/count_lines.py
