# Rius local stack. Every recipe runs from this file's own folder, so
# `make -C compose <target>` works from anywhere.
COMPOSE := docker compose
HERE    := $(patsubst %/,%,$(dir $(abspath $(firstword $(MAKEFILE_LIST)))))
RUN     := cd '$(HERE)' &&
# The MCP port as the scripts see it (environment, else compose/.env, else 8082); read only when used.
MCP_PORT = $(shell cd '$(HERE)' && bash -c '. ./scripts/lib.sh; printf %s "$$RIUS_MCP_PORT"')

# `--profile rca` while compose/.env (or the environment) holds RIUS_RCA_KEY, so a down/up keeps Tares running with the key.
RCA_PROFILE = $(shell cd '$(HERE)' && bash -c '. ./scripts/lib.sh; if [ -n "$$(rius_var RIUS_RCA_KEY "")" ]; then printf %s "--profile rca"; fi')

# The Tares model key comes ONLY from compose/.env (RIUS-1356). Compose reads the shell environment before
# .env, so a key exported in the shell would reach taresd and spend money. `unexport` keeps both variables out of
# every recipe (and so out of docker compose), which then reads them from .env alone. The line below names an
# exported variable on stderr, never its value; $(value) is used so the value is never expanded or echoed.
unexport RIUS_TARES_ANTHROPIC_API_KEY ANTHROPIC_API_KEY
SHELL_KEYS := $(strip $(if $(strip $(value RIUS_TARES_ANTHROPIC_API_KEY)),RIUS_TARES_ANTHROPIC_API_KEY) $(if $(strip $(value ANTHROPIC_API_KEY)),ANTHROPIC_API_KEY))
ifneq ($(SHELL_KEYS),)
ifneq ($(filter up dev rca-up rca-seed rca-off,$(MAKECMDGOALS)),)
$(shell echo "compose: $(SHELL_KEYS) is set in your shell and is ignored; the Tares model key is read only from compose/.env (RIUS_TARES_ANTHROPIC_API_KEY=...)" >&2)
endif
endif

# Which images the stack runs (RIUS-1351): `release` (default; the chart's images, from compose.yaml's
# defaults) or `workshop` (an event's rius-workshop-* images, which needs RIUS_WORKSHOP_TAG). Each setting
# comes from the make command line, the shell, or compose/.env, in that order.
ifeq ($(strip $(RIUS_IMAGES)),)
RIUS_IMAGES := $(shell cd '$(HERE)' && bash -c '. ./scripts/lib.sh; rius_var RIUS_IMAGES release')
endif
ifeq ($(RIUS_IMAGES),workshop)
ifeq ($(strip $(RIUS_WORKSHOP_TAG)),)
RIUS_WORKSHOP_TAG := $(shell cd '$(HERE)' && bash -c '. ./scripts/lib.sh; rius_var RIUS_WORKSHOP_TAG ""')
endif
ifeq ($(strip $(RIUS_WORKSHOP_TAG)),)
$(error RIUS_IMAGES=workshop needs RIUS_WORKSHOP_TAG, the event tag. Usage: set RIUS_IMAGES=workshop and RIUS_WORKSHOP_TAG=<event> in compose/.env (see compose/.env.workshop.example), or run: make -C compose up RIUS_IMAGES=workshop RIUS_WORKSHOP_TAG=<event>)
endif
ifeq ($(shell printf %s '$(subst ','\'',$(RIUS_WORKSHOP_TAG))' | grep -Eq '^[a-z0-9][a-z0-9.-]{0,62}$$' && echo ok),)
$(error RIUS_WORKSHOP_TAG='$(RIUS_WORKSHOP_TAG)' is not an event tag (lowercase letters, digits, '.' and '-'; starts with a letter or digit; at most 63 characters; no '<' or spaces). Use the event tag you were given)
endif
export RIUS_CORE_IMAGE_PREFIX := ghcr.io/glassflow/rius-workshop-
export RIUS_CORE_TAG := $(RIUS_WORKSHOP_TAG)
export RIUS_UI_IMAGE := ghcr.io/glassflow/rius-workshop-ui
export RIUS_UI_TAG := $(RIUS_WORKSHOP_TAG)
export RIUS_MCP_IMAGE := ghcr.io/glassflow/rius-workshop-mcp
export RIUS_MCP_TAG := $(RIUS_WORKSHOP_TAG)
else ifneq ($(RIUS_IMAGES),release)
$(error RIUS_IMAGES='$(RIUS_IMAGES)' is not valid: use release (the chart's images, the default) or workshop (needs RIUS_WORKSHOP_TAG))
endif

# EXTEND HERE: later tickets append their targets to PHONY_LATER (PHONY_LATER += smoke).
PHONY_LATER :=
PHONY_LATER += smoke key alerts psql ch
PHONY_LATER += check-versions sync-versions
PHONY_LATER += dev version
PHONY_LATER += plugin-check claude
PHONY_LATER += rca-up rca-seed rca-off rca-smoke
PHONY_LATER += print-images
.PHONY: up down reset pull ps logs urls check help $(PHONY_LATER)

.DEFAULT_GOAL := help

up: ## start the stack and wait until every endpoint answers
	@$(RUN) ./scripts/preflight.sh
	@$(RUN) $(COMPOSE) $(RCA_PROFILE) up -d --wait --wait-timeout 180
	@$(RUN) ./scripts/wait-ready.sh
	@$(MAKE) --no-print-directory -f '$(HERE)/Makefile' urls

down: ## stop the stack, keep data
	@$(RUN) $(COMPOSE) --profile '*' down

reset: ## stop the stack and delete all data (volumes); .env is kept
	@$(RUN) $(COMPOSE) --profile '*' down -v --remove-orphans

pull: ## pull the images (fails loudly on registry auth errors)
	@$(RUN) $(COMPOSE) pull

ps: ## list the containers, including exited one-shots
	@$(RUN) $(COMPOSE) ps -a

logs: ## follow logs; SERVICE=<name> for one service
	@$(RUN) $(COMPOSE) logs -f $(SERVICE)

urls: ## print where to reach the stack
	@$(RUN) ./scripts/urls.sh

smoke: ## end-to-end check of the running stack; last line SMOKE OK
	@$(RUN) ./scripts/smoke.sh

key: ## mint an API key (ingest, read) and print only the key; use make -s key
	@$(RUN) ./scripts/key.sh

alerts: ## print the alert bodies the local webhook received
	@$(RUN) $(COMPOSE) logs --no-log-prefix webhook | grep '"body"' || true

psql: ## open psql on the argus database
	@$(RUN) $(COMPOSE) exec postgres psql -U argus -d argus

ch: ## open clickhouse-client on the argus database
	@$(RUN) $(COMPOSE) exec clickhouse clickhouse-client -d argus

check: ## offline: compose config validates, scripts parse
	@$(RUN) $(COMPOSE) config -q
	@$(RUN) $(COMPOSE) -f compose.yaml -f compose.dev.yaml config -q
	@$(RUN) for f in scripts/*.sh; do \
	  if head -n 1 "$$f" | grep -q '^#!/bin/sh'; then sh -n "$$f" || exit 1; else bash -n "$$f" || exit 1; fi; \
	done; echo "check: ok"

rca-up: ## turn on local root-cause analysis (Tares); spends YOUR Anthropic credits once you set a key
	@$(RUN) ./scripts/preflight.sh rca
	@$(RUN) ./scripts/rca-key.sh on
	@$(RUN) bash -c '. ./scripts/lib.sh; if [ -z "$$(rius_var RIUS_TARES_ANTHROPIC_API_KEY "")" ]; then \
	  echo "warning: Tares runs will stop at '"'"'no model provider configured'"'"' until you set RIUS_TARES_ANTHROPIC_API_KEY in compose/.env"; fi'
	@$(RUN) $(COMPOSE) --profile rca up -d --wait --wait-timeout 180
	@$(RUN) ./scripts/wait-ready.sh
	@$(RUN) $(COMPOSE) --profile rca-tools run --rm rca-seed
	@$(RUN) echo "Tares console: http://localhost:$$(bash -c '. ./scripts/lib.sh; printf %s "$$RIUS_TARES_PORT"')"

rca-seed: ## re-run only the RCA wiring (updates the existing Tares project)
	@$(RUN) $(COMPOSE) --profile rca-tools run --rm rca-seed

rca-smoke: ## prove an alert gets a root-cause analysis (after rca-up); last line RCA OK
	@$(RUN) ./scripts/rca-smoke.sh

rca-off: ## turn local root-cause analysis off (stops Tares, drops the key from .env)
	@$(RUN) ./scripts/rca-key.sh off
	@$(RUN) $(COMPOSE) --profile rca stop taresd
	@$(RUN) $(COMPOSE) up -d --wait api-server alert-manager
	@$(RUN) ./scripts/wait-ready.sh

print-images: ## print the images this stack would run (RIUS_IMAGES=workshop RIUS_WORKSHOP_TAG=<event> for a workshop)
	@$(RUN) $(COMPOSE) config --images

check-versions: ## compose's default image tags equal the chart's (needs helm; network on first run)
	@$(RUN) ./scripts/check-versions.sh

sync-versions: ## copy the chart's image pins into compose.yaml, then check them
	@$(RUN) ./scripts/sync-versions.sh

dev: ## rebuild one service from a local checkout and swap it in: make -C compose dev SERVICE=api-server
	@$(RUN) ./scripts/dev.sh "$$SERVICE"

version: ## which image and version the api-server runs
	@$(RUN) ./scripts/version.sh

plugin-check: ## prove the Rius plugin traces into this stack (needs the default ports); last line PLUGIN OK
	@$(RUN) ./scripts/plugin-check.sh

claude: ## print how to point Claude Code at this stack (MCP server and plugin)
	@echo "# This bundle has no MCP server; Rius cloud has one (the plugin adds it as plugin:rius:rius)."
	@echo "# Send this machine's Claude Code sessions to the local stack (needs a Rius plugin with --env local; 0.6.2 will have it):"
	@echo "make -s -C compose key | bash <plugin>/scripts/rius_ctl.sh use-key --env local"
	@echo "# <plugin> is the installed Rius plugin's folder, for example"
	@echo "# ~/.claude/plugins/cache/rius-coding-agents/rius/<version> (see the plugin's docs/api-keys.md)."
	@echo "# This replaces, and revokes, the key stored for production; /rius:login gets a new one when you go back."
	@echo "# Then run /rius:enable-here in the project folder you want traced."

help: ## list the targets
	@echo "Targets (make -C compose <target>):"
	@echo "  up      start the stack and wait until every endpoint answers"
	@echo "  down    stop the stack, keep data"
	@echo "  reset   stop the stack and delete all data (volumes); .env is kept"
	@echo "  pull    pull the images"
	@echo "  ps      list the containers, including exited one-shots"
	@echo "  logs    follow logs; SERVICE=<name> for one service"
	@echo "  urls    print where to reach the stack"
	@echo "  smoke   end-to-end check of the running stack; last line SMOKE OK"
	@echo "  key     mint an API key (ingest, read) and print only the key; use make -s key"
	@echo "  alerts  print the alert bodies the local webhook received"
	@echo "  psql    open psql on the argus database"
	@echo "  ch      open clickhouse-client on the argus database"
	@echo "  check   offline: compose config validates, scripts parse"
	@echo "  print-images  the images this stack would run (RIUS_IMAGES=workshop RIUS_WORKSHOP_TAG=<event> for a workshop)"
	@echo "  check-versions  compose's default image tags equal the chart's"
	@echo "  sync-versions   copy the chart's image pins into compose.yaml, then check"
	@echo "  dev     rebuild one service from a local checkout: make -C compose dev SERVICE=api-server"
	@echo "  version which image and version the api-server runs"
	@echo "  plugin-check  prove the Rius plugin traces into this stack; last line PLUGIN OK"
	@echo "  rca-up  turn on local root-cause analysis (Tares); needs your own Anthropic key to spend credits"
	@echo "  rca-seed  re-run only the RCA wiring"
	@echo "  rca-off turn local root-cause analysis off"
	@echo "  rca-smoke  prove an alert gets a root-cause analysis (after rca-up); last line RCA OK"
	@echo "  claude  print how to point Claude Code at this stack (MCP server and plugin)"
	@echo "  help    this list"
# EXTEND HERE: later tickets add one echo line per new target above this comment.
