# Makefile for arborist-viz (Merkle Command Center)
# Matches the remarkbox / make_post_sell idiom.

VENV_DIR    = $(shell pwd)/env
DATA_DIR    = $(shell pwd)/data
CONFIG_FILE = development.ini

PYTHON  = $(VENV_DIR)/bin/python
PIP     = $(VENV_DIR)/bin/pip
PSERVE  = $(VENV_DIR)/bin/pserve
PYTEST  = $(VENV_DIR)/bin/pytest
ALEMBIC = $(VENV_DIR)/bin/alembic
INITDB  = $(VENV_DIR)/bin/arborist_viz_init_db

SIX_REPO  = ssh://git@git.unturf.com:2222/gumyum/six.js.git
SIX_CLONE = $(shell pwd)/build/six.js
SIX_DST   = arborist_viz/static/six

.PHONY: all clean install install-dev test serve six commit-hash version help \
        arborist seed demo

# `make all` = production install + serve.
# `make demo` = full demo pipeline (deps + arborist + seed shard + serve).
all: install-dev six init-db serve

help:
	@echo "arborist-viz make targets:"
	@echo "  make install      install package (production deps only)"
	@echo "  make install-dev  install package with dev + test extras"
	@echo "  make six          clone six.js fork from gumyum + vendor bundles"
	@echo "  make init-db      create dashboard metadata SQLite + run migrations"
	@echo "  make serve        run waitress on :6543 with development.ini"
	@echo "  make test         run pytest"
	@echo "  make clean        nuke venv, build artifacts, sqlite"
	@echo "  make commit-hash  write commit-hash.txt for /version endpoint"

$(VENV_DIR)/bin/activate:
	@echo "Creating virtual environment in $(VENV_DIR)..."
	python3 -m venv $(VENV_DIR)
	$(PIP) install --upgrade pip 'setuptools<81' wheel

venv: $(VENV_DIR)/bin/activate

install: venv
	$(PIP) install -e .

install-dev: venv arborist
	$(PIP) install -e '.[dev,test]'

# arborist lives in a sibling git repo; install it editable into our venv
# so arborist_viz/storage.py can import arborist.embed + arborist.merkle.
# If arborist is unavailable, VIZ falls back to fixture mode automatically.
ARBORIST_SRC = $(HOME)/git/arborist
arborist: venv
	@if [ -d "$(ARBORIST_SRC)" ]; then \
	  echo "Installing arborist from $(ARBORIST_SRC)..."; \
	  $(PIP) install -e "$(ARBORIST_SRC)" >/dev/null; \
	  echo "  -> arborist installed (editable)"; \
	else \
	  echo "WARNING: $(ARBORIST_SRC) not found — VIZ will run in fixture mode."; \
	fi

# Create a small demo shard so dropping a hash actually visualizes.
SEED_DB = $(DATA_DIR)/demo_arborist.db
seed: install-dev
	@mkdir -p $(DATA_DIR)
	$(VENV_DIR)/bin/arborist_viz_seed_demo $(SEED_DB)

demo: install-dev six seed init-db
	@echo ""
	@echo "================================================================"
	@echo "  arborist-viz demo ready."
	@echo ""
	@echo "  Seed shard:  $(SEED_DB)"
	@echo "  Sample hashes printed above ^^"
	@echo ""
	@echo "  Edit development.ini -> arborist.shards = $(SEED_DB)"
	@echo "  Or just: make serve-demo"
	@echo "================================================================"
	@echo ""

serve-demo: install-dev six seed
	ARBORIST_VIZ_SHARDS=$(SEED_DB) $(PSERVE) --reload $(CONFIG_FILE)

# Serve against the real arborist corpus on this host. We glob every
# *.db file in the shards directory — arborist owns its own internal file
# layout (document shards, provenance shard, future kinds added by
# recursive falsification) and arborist.read tolerates any shard whose
# tables don't match a given query. VIZ never names individual files.
ARBORIST_SHARDS_DIR ?= $(HOME)/.arborist/shards
# Also mount the local crawl db (web.db) so the VIZ can resolve and
# display the chunk content of locally-crawled sources (a context root's
# leaves reference docs that live there, not in the main shards). It is a
# single file, well under SQLite's 10-attach cap; arborist.read tolerates
# a missing file, so this is a no-op when nothing has been crawled.
ARBORIST_CRAWL_DB ?= $(HOME)/.arborist/crawl/web.db
ARBORIST_REAL_SHARDS = $(shell { ls $(ARBORIST_SHARDS_DIR)/*.db 2>/dev/null; [ -f $(ARBORIST_CRAWL_DB) ] && echo $(ARBORIST_CRAWL_DB); } | paste -sd,)
serve-arborist: install-dev six
	@echo "Mounting shards from $(ARBORIST_SHARDS_DIR) ..."
	@echo "  -> $(ARBORIST_REAL_SHARDS)"
	ARBORIST_VIZ_SHARDS=$(ARBORIST_REAL_SHARDS) $(PSERVE) --reload $(CONFIG_FILE)

six:
	@echo "Vendoring six.js bundles from $(SIX_REPO) -> $(SIX_DST)..."
	@if [ -d "$(SIX_CLONE)/.git" ]; then \
	  echo "  refreshing existing clone..."; \
	  git -C "$(SIX_CLONE)" fetch --depth 1 --quiet origin main; \
	  git -C "$(SIX_CLONE)" reset --hard --quiet FETCH_HEAD; \
	else \
	  echo "  cloning $(SIX_REPO) (shallow, build/ only)..."; \
	  rm -rf "$(SIX_CLONE)"; \
	  mkdir -p "$(dir $(SIX_CLONE))"; \
	  git clone --depth 1 --filter=blob:none --no-checkout --single-branch \
	    --branch main --no-tags "$(SIX_REPO)" "$(SIX_CLONE)"; \
	  git -C "$(SIX_CLONE)" sparse-checkout set --no-cone build; \
	  git -C "$(SIX_CLONE)" checkout --quiet main; \
	fi
	@mkdir -p $(SIX_DST)
	@# The fork ships vanilla three.js build output (build/three.*.js). The
	@# consumer imports six.module.js, which is an alias of three.module.js;
	@# three.* entry files import './three.core.js', so both name-sets must
	@# sit together in /static/six/ or the ES-module import fails to resolve.
	@cp $(SIX_CLONE)/build/three*.js $(SIX_DST)/
	@for f in $(SIX_CLONE)/build/three*.js; do \
	  b=$$(basename "$$f"); \
	  cp "$$f" "$(SIX_DST)/$$(echo "$$b" | sed 's/^three/six/')"; \
	done
	@echo "  vendored $$(ls $(SIX_DST)/*.js | wc -l) bundle files into $(SIX_DST)."

commit-hash:
	@git rev-parse --short HEAD > commit-hash.txt 2>/dev/null || echo "0" > commit-hash.txt
	@echo "commit-hash.txt -> $$(cat commit-hash.txt)"

init-db: install-dev
	@mkdir -p $(DATA_DIR)
	$(INITDB) $(CONFIG_FILE)

serve: install-dev
	$(PSERVE) --reload $(CONFIG_FILE)

test: install-dev
	$(PYTEST) -q

version:
	@curl -s http://127.0.0.1:6543/version || echo "server not running"

clean:
	rm -rf $(VENV_DIR) build dist *.egg-info
	rm -f data/arborist_viz.sqlite
	find . -type d -name __pycache__ -exec rm -rf {} +
