-include .env
export

# Bare `make` lists targets (ACTIVITY-WP-0028 follow-up).
.DEFAULT_GOAL := help

.PHONY: help sync-event-types sync-activity-definitions sync-schedules test migrate sync-all \
        automation-status automation-status-json automation-list automation-list-json \
        activity-review install-cli \
        prod-automation-status refresh-live-images \
        dev-up dev-down railiance-up railiance-down \
        start-worker start-api start-event-router \
        openbao-eso-token-apply agents-list agents-update agents-validate

# ── Help ──────────────────────────────────────────────────────────────────────

help:  ## Show available make targets
	@echo "activity-core — available targets"
	@echo ""
	@grep -Eh '^[a-zA-Z0-9_-]+:.*?##' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-28s\033[0m %s\n", $$1, $$2}' | \
		sort
	@echo ""
	@echo "CLI:  make install-cli          # install 'activity' into userspace (uv tool)"
	@echo "      make activity-review ARGS='status --cwd ~/freedom-intelligence'"
	@echo "      activity service list     # named activity-core backends"
	@echo "      activity runs -s railiance --since today"

# ── Sync / test ───────────────────────────────────────────────────────────────

sync-activity-definitions:  ## Sync ActivityDefinition files into DB
	uv run python -m activity_core.sync_activity_definitions

sync-schedules:  ## Reconcile Temporal schedules from activity_definitions DB
	uv run python -m activity_core.sync_schedules

sync-event-types:  ## Sync event type YAML files into DB
	uv run python scripts/sync_event_types.py

test:  ## Run test suite
	uv run pytest tests/ -v

migrate:  ## Apply all pending Alembic migrations
	uv run alembic upgrade head

sync-all: sync-event-types sync-activity-definitions  ## Sync event types and activity definitions

# ── Automation status (org-wide) ──────────────────────────────────────────────

SINCE ?= today
FORMAT ?= human
ENABLED ?= all
TRIGGER ?=
ACTIVITY_ID ?=
ACTIVITY_NAME ?=
ARGS ?=

automation-status:  ## Report recent automation status from repo-owned evidence
	uv run python scripts/automation_status.py --since "$(SINCE)" $(if $(UNTIL),--until "$(UNTIL)",) --format "$(FORMAT)"

automation-status-json:  ## Report recent automation status as JSON
	$(MAKE) automation-status FORMAT=json

prod-automation-status:  ## Prod run counts via SSH railiance01 (ACTIVITY-WP-0021)
	./scripts/prod_automation_status.sh $(SINCE)

refresh-live-images:  ## Refresh multi-cluster Forgejo live-images protection list
	./scripts/refresh_live_images.sh

automation-list:  ## List configured scheduled automations from repo-owned definitions
	@uv run python scripts/automation_inventory.py --format "$(FORMAT)" --enabled "$(ENABLED)" $(if $(TRIGGER),--trigger-type "$(TRIGGER)",) $(if $(ACTIVITY_ID),--activity-id "$(ACTIVITY_ID)",) $(if $(ACTIVITY_NAME),--activity-name "$(ACTIVITY_NAME)",)

automation-list-json:  ## List configured scheduled automations as JSON
	@$(MAKE) --no-print-directory automation-list FORMAT=json

# ── activity CLI (repo review + multi-service) ────────────────────────────────

install-cli:  ## Install 'activity' CLI into userspace (uv tool install -e .)
	@command -v uv >/dev/null || { echo "uv not found — install from https://docs.astral.sh/uv/"; exit 1; }
	uv tool install --force -e .
	@echo ""
	@echo "Installed. Ensure \$$HOME/.local/bin is on PATH, then:"
	@echo "  activity --help"
	@echo "  activity service add railiance --url https://activity.coulomb.social --make-default"
	@echo "  activity service list"
	@command -v activity >/dev/null && activity --help | head -3 || true

activity-review:  ## Run activity CLI (pass ARGS='…'). Example: ARGS='--cwd ~/freedom-intelligence status'
	uv run activity $(ARGS)

# ── Infrastructure ────────────────────────────────────────────────────────────

dev-up:  ## Start full dev stack (Temporal + PG + ES + NATS)
	docker compose -f docker-compose.dev.yml up -d

dev-down:  ## Stop and remove dev stack containers
	docker compose -f docker-compose.dev.yml down

railiance-up:  ## Build image and start full railiance stack (no Elasticsearch)
	docker compose -f docker-compose.railiance.yml up -d --build

railiance-down:  ## Stop and remove railiance stack containers
	docker compose -f docker-compose.railiance.yml down

openbao-eso-token-apply:  ## Mint coulombcore OpenBao ESO token for railiance01 activity-core
	OPENBAO_TOKEN_FILE="$(HOME)/.local/openbao/platform-admin.token" bash scripts/openbao-eso-token-apply.sh

# ── Local dev processes ───────────────────────────────────────────────────────

start-worker:  ## Start Temporal worker (reads env from .env if present)
	uv run python -m activity_core.worker

start-api:  ## Start FastAPI server on :8010 with hot reload
	uv run uvicorn activity_core.api:app --host 0.0.0.0 --port 8010 --reload

start-event-router:  ## Start NATS event router
	uv run python -m activity_core.event_router

# ── Agents ────────────────────────────────────────────────────────────────────

agents-list:  ## List installed kaizen agents
	@echo "Installed agents:"
	@ls agents/ 2>/dev/null | grep agent- | sed 's/agent-//g' | sed 's/.md//g' \
	|| echo "No agents installed"

agents-update:  ## Update agents via kaizen-agentic
	@echo "Updating agents..."
	@kaizen-agent update

agents-validate:  ## Validate agents via kaizen-agentic
	@echo "Validating agents..."
	@kaizen-agentic validate agents/
