VENV     := .venv
PYTHON   := $(VENV)/bin/python
PIP      := $(VENV)/bin/pip
MKDOCS   := $(VENV)/bin/mkdocs

CARGO_DIR := src

DOC_DIR  := DevDocMD
DOC_FILE := mkdocs.yml
DOC_SITE := DevDoc
DOC_PORT := 8001

DOC_USER_DIR  := UserDocMD
DOC_USER_FILE := mkdocs-user.yml
DOC_USER_SITE := doc
DOC_USER_PORT := 8002

# ── virtualenv ────────────────────────────────────────────────────────────────

$(VENV)/bin/activate:
	python3 -m venv $(VENV)

$(MKDOCS): $(VENV)/bin/activate
	$(PIP) install --quiet --upgrade pip
	$(PIP) install --quiet \
		livereload watchdog \
		mkdocs mkdocs-material \
		mkdocs-mermaid2-plugin \
		mkdocs-bibtex
	$(PIP) install --quiet --upgrade InSilicoSeq

# ── standard targets (names and behaviour follow crate-kit's crate.mk) ───────────
# obiskim is an application workspace: cargo runs on $(CARGO_DIR)/Cargo.toml, default features.

CARGO ?= cargo
CARGO_MANIFEST := $(CARGO_DIR)/Cargo.toml
GREEN  ?= \033[0;32m
YELLOW ?= \033[1;33m
BLUE   ?= \033[1;34m
NC     ?= \033[0m

.DEFAULT_GOAL := build

.PHONY: help build release test check fmt fmt-check clippy ci
## help: Lists the documented targets (this Makefile and the kit's)
help:
	@echo "$(GREEN)Available commands:$(NC)"
	@sed -n "s/^## *\([^:]*\): */\1$$(printf '\t')/p" $(MAKEFILE_LIST) | column -t -s "$$(printf '\t')" | sed -e 's/^/ /'

## build: Release build of the obiskim binary (alias of release)
build: release

## release: Release build of the obiskim binary
release: obiskim

## test: Workspace tests
test:
	$(CARGO) test --workspace --manifest-path $(CARGO_MANIFEST)

## check: Code check (all targets of the workspace)
check:
	$(CARGO) check --workspace --all-targets --manifest-path $(CARGO_MANIFEST)

## fmt: Code formatting
fmt:
	$(CARGO) fmt --all --manifest-path $(CARGO_MANIFEST)

## fmt-check: Formatting check
fmt-check:
	$(CARGO) fmt --all --manifest-path $(CARGO_MANIFEST) -- --check

## clippy: Static analysis (warnings = errors)
clippy:
	$(CARGO) clippy --workspace --all-targets --manifest-path $(CARGO_MANIFEST) -- -D warnings

## ci: Verification pipeline (deps-check fmt-check clippy test)
ci: deps-check fmt-check clippy test
	@echo "$(GREEN)✓ All CI checks passed$(NC)"

# ── obiskim binary ───────────────────────────────────────────────────────────

PROFILE_FILE := profile.pb
PPROF_PORT   := 8081

.PHONY: obiskim
## obiskim: Release build of the obiskim binary
obiskim:
	cargo build --release --manifest-path $(CARGO_DIR)/Cargo.toml --bin obiskim

.PHONY: debug
## debug: Debug build of the obiskim binary
debug:
	cargo build --manifest-path $(CARGO_DIR)/Cargo.toml --bin obiskim

.PHONY: profile-build
profile-build:
	cargo build --release --manifest-path $(CARGO_DIR)/Cargo.toml \
	            --bin obiskim --features obiskim/profiling

.PHONY: profile-view
profile-view:
	go tool pprof -http=127.0.0.1:$(PPROF_PORT) $(PROFILE_FILE)
# ── documentation ─────────────────────────────────────────────────────────────

.PHONY: doc
## doc: Builds the developer documentation (DevDoc/)
doc: $(MKDOCS)
	$(MKDOCS) build -f $(DOC_FILE)

.PHONY: doc-serve
doc-serve: $(MKDOCS)
	$(MKDOCS) serve -f $(DOC_FILE) \
	                --dev-addr=127.0.0.1:$(DOC_PORT) \
	                --livereload
.PHONY: clean-doc
clean-doc:
	rm -rf $(DOC_SITE)/

.PHONY: doc-user
## doc-user: Builds the user documentation (doc/)
doc-user: $(MKDOCS)
	$(MKDOCS) build -f $(DOC_USER_FILE)

.PHONY: doc-user-serve
doc-user-serve: $(MKDOCS)
	$(MKDOCS) serve -f $(DOC_USER_FILE) \
	                --dev-addr=127.0.0.1:$(DOC_USER_PORT) \
	                --livereload

.PHONY: clean-doc-user
clean-doc-user:
	rm -rf $(DOC_USER_SITE)/

# Regenerates the Gitea/Forgejo wiki (a separate repository, checked out next
# to this one) from the user documentation. Only rewrites its working tree:
# committing and pushing it stays a manual step. Not part of `clean`.
# The wiki is flat and entirely generated: its pages (top-level *.md) are deleted first, so that a page
# renamed or removed in the sources does not linger. Only *.md at the top level are touched (.git, .jj kept).
DOC_WIKI_DIR := ../obiskim.wiki

.PHONY: wiki
## wiki: Regenerates the wiki working tree (../obiskim.wiki) from scratch; commit and push stay manual
wiki:
	@command -v pmotransdoc >/dev/null || { echo "pmotransdoc not found in PATH" >&2; exit 1; }
	@test -d $(DOC_WIKI_DIR)/.git || { echo "$(DOC_WIKI_DIR) is not a git checkout of the wiki" >&2; exit 1; }
	find $(DOC_WIKI_DIR) -maxdepth 1 -type f -name '*.md' -delete
	pmotransdoc $(DOC_USER_DIR) $(DOC_WIKI_DIR)

.PHONY: clean
clean: clean-doc clean-doc-user
	rm -rf $(VENV)

# ── release (jj only; the kit's norms for a repository that is not a crate) ───────
# Version: patch bump of the obiskim crate (RELEASE=x.y.z forces it). Tag: v<version> (what release.yml
# triggers on), created and pushed with jj, lightweight: the CI release body is the tagged commit's
# message. Release notes: ReleaseNotes/version-<version>.md (jj release-notes), committed with the release.
# No git command is run. The user runs `make jjrelease`; it never runs by itself.

CARGO_TOML := $(CARGO_DIR)/obiskim/Cargo.toml
RELEASE_NOTES_DIR := ReleaseNotes
API_DIR := api
RELEASE_PROJECT_DESC := obiskim (Rust k-mer genomics tool)
TAG_PREFIX := v
# Backlog of outdated external dependencies: not enforced until TASK-24 of the obi backlog is done.
# The private-registry check (neighbours, lock, duplicates) stays active.
export DEPS_CHECK_EXTERNAL ?= 0
export RELEASE_NOTES_DIR API_DIR RELEASE_PROJECT_DESC TAG_PREFIX

.PHONY: bump-version jjpush jjrelease bookmark-check print-%

## bump-version: Next patch version of the obiskim crate, or fixed RELEASE=x.y.z (refused if the tag exists)
bump-version:
	@jj git fetch
	@current=$$(grep '^version = ' $(CARGO_TOML) | head -n 1 | sed 's/version = "\(.*\)"/\1/'); \
	if [ -n "$(RELEASE)" ]; then \
		new_version="$(RELEASE)"; \
	else \
		major=$$(echo $$current | cut -d. -f1); \
		minor=$$(echo $$current | cut -d. -f2); \
		patch=$$(echo $$current | cut -d. -f3); \
		new_version="$$major.$$minor.$$((patch + 1))"; \
	fi; \
	if [ -n "$$(jj tag list "$(TAG_PREFIX)$$new_version" 2>/dev/null)" ]; then \
		echo "✗ tag $(TAG_PREFIX)$$new_version already exists (use RELEASE=x.y.z)" >&2; exit 1; \
	fi; \
	echo "Version: $$current -> $$new_version"; \
	sed -i.bak "s/^version = \"$$current\"/version = \"$$new_version\"/" $(CARGO_TOML) && \
	rm $(CARGO_TOML).bak

## bookmark-check: Does main track main@origin? Otherwise the push of main would fail (read-only)
bookmark-check:
	@.crate-kit/scripts/bookmark-check.sh

## jjpush: Documents the commit, advances main to it and pushes main (no PR branch), then starts a new commit
jjpush: bookmark-check deps-check
	@echo "$(YELLOW)→ Pushing the commit to main...$(NC)"
	@jj auto-doc
	@.crate-kit/scripts/push-main.sh
	@.crate-kit/scripts/new-commit.sh
	@echo "$(GREEN)✓ main pushed; new working commit created on top of it$(NC)"

## jjrelease: Release (ci, version bump, notes, push of main, tag v<version> pushed; release.yml then builds)
jjrelease: bookmark-check ci bump-version
	@echo "$(BLUE)→ Documenting the undocumented commits...$(NC)"
	@jj auto-doc
	@.crate-kit/scripts/release-notes.sh
	@echo "$(BLUE)→ Documenting the current commit...$(NC)"
	@jj auto-doc
	@echo "$(BLUE)→ Pushing main...$(NC)"
	@.crate-kit/scripts/push-main.sh
	@scripts/jj-tag.sh
	@.crate-kit/scripts/new-commit.sh
	@echo "$(GREEN)✓ Tag pushed: the CI (release.yml) builds and publishes the release$(NC)"

## print-%: Value of a make variable (e.g. make print-REGISTRY)
print-%:
	@echo '$($*)'

# ── crate-kit (registry `obi`, joint development) ─────────────────────────────
# obiskim is an APPLICATION workspace, not a crate. It includes dev-patch.mk (private registry
# definition, local patches to neighbouring crates, deps-check/deps-bump) and jj.mk (jjnew, jjfetch).
# The crate targets of crate.mk (publish, api-*, kit-check: it requires a release.yaml workflow) do NOT
# apply; the release targets above are the application's own, built on the kit's scripts.
# .crate-kit/ is resynchronized from obi-kit (which assembles crate-kit) at every make, before the include.
# Skipped when obi-kit is absent (CI, repository cloned alone): the committed copy is used.
# Included last so that the default goal stays `build`.
KIT_SRC ?= ../obi-kit
# The workspace (its Cargo.toml and its members) lives under src/: dev-patch and deps-check scan it too.
MEMBERS_DIR := $(CARGO_DIR)
$(shell [ -x '$(KIT_SRC)/tools/kit-copy.sh' ] && '$(KIT_SRC)/tools/kit-copy.sh' .crate-kit)

include .crate-kit/dev-patch.mk
include .crate-kit/jj.mk

# Exported after the includes: KIT_LANG comes from org.mk (`?=`), and an earlier `export KIT_LANG` would
# define it empty with GNU make 3.81 (macOS).
export KIT_LANG CARGO_TOML
export PMOJJ_LANG = $(KIT_LANG)
