.DEFAULT_GOAL := help
DIST := dist
UV := uv

.PHONY: help install dev lint fmt test build clean smoke check docs-cli supabase-start supabase-start-mcp eval-agent-load eval-agent-check supabase-stop supabase-reset test-supabase

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

install:  ## Create .venv + editable install (core deps only)
	$(UV) venv
	$(UV) pip install -e .

dev:  ## Create .venv + editable install with the extras CI tests with (dev, server, cli, lineage)
	$(UV) venv
	$(UV) pip install -e ".[dev,server,cli,lineage]"

lint:  ## Run ruff linter
	$(UV) run --no-sync ruff check src/ tests/ workflows/

fmt:  ## Auto-format with ruff
	$(UV) run --no-sync ruff format src/ tests/ workflows/
	$(UV) run --no-sync ruff check --fix src/ tests/ workflows/

test:  ## Run pytest
	$(UV) run --no-sync pytest --tb=short -q

build: clean  ## Build sdist + wheel into dist/
	$(UV) build
	@echo ""
	@ls -lh $(DIST)/
	@echo ""
	@echo "Wheel: $$(ls $(DIST)/*.whl)"

clean:  ## Remove build artifacts
	rm -rf $(DIST) build src/*.egg-info src/argus_lens/*.egg-info

smoke: build  ## Build wheel, install in throwaway venv, smoke-test import
	$(eval TMPVENV := $(shell mktemp -d))
	$(UV) venv $(TMPVENV)/venv
	$(UV) pip install --python $(TMPVENV)/venv $(DIST)/*.whl
	$(TMPVENV)/venv/bin/python -c \
		"from argus_lens import ArgusLens, __version__; print(f'argus-lens {__version__} OK')"
	rm -rf $(TMPVENV)

docs-cli:  ## Regenerate docs/cli.md with the locked Typer (its output format varies by version)
	$(UV) run --frozen --extra cli typer argus_lens.cli utils docs --name argus-lens --output docs/cli.md

check: lint test build  ## Full local CI: lint + test + build
	@echo ""
	@echo "All checks passed."

# The local Supabase stack for the dataset exporter's tests (#161). Local only; needs Docker and Node.
SUPABASE := npx -y supabase@3.4.0
SUPABASE_SKIP := imgproxy,postgres-meta,studio,supavisor,vector,logflare
# The agent-query eval (eval/agent_query) also needs Studio and postgres-meta: Studio serves the MCP endpoint.
SUPABASE_SKIP_MCP := imgproxy,supavisor,vector,logflare
# argus-lightbox's committed synthetic fixture, when the sibling repo is checked out (else that test skips).
LIGHTBOX_DATA ?= ../argus-lightbox/web/public/data

# The CLI publishes the stack's ports on every interface (0.0.0.0), and Docker's published ports bypass ufw, so
# the database (stock postgres login) is reachable from any network the host is on. To pin it to one address,
# create a Docker network that binds there and name it, with the hostname the CLI should report:
#   docker network create -o com.docker.network.bridge.host_binding_ipv4=<address> <name>
#   SUPABASE_NETWORK=<name> SUPABASE_SERVICES_HOSTNAME=<address> make supabase-start
# (SUPABASE_SERVICES_HOSTNAME is also needed for `make test-supabase`.)
SUPABASE_NETWORK ?=
SUPABASE_NETWORK_FLAG := $(if $(SUPABASE_NETWORK),--network-id $(SUPABASE_NETWORK))

supabase-start:  ## Start the local Supabase stack (applies supabase/migrations)
	$(SUPABASE) start -x $(SUPABASE_SKIP) $(SUPABASE_NETWORK_FLAG)

supabase-start-mcp:  ## Start the local stack with Studio, which serves its MCP endpoint (for eval/agent_query)
	$(SUPABASE) start -x $(SUPABASE_SKIP_MCP) $(SUPABASE_NETWORK_FLAG)

# `db reset` recreates the database container, and without --network-id it goes back on the CLI's own network:
# published on every interface again, and unreachable by the other services. Always reset through this target.
supabase-reset:  ## Recreate the local database and reapply supabase/migrations (deletes its data)
	$(SUPABASE) db reset $(SUPABASE_NETWORK_FLAG)

supabase-stop:  ## Stop the local Supabase stack (keeps its data volumes)
	$(SUPABASE) stop

test-supabase:  ## Run the dataset exporter tests against the running local stack
	@eval "$$($(SUPABASE) status -o env 2>/dev/null | grep -E '^(API_URL|SECRET_KEY|ANON_KEY|DB_URL)=')" && \
		LENS_TEST_SUPABASE_URL="$$API_URL" LENS_TEST_SUPABASE_KEY="$$SECRET_KEY" LENS_TEST_PG_URL="$$DB_URL" \
		LENS_TEST_SUPABASE_ANON_KEY="$$ANON_KEY" \
		LENS_TEST_LIGHTBOX_DATA="$(wildcard $(LIGHTBOX_DATA))" \
		$(UV) run --no-sync pytest tests/test_dataset_export.py tests/test_supabase_export.py \
			tests/test_supabase_storage.py tests/test_supabase_realtime.py \
			tests/test_supabase_vectors.py -q -rs

# The agent-query eval (eval/agent_query, #161). Needs the stack from `make supabase-start-mcp`.
EVAL_AGENT := eval/agent_query
# A directory holding the original photos, when there is one; otherwise they are downloaded.
EVAL_AGENT_IMAGES ?=

eval-agent-load: supabase-reset  ## Reset the local database and load the agent-query eval's fixture into it
	@eval "$$($(SUPABASE) status -o env 2>/dev/null | grep -E '^(API_URL|SECRET_KEY)=')" && \
		API_URL="$$API_URL" SECRET_KEY="$$SECRET_KEY" $(UV) run --no-sync python $(EVAL_AGENT)/load_fixture.py \
			$(if $(EVAL_AGENT_IMAGES),--images $(EVAL_AGENT_IMAGES))

eval-agent-check:  ## Check that every eval task's reference SQL reproduces its ground truth
	@eval "$$($(SUPABASE) status -o env 2>/dev/null | grep -E '^MCP_URL=')" && \
		MCP_URL="$$MCP_URL" $(UV) run --no-sync python $(EVAL_AGENT)/check_tasks.py
