.PHONY: kind tilt-up tilt-down seed seed-fetch seed-push evals test \
        compose-up compose-down compose-clean compose-seed compose-evals \
        docs-graphs

COMPOSE := docker compose -f ctrl/docker-compose.yml
KCTX    := --context kind-nvi
KNS     := -n nvi

# ── kind + Tilt (primary path) ─────────────────────────────

kind:
	bash ctrl/kind-config.sh

tilt-up:
	cd ctrl && tilt up $(KCTX)

tilt-down:
	cd ctrl && tilt down $(KCTX)

seed-fetch:
	bash ctrl/seed/download.sh

# Copy the downloaded SQLite into the api pod. The file is excluded from the
# docker build context (.dockerignore) to keep the image small, so we push
# it explicitly the first time.
seed-push:
	@POD=$$(kubectl $(KCTX) $(KNS) get pod -l app=api -o jsonpath='{.items[0].metadata.name}'); \
	echo "pushing financial.sqlite → $$POD"; \
	kubectl $(KCTX) $(KNS) exec $$POD -- mkdir -p /app/seed/data; \
	kubectl $(KCTX) $(KNS) cp ctrl/seed/data/financial.sqlite $$POD:/app/seed/data/financial.sqlite

seed: seed-fetch seed-push
	kubectl $(KCTX) $(KNS) exec deploy/api -- uv run python -m seed.load_bird

evals:
	kubectl $(KCTX) $(KNS) exec deploy/api -- uv run python -m api.evals.run_evals

# Unit tests run on the host venv (no postgres/llm calls needed).
# Ensures dev extras are installed first; uv is idempotent on no-ops.
test:
	uv sync --extra dev
	uv run pytest -q

# ── docker-compose (alternative path) ──────────────────────

compose-up:
	$(COMPOSE) up -d --build

compose-down:
	$(COMPOSE) down

compose-clean:
	$(COMPOSE) down -v

compose-seed: seed-fetch
	$(COMPOSE) exec api uv run python -m seed.load_bird

compose-evals:
	$(COMPOSE) exec api uv run python -m api.evals.run_evals

# ── docs ───────────────────────────────────────────────────

docs-graphs:
	@for f in docs/graphs/*.dot; do \
		out=$${f%.dot}.svg; \
		echo "  graphviz $$f → $$out"; \
		dot -Tsvg $$f -o $$out; \
	done
