# macOS ships GNU Make 3.81, which ignores .ONESHELL — so the `dev` recipe is one
# shell line (\-joined) to keep the trap + background jobs in a single shell.
SHELL := /bin/bash

TOKEN      ?= devtoken
CLOUD_PORT ?= 8770
DASH_PORT  ?= 8765
CLOUD_URL  := http://127.0.0.1:$(CLOUD_PORT)
# cloud_api.py is Postgres-backed (Phase 3). `make pg` spins a throwaway local one.
DATABASE_URL ?= postgres://postgres:pw@127.0.0.1:5432/gcontext

.PHONY: dev cloud dashboard pg test help

help:
	@echo "make pg         - run a throwaway local Postgres in docker (port 5432)"
	@echo "make dev        - run cloud API + dashboard together (dashboard is cloud-backed)"
	@echo "make cloud      - run only the cloud API   ($(CLOUD_URL))  [needs DATABASE_URL]"
	@echo "make dashboard  - run only the dashboard         (http://127.0.0.1:$(DASH_PORT))"
	@echo "make test       - run the phase verify checks against DATABASE_URL"
	@echo
	@echo "To connect your AI client in cloud mode, set in its MCP config env:"
	@echo "    GCONTEXT_API_URL=$(CLOUD_URL)   GCONTEXT_TOKEN=$(TOKEN)"

# Throwaway local Postgres for dev/tests. Data is ephemeral.
pg:
	docker run -d --name gcontext-pg -e POSTGRES_PASSWORD=pw -e POSTGRES_DB=gcontext \
		-p 5432:5432 postgres:16

# One command to bring the whole local stack up. Ctrl-C stops both.
dev:
	@echo "cloud API -> $(CLOUD_URL)"
	@echo "dashboard -> http://127.0.0.1:$(DASH_PORT)"
	@echo "(Ctrl-C to stop both)"
	@trap 'kill 0' EXIT; \
	DATABASE_URL=$(DATABASE_URL) GCONTEXT_DEV_TOKEN=$(TOKEN) PORT=$(CLOUD_PORT) uv run cloud_api.py & \
	sleep 1; \
	GCONTEXT_API_URL=$(CLOUD_URL) GCONTEXT_TOKEN=$(TOKEN) uv run dashboard.py & \
	wait

cloud:
	DATABASE_URL=$(DATABASE_URL) GCONTEXT_DEV_TOKEN=$(TOKEN) PORT=$(CLOUD_PORT) uv run cloud_api.py

dashboard:
	GCONTEXT_API_URL=$(CLOUD_URL) GCONTEXT_TOKEN=$(TOKEN) uv run dashboard.py

test:
	DATABASE_URL=$(DATABASE_URL) uv run test_phase1.py
	DATABASE_URL=$(DATABASE_URL) uv run test_phase2.py
