# ─────────────────────────────────────────────────────────────────────────────
# AIDO white-label of Onyx Community Edition — operator shortcuts.
#
#   make rebrand     apply branding + build the web image + (re)start the stack
#   make apply       only re-run the branding overlay into ./onyx/web
#   make build       only (re)build the branded web image
#   make up / down   start / stop the full stack
#   make logs        follow the web container logs
#   make verify      sanity-check that branding was applied (no stray "Onyx")
#
# The --project-directory . flag pins the Compose project dir to this repo root so
# the override's `./onyx/web` context and the root-level `.env` both resolve, while
# every other service is pulled unchanged from Onyx's official images.
# ─────────────────────────────────────────────────────────────────────────────

# Use the no-letsencrypt prod compose by default (no TLS/domain needed). Override
# with `make COMPOSE_BASE=onyx/deployment/docker_compose/docker-compose.prod.yml`.
COMPOSE_BASE ?= onyx/deployment/docker_compose/docker-compose.prod-no-letsencrypt.yml
COMPOSE = docker compose --project-directory . -f $(COMPOSE_BASE) -f docker-compose.branding.yml

.PHONY: setup rebrand apply build up down restart logs verify config

# Clone the upstream Onyx checkout at the pinned commit (only if ./onyx is absent).
# Reads upstream.repo / upstream.pinnedCommit from branding/branding.config.json.
ONYX_REPO   = $(shell node -e "console.log(require('./branding/branding.config.json').upstream.repo)")
ONYX_COMMIT = $(shell node -e "console.log(require('./branding/branding.config.json').upstream.pinnedCommit)")
setup:
	@if [ -d onyx/.git ] || [ -d onyx/web ]; then \
		echo "onyx/ already present — skipping clone."; \
	else \
		echo "Cloning $(ONYX_REPO) @ $(ONYX_COMMIT) into ./onyx ..."; \
		git clone $(ONYX_REPO) onyx && git -C onyx checkout $(ONYX_COMMIT); \
	fi

rebrand: apply build up

apply:
	node branding/apply-branding.mjs

build:
	$(COMPOSE) build web_server

up:
	$(COMPOSE) up -d

down:
	$(COMPOSE) down

restart:
	$(COMPOSE) up -d --force-recreate web_server

logs:
	$(COMPOSE) logs -f web_server

config:
	$(COMPOSE) config

verify:
	@echo "Stray standalone 'Onyx' in shipping web source (expect 0):"
	@grep -rnE '\bOnyx\b' onyx/web/src --include='*.ts' --include='*.tsx' \
		| grep -vE '\.test\.|\.spec\.|\.stories\.|__tests__|__mocks__' | wc -l
