# Sapere standalone dev data-plane (ADR-008). See docker-compose.dev.yml.
COMPOSE := docker compose -f docker-compose.dev.yml
SAPERE_DATABASE_URL ?= postgresql://postgres:postgres@127.0.0.1:5434/sapere

.PHONY: dev-up dev-studio dev-bootstrap dev-down dev-reset eval eval-full

# G5 retrieval-eval harness. `eval` is the reproducible one-command deliverable:
# stands up the standalone PG + schema, then runs the fixture through every arm,
# prints the quality + latency tables, and gates against the committed baseline.
BENCH_OWNER ?= postgresql://postgres:postgres@127.0.0.1:5434/sapere
BENCH_APP_RW ?= postgresql://app_rw:app_rw@127.0.0.1:5434/sapere

eval: dev-up dev-bootstrap ## run the fixture benchmark + gate (prints both tables)
	SAPERE_TEST_OWNER_URL=$(BENCH_OWNER) SAPERE_TEST_APP_RW_URL=$(BENCH_APP_RW) \
	  python -m prodedify_memory.benchmark.cli --corpus fixture --embedder fake --judge scripted --gate

eval-full: ## full-scale run — CORPUS= ADAPTER= LIMIT= INFLATE= JUDGE= REPEATS= pass-through, no gate
	SAPERE_TEST_OWNER_URL=$(BENCH_OWNER) SAPERE_TEST_APP_RW_URL=$(BENCH_APP_RW) \
	  python -m prodedify_memory.benchmark.cli --corpus $(or $(CORPUS),fixture) \
	  --adapter $(or $(ADAPTER),none) --inflate $(or $(INFLATE),0) \
	  --repeats $(or $(REPEATS),20) --judge $(or $(JUDGE),none) --embedder $(or $(EMBEDDER),fake)

dev-up: ## standalone Postgres + pgweb viewer (http://localhost:8081)
	$(COMPOSE) up -d sapere-db pgweb

dev-studio: ## also bring up Supabase Studio (http://localhost:54330)
	$(COMPOSE) --profile studio up -d

dev-bootstrap: ## apply the engine schema (incl. 0007 app_rw grants; needs an installed '.[migrations]' env)
	SAPERE_DATABASE_URL=$(SAPERE_DATABASE_URL) alembic upgrade head
	@echo "bootstrapped head: $$($(COMPOSE) exec -T sapere-db psql -U postgres -d sapere -tAc 'select version_num from alembic_version')"

dev-down: ## stop the stack (keeps data)
	$(COMPOSE) --profile studio down

dev-reset: ## stop + delete the volume (fresh DB on next up)
	$(COMPOSE) --profile studio down -v
