# Copied with the demo directory. build/exec/sync use a local image based on bkai-init.
DEMO_DIR:=$(abspath $(dir $(lastword $(MAKEFILE_LIST))))
CONTAINER_TOOL?=podman
IMAGE?=bkai-init-demo:local
BKAI_INIT_IMAGE?=bkai-init:local
BKAI_INIT_VERSION?=0.1.2
ENV_FILE?=$(DEMO_DIR)/.env
PACKAGE_IN_IMAGE?=/app/package/bkai.yaml
TENANT_ID?=
SPACE?=
ADMINS?=
RESOURCES?=
EXCLUDE_RESOURCES?=
CHECK?=false
PUBLISH?=true
PUBLISH_CONFIG_ONLY?=0

.PHONY: build
build:
	@set -eu; \
		env_file="$(ENV_FILE)"; \
		case "$$env_file" in /*) ;; *) env_file="./$$env_file" ;; esac; \
		version=$$( \
			if test -f "$$env_file"; then \
				. "$$env_file"; \
			fi; \
			printf '%s' "$(if $(filter file,$(origin BKAI_INIT_VERSION)),$${BKAI_INIT_VERSION:-$(BKAI_INIT_VERSION)},$(BKAI_INIT_VERSION))"; \
		); \
	$(CONTAINER_TOOL) build \
		--build-arg BKAI_INIT_IMAGE=$(BKAI_INIT_IMAGE) \
		--build-arg "BKAI_INIT_VERSION=$$version" \
		-t $(IMAGE) \
		-f $(DEMO_DIR)/Dockerfile \
		$(DEMO_DIR)

# Start a container from the local image and open a shell.
.PHONY: exec
exec:
	@set -eu; \
		set -- --rm -it --entrypoint bash; \
		if test -f "$(ENV_FILE)"; then \
			env_file="$(ENV_FILE)"; \
			case "$$env_file" in /*) ;; *) env_file="./$$env_file" ;; esac; \
			container_env=$$(mktemp); \
			trap 'rm -f "$$container_env"' EXIT; \
			sed -e 's/^[[:space:]]*export[[:space:]]\{1,\}//' -e '/^[[:space:]]*\#/d' -e '/^[[:space:]]*$$/d' "$$env_file" > "$$container_env"; \
			set -- --rm -it --env-file "$$container_env" -v "$$env_file:/app/.env:ro" --entrypoint bash; \
		fi; \
		$(CONTAINER_TOOL) run "$$@" "$(IMAGE)"

# validate only checks the package baked into the image.
.PHONY: validate show diff plan sync
validate:
	@set -eu; \
		space="$(or $(SPACE),$${BKAI_SPACE_ID:-})"; \
		set --; if test -n "$$space"; then set -- --space "$$space"; fi; \
		$(CONTAINER_TOOL) run --rm "$(IMAGE)" validate -f "$(PACKAGE_IN_IMAGE)" "$$@" $(foreach admin,$(ADMINS),--admins "$(admin)")

# sync writes and always passes --confirm. plan previews the same image.
show diff plan sync:
	@set -eu; \
		test -f "$(ENV_FILE)" || { echo "ENV_FILE does not exist; copy .env.example to .env" >&2; exit 1; }; \
		env_file="$(ENV_FILE)"; case "$$env_file" in /*) ;; *) env_file="./$$env_file" ;; esac; \
		container_env=$$(mktemp); \
		trap 'rm -f "$$container_env"' EXIT; \
		sed -e 's/^[[:space:]]*export[[:space:]]\{1,\}//' -e '/^[[:space:]]*\#/d' -e '/^[[:space:]]*$$/d' "$$env_file" > "$$container_env"; \
		set -a; . "$$env_file"; set +a; \
		space="$(or $(SPACE),$${BKAI_SPACE_ID:-})"; \
		tenant="$(or $(TENANT_ID),$${BKAI_TENANT_ID:-})"; \
		set --; \
		if test -n "$$tenant"; then set -- --tenant-id "$$tenant"; fi; \
		if test -n "$$space"; then set -- "$$@" --space "$$space"; fi; \
		if test "$${BKAI_SKILL_IMAGE_REPO+x}" = x; then \
			set -- "$$@" --var "BKAI_SKILL_IMAGE_REPO=$$BKAI_SKILL_IMAGE_REPO"; \
		fi; \
		$(CONTAINER_TOOL) run --rm --env-file "$$container_env" -v "$$env_file:/app/.env:ro" "$(IMAGE)" $@ \
		-f "$(PACKAGE_IN_IMAGE)" \
		"$$@" \
		$(foreach admin,$(ADMINS),--admins "$(admin)") \
		$(if $(filter sync,$@),--confirm) \
		$(foreach resource,$(RESOURCES),--resource "$(resource)") \
		$(if $(filter sync plan,$@),$(foreach resource,$(EXCLUDE_RESOURCES),--exclude-resource "$(resource)")) \
		$(if $(and $(filter diff,$@),$(filter true,$(CHECK))),--check) \
		$(if $(and $(filter sync plan,$@),$(filter true,$(PUBLISH))),--publish --publish_config_only="$(PUBLISH_CONFIG_ONLY)")
