.DEFAULT_GOAL := help
SHELL := /bin/bash
RUN := uv run

.PHONY: help install migrate seed test lint fmt mcp agents css demo ui export import reindex test-db

help:  ## Show available targets
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
		| awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-10s\033[0m %s\n", $$1, $$2}'

install:  ## Create the venv and install (editable, with dev deps)
	uv sync --locked --extra dev

migrate:  ## Create the store file if needed and apply migrations/*.sql (MNEMO_DB)
	$(RUN) python -m mnemo.db

seed:  ## Load demo seed data
	$(RUN) python -m mnemo.seed

test:  ## Run pytest
	$(RUN) pytest -q

lint:  ## ruff check + black --check
	$(RUN) ruff check .
	$(RUN) black --check .

fmt:  ## ruff --fix + black
	$(RUN) ruff check --fix .
	$(RUN) black .

mcp:  ## Run the seven guarded memory tools over MCP (stdio)
	$(RUN) python -m mnemo.mcp_direct

agents:  ## Install the mnemo tool from this checkout, then connect the coding agents
	uv tool install --force --reinstall --python 3.12 .
	"$$(uv tool dir --bin)/mnemo" install $(ARGS)

demo:  ## Direct write -> history -> revert through the SDK, no models
	MNEMO_BACKEND=hash $(RUN) python -m examples.direct_memory

export:  ## Export the configured scope as versioned JSON (OUT=file, default stdout)
	$(RUN) python -m mnemo.transfer export --out $(or $(OUT),-)

import:  ## Restore an export (or a Postgres-era one) into an empty store (FILE=export.json)
	$(RUN) python -m mnemo.transfer import $(FILE)

reindex:  ## Rebuild vectors (for the configured model) and the word index
	$(RUN) mnemo reindex

ui:  ## Run the FastAPI + HTMX web UI
	$(RUN) uvicorn mnemo.web.app:app --host 127.0.0.1 --port 8000

css:  ## Rebuild mnemo/web/static/app.css from the templates (needs Node for npx)
	npx -y tailwindcss@3.4.19 -c mnemo/web/tailwind.config.js -i mnemo/web/tailwind.css -o mnemo/web/static/app.css --minify

test-db:  ## The full suite; every test gets its own temporary store file
	$(RUN) pytest -q
