.DEFAULT_GOAL := help

ROOT := $(abspath ..)
DOCKER ?= docker
APP_PROJECT ?= app
ML_PROJECT ?= ml
MONITORING_PROJECT ?= monitoring
APP_NETWORK ?= $(APP_PROJECT)_default
ENV_FILE ?= $(ROOT)/.env
CHECK_ENV_FILE := $(ROOT)/.env.example
# The example file has no usable IDENTITY_KEY_SECRET, which Compose requires.
# Commands that render or build from it get this value in their own
# environment only, never in .env targets, where it would override the real one.
CHECK_IDENTITY_KEY_SECRET := render-only-identity-key-secret-not-for-deployment
CORE_FILE := $(ROOT)/deploy/docker/docker-compose.yml
ML_FILE := $(ROOT)/deploy/docker/docker-compose.ml.yml
MONITORING_FILE := $(ROOT)/deploy/docker/docker-compose.monitoring.yml
E2E_FILE := $(ROOT)/deploy/docker/docker-compose.e2e.yml
E2E_MAIL_FILE := $(ROOT)/deploy/docker/docker-compose.e2e-mail.yml
DEMO_FILE := $(ROOT)/deploy/docker/docker-compose.demo.yml
DEMO_RELEASE_FILE := $(ROOT)/deploy/docker/docker-compose.demo.release.yml
RELEASE_FILE := $(ROOT)/deploy/docker/docker-compose.release.yml
ML_RELEASE_FILE := $(ROOT)/deploy/docker/docker-compose.ml.release.yml
# DEVELOPMENT ONLY: the privileged local code runner. It is never rendered
# together with a release overlay or started by a release target.
RUNNER_DEV_FILE := $(ROOT)/deploy/docker/docker-compose.runner.dev.yml
RUNNER_DEV_PROJECT ?= runner-dev
# Local random service token of the dev runner; runner/.gitignore keeps it out of Git.
RUNNER_DEV_TOKEN_FILE ?= $(ROOT)/runner/.dev/api-token
RELEASE_SCRIPT := $(ROOT)/deploy/scripts/release_manifest.py
RELEASE := PYTHONDONTWRITEBYTECODE=1 python3 "$(RELEASE_SCRIPT)"
# Pull policy of release-build for base and service images: missing or always.
RELEASE_PULL ?= missing
# Compose project whose running containers release-manifest-verify compares.
RELEASE_VERIFY_PROJECT ?= $(APP_PROJECT)
# Optional manifest path for release-manifest-check, verify, and restore.
# Empty selects RELEASE_ID when set, otherwise the current commit-based ID.
RELEASE_MANIFEST ?=
# Optional immutable delivery ID; empty keeps the current commit-based ID.
RELEASE_ID ?=
# Optional repository-relative manifest describing bank, policy, and seed inputs.
RELEASE_DATA_MANIFEST ?=
RELEASE_MANIFEST_ARG = $(if $(strip $(RELEASE_MANIFEST)),--manifest "$(RELEASE_MANIFEST)",$(if $(strip $(RELEASE_ID)),--manifest "deploy/release/$(RELEASE_ID)/manifest.json",))
# Recursive on purpose: git is asked only by the targets that use the tag. The
# project-specific name keeps a generic RELEASE_TAG from enabling release mode.
LINCODEX_RELEASE_TAG ?= $(shell PYTHONDONTWRITEBYTECODE=1 python3 "$(RELEASE_SCRIPT)" tag)
# Isolated demonstration stand (deploy/scripts/demo.sh). Every demo target
# needs DEMO_MODE=isolated on the command line and acts only on DEMO_PROJECT.
DEMO_MODE ?=
DEMO_PROJECT ?=
DEMO_HTTP_PORT ?= 8090
DEMO_PUBLIC_URL ?= http://localhost:$(DEMO_HTTP_PORT)
DEMO_SUBNET ?= 172.29.84.0/24
DEMO_FRONTEND_IP ?= 172.29.84.10
DEMO_ACTOR ?= operator:demo-seed
# demo.env is local; the checked-in example is used when it does not exist.
DEMO_ENV ?= $(if $(wildcard $(ROOT)/deploy/demo/demo.env),$(ROOT)/deploy/demo/demo.env,$(ROOT)/deploy/demo/demo.env.example)
# An explicit RELEASE_MANIFEST or RELEASE_ID restores the demo stand from that
# immutable manifest; without them the stand builds from the current sources.
DEMO_RELEASE_MANIFEST = $(if $(strip $(RELEASE_MANIFEST)),$(RELEASE_MANIFEST),$(if $(strip $(RELEASE_ID)),deploy/release/$(RELEASE_ID)/manifest.json,))
DEMO_SCRIPT_ENV = DOCKER="$(DOCKER)" DEMO_MODE="$(DEMO_MODE)" DEMO_PROJECT="$(DEMO_PROJECT)" APP_PROJECT="$(APP_PROJECT)" \
	ENV_FILE="$(ENV_FILE)" DEMO_ENV_FILE="$(DEMO_ENV)" DEMO_HTTP_PORT="$(DEMO_HTTP_PORT)" DEMO_PUBLIC_URL="$(DEMO_PUBLIC_URL)" \
	DEMO_SUBNET="$(DEMO_SUBNET)" DEMO_FRONTEND_IP="$(DEMO_FRONTEND_IP)" DEMO_ACTOR="$(DEMO_ACTOR)" \
	DEMO_RELEASE_MANIFEST="$(DEMO_RELEASE_MANIFEST)"
DEMO_MONITORING_SCRIPT_ENV = DOCKER="$(DOCKER)" DEMO_MODE="$(DEMO_MODE)" DEMO_PROJECT="$(DEMO_PROJECT)" \
	DEMO_ENV_FILE="$(DEMO_ENV)" APP_PROJECT="$(APP_PROJECT)" MONITORING_PROJECT="$(MONITORING_PROJECT)"
PLAYWRIGHT_VERSION := $(shell sed -n 's/^ *"@playwright\/test": "\([^"]*\)",\{0,1\}$$/\1/p' "$(ROOT)/frontend/package.json")

.PHONY: help config build up down logs monitoring-config monitoring-up monitoring-down monitoring-logs ml-config ml-build ml-up ml-down ml-logs runner-dev-config runner-dev-build runner-dev-up runner-dev-down runner-dev-logs check-runner-dev check-runner-vm check check-workflows check-release check-perimeter check-e2e-mail check-demo perimeter-test smoke test-e2e test-e2e-registration release-build release-smoke release-e2e release-manifest release-manifest-verify release-manifest-check release-up release-down release-clean demo-seed demo-reset demo-down

help: ## Show deployment commands
	@awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z0-9_-]+:.*## / {printf "%-24s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

config: ## Render all Compose files with safe example values
	@test -f "$(ROOT)/backend/Dockerfile" || { printf 'Missing backend/Dockerfile.\n' >&2; exit 2; }
	@test -f "$(ROOT)/frontend/Dockerfile" || { printf 'Missing frontend/Dockerfile.\n' >&2; exit 2; }
	@test -f "$(ROOT)/ml/Dockerfile" || { printf 'Missing ml/Dockerfile.\n' >&2; exit 2; }
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" config --quiet
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" config --quiet
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" config --quiet
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" LINCODEX_RELEASE_TAG=000000000000 IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" -f "$(RELEASE_FILE)" config --quiet
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" LINCODEX_RELEASE_TAG=000000000000 IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" -f "$(ML_RELEASE_FILE)" config --quiet
	APP_PROJECT=lincodex-e2e-config APP_NETWORK=lincodex-e2e-config_default PLAYWRIGHT_VERSION="$(PLAYWRIGHT_VERSION)" E2E_UID=1000 E2E_GID=1000 E2E_SPEC_DIR="$(ROOT)/frontend/e2e" E2E_ARTIFACTS_DIR="$(ROOT)/frontend/test-results/e2e" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-e2e-config -f "$(CORE_FILE)" -f "$(E2E_FILE)" --profile e2e config --quiet
	APP_PROJECT=lincodex-demo-config APP_NETWORK=lincodex-demo-config_default DEMO_ACCOUNT_PASSWORD=render-only-demo-account-password IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-demo-config -f "$(CORE_FILE)" -f "$(DEMO_FILE)" --profile demo-seed config --quiet
	APP_PROJECT=lincodex-demo-config APP_NETWORK=lincodex-demo-config_default LINCODEX_RELEASE_TAG=000000000000 DEMO_ACCOUNT_PASSWORD=render-only-demo-account-password IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-demo-config -f "$(CORE_FILE)" -f "$(RELEASE_FILE)" -f "$(DEMO_FILE)" -f "$(DEMO_RELEASE_FILE)" --profile demo-seed config --quiet
	E2E_COMPANY_FIXTURE_TOKEN=render-only-company-fixture-token-00000000000000000000000000000000 APP_PROJECT=lincodex-e2e-mail-config APP_NETWORK=lincodex-e2e-mail-config_default PLAYWRIGHT_VERSION="$(PLAYWRIGHT_VERSION)" E2E_UID=1000 E2E_GID=1000 E2E_SPEC_DIR="$(ROOT)/frontend/e2e" E2E_ARTIFACTS_DIR="$(ROOT)/frontend/test-results/e2e" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-e2e-mail-config -f "$(CORE_FILE)" -f "$(E2E_FILE)" -f "$(E2E_MAIL_FILE)" --profile e2e config --quiet
	RUNNER_DEV_TOKEN_FILE=/dev/null $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" config --quiet

build: ## Build local frontend and backend images
	APP_PROJECT="$(APP_PROJECT)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" build

up: ## Start the core app, PostgreSQL, Redis, SeaweedFS, and Kafka without monitoring
	@test -f "$(ENV_FILE)" || { printf 'Missing %s. Copy .env.example to .env and replace the placeholder passwords.\n' "$(ENV_FILE)" >&2; exit 2; }
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" up -d

down: ## Stop core services and keep named volumes
	@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" down

logs: ## Follow core service logs
	@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" logs -f

monitoring-config: ## Render core monitoring, or the isolated demo with DEMO_MODE=isolated
	@if [ "$(DEMO_MODE)" = isolated ]; then \
		ENV_FILE="$(CHECK_ENV_FILE)" $(DEMO_MONITORING_SCRIPT_ENV) bash scripts/demo-monitoring.sh config; \
	elif [ -n "$(DEMO_MODE)" ]; then \
		printf 'Refusing monitoring config: DEMO_MODE must be isolated.\n' >&2; exit 2; \
	else \
		APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" config --quiet; \
	fi

monitoring-up: ## Start core monitoring, or the isolated demo with DEMO_MODE=isolated
	@if [ "$(DEMO_MODE)" = isolated ]; then \
		ENV_FILE="$(ENV_FILE)" $(DEMO_MONITORING_SCRIPT_ENV) bash scripts/demo-monitoring.sh up; \
	elif [ -n "$(DEMO_MODE)" ]; then \
		printf 'Refusing monitoring up: DEMO_MODE must be isolated.\n' >&2; exit 2; \
	else \
		test -f "$(ENV_FILE)" || { printf 'Missing %s. Copy .env.example to .env and replace the placeholder passwords.\n' "$(ENV_FILE)" >&2; exit 2; }; \
		APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" up -d; \
	fi

monitoring-down: ## Stop core monitoring, or the isolated demo with DEMO_MODE=isolated
	@if [ "$(DEMO_MODE)" = isolated ]; then \
		ENV_FILE="$(ENV_FILE)" $(DEMO_MONITORING_SCRIPT_ENV) bash scripts/demo-monitoring.sh down; \
	elif [ -n "$(DEMO_MODE)" ]; then \
		printf 'Refusing monitoring down: DEMO_MODE must be isolated.\n' >&2; exit 2; \
	else \
		test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }; \
		APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" down; \
	fi

monitoring-logs: ## Follow core monitoring logs, or the isolated demo with DEMO_MODE=isolated
	@if [ "$(DEMO_MODE)" = isolated ]; then \
		ENV_FILE="$(ENV_FILE)" $(DEMO_MONITORING_SCRIPT_ENV) bash scripts/demo-monitoring.sh logs; \
	elif [ -n "$(DEMO_MODE)" ]; then \
		printf 'Refusing monitoring logs: DEMO_MODE must be isolated.\n' >&2; exit 2; \
	else \
		test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }; \
		APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(MONITORING_PROJECT)" -f "$(MONITORING_FILE)" logs -f; \
	fi

ml-config: ## Render the separate ML Compose file
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" config --quiet

ml-build: ## Build the ML image without starting it
	APP_PROJECT="$(APP_PROJECT)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" build

ml-up: ## Start the ML service as a separate Compose project
	@test -f "$(ENV_FILE)" || { printf 'Missing %s. Copy .env.example to .env and replace the placeholder passwords.\n' "$(ENV_FILE)" >&2; exit 2; }
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" up -d

ml-down: ## Stop the ML service and keep named volumes
	@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" down

ml-logs: ## Follow ML service logs
	@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
	APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" logs -f

# DEVELOPMENT ONLY: the code runner in a privileged local container. See the
# header of docker-compose.runner.dev.yml; production runs the runner only on
# its dedicated VM.
runner-dev-config: ## Render the dev-only runner Compose file
	RUNNER_DEV_TOKEN_FILE=/dev/null $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" config --quiet

runner-dev-build: ## Build the runner image for the dev-only Compose project
	RUNNER_DEV_TOKEN_FILE=/dev/null $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" build

runner-dev-up: ## Start the dev-only privileged runner on 127.0.0.1 with a local random token
	@if [ ! -s "$(RUNNER_DEV_TOKEN_FILE)" ]; then \
		mkdir -p "$$(dirname "$(RUNNER_DEV_TOKEN_FILE)")"; \
		(umask 077 && python3 -c 'import secrets; print(secrets.token_urlsafe(48))' > "$(RUNNER_DEV_TOKEN_FILE)"); \
		printf 'Created the dev runner token file %s\n' "$(RUNNER_DEV_TOKEN_FILE)"; \
	fi
	RUNNER_DEV_TOKEN_FILE="$(RUNNER_DEV_TOKEN_FILE)" $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" up -d --build

runner-dev-down: ## Stop the dev-only runner
	RUNNER_DEV_TOKEN_FILE="$(RUNNER_DEV_TOKEN_FILE)" $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" down

runner-dev-logs: ## Follow dev-only runner logs
	RUNNER_DEV_TOKEN_FILE="$(RUNNER_DEV_TOKEN_FILE)" $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" logs -f

# Production code runner VM (deploy/labs/runner-vm.sh). The runner-vm-*
# targets run on the app host as a member of the docker and lxd groups,
# without sudo. Secrets and reports stay in RUNNER_VM_STATE_DIR (default
# ~/lincodex-runner), never in the repository. Settings come from the
# environment or the make command line, for example A16_BUNDLE=<path>.
.PHONY: runner-vm-up runner-vm-secrets runner-vm-rootfs runner-vm-create runner-vm-install runner-vm-a16 runner-vm-check
RUNNER_VM_SCRIPT := $(ROOT)/deploy/labs/runner-vm.sh

runner-vm-up: ## App host: secrets, runner file system, VM (once), install, and checks
	@bash "$(RUNNER_VM_SCRIPT)" up

runner-vm-secrets: ## App host: create the runner CA, server certificate, and token outside the repository
	@bash "$(RUNNER_VM_SCRIPT)" secrets

runner-vm-rootfs: ## App host: build runner/Dockerfile and export its file system for the VM
	@bash "$(RUNNER_VM_SCRIPT)" rootfs

runner-vm-create: ## App host: create and start the runner VM with its static address
	@bash "$(RUNNER_VM_SCRIPT)" create

runner-vm-install: ## App host: push the runner, its secrets, and the systemd unit into the VM and restart it
	@bash "$(RUNNER_VM_SCRIPT)" install

runner-vm-a16: ## App host: run the A16 suite in the runner VM (bundle from make -C runner a16-bundle)
	@bash "$(RUNNER_VM_SCRIPT)" a16

runner-vm-check: ## App host: check the VM, TLS and token, a harmless run, mounts, secrets, limits, and network denial
	@bash "$(RUNNER_VM_SCRIPT)" check

# Edge proxy (TLS and tunnel templates under deploy/edge). edge.env is local;
# the checked-in example is used when it does not exist.
.PHONY: edge-render edge-config
EDGE_DIR := $(ROOT)/deploy/edge
EDGE_ENV ?= $(if $(wildcard $(EDGE_DIR)/edge.env),$(EDGE_DIR)/edge.env,$(EDGE_DIR)/edge.env.example)
EDGE_NGINX_IMAGE ?= nginx:stable-alpine

edge-render: ## Render the edge nginx config from deploy/edge/edge.env into deploy/edge/rendered
	@bash "$(EDGE_DIR)/scripts/render.sh" --env "$(EDGE_ENV)" --out "$(EDGE_DIR)/rendered" nginx

edge-config: ## Render the edge templates and check the nginx config with nginx -t in a container
	@DOCKER="$(DOCKER)" EDGE_NGINX_IMAGE="$(EDGE_NGINX_IMAGE)" bash "$(EDGE_DIR)/scripts/check.sh" "$(EDGE_ENV)"

check: config check-workflows check-release check-perimeter check-e2e-mail check-demo check-runner-dev check-runner-vm ## Validate Compose files, CI workflow aggregates, release tooling, and network boundaries without starting services

check-workflows: ## Check that each workflow aggregate job requires every other job
	PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/scripts" -p 'test_workflow_aggregate.py'

check-release: ## Test the release manifest tooling, its secret scan, and the Go and base image pins
	PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_release_manifest.py'

check-perimeter: ## Check published ports, the app Nginx paths, limits, and the trusted proxy chain from the files
	PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_perimeter.py'

check-e2e-mail: ## Check that the mail fixture stays inside the explicit local E2E network
	PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_e2e_mail.py'

check-runner-dev: ## Check that the dev-only runner switches stay out of production files
	PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_runner_dev.py'

check-runner-vm: ## Check that the runner VM script keeps the VM boundary (no LXD needed)
	PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_runner_vm.py'

check-demo: ## Check the guards of the demo targets and the demo overlay without starting services
	PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_demo.py'

perimeter-test: ## Start throwaway Nginx containers and test denied paths, limits, headers, and forwarded headers over HTTP
	LINCODEX_PERIMETER_LIVE=1 DOCKER="$(DOCKER)" PERIMETER_NGINX_IMAGE="$(EDGE_NGINX_IMAGE)" PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -v -s "$(ROOT)/deploy/scripts" -p 'test_perimeter.py'

smoke: ## Start an isolated stack and check infrastructure health routes
	@bash scripts/smoke.sh

test-e2e: ## Run the Playwright suite against an isolated throwaway stack
	@bash scripts/e2e.sh

test-e2e-registration: ## Run browser registration tests with the local authenticated mail fixture
	@bash scripts/e2e.sh --mail-fixture

release-build: ## Build release images (core, demo seed, ML) with every base image pinned to its resolved digest
	@$(RELEASE) build --pull "$(RELEASE_PULL)"

release-smoke: ## Run the smoke against the release images and record passing evidence
	@LINCODEX_RELEASE_TAG="$(LINCODEX_RELEASE_TAG)" bash scripts/smoke.sh

release-e2e: ## Run the Playwright suite against the release images and record passing evidence
	@LINCODEX_RELEASE_TAG="$(LINCODEX_RELEASE_TAG)" bash scripts/e2e.sh

release-manifest: ## Publish a verified immutable manifest from local images and data versions
	@$(RELEASE) manifest $(if $(strip $(RELEASE_ID)),--release-id "$(RELEASE_ID)",) $(if $(strip $(RELEASE_DATA_MANIFEST)),--data-manifest "$(RELEASE_DATA_MANIFEST)",)

release-manifest-verify: ## Compare running containers of RELEASE_VERIFY_PROJECT with the manifest (or RELEASE_MANIFEST)
	@$(RELEASE) verify --project "$(RELEASE_VERIFY_PROJECT)" $(RELEASE_MANIFEST_ARG)

release-manifest-check: ## Check the manifest structure and scan it for secrets and machine data
	@$(RELEASE) check $(RELEASE_MANIFEST_ARG)

release-up: ## Restore the core stack from an immutable release manifest without building or pulling
	@test -f "$(ENV_FILE)" || { printf 'Missing %s. Copy .env.example to .env and replace the placeholder passwords.\n' "$(ENV_FILE)" >&2; exit 2; }
	@$(RELEASE) restore --action up --project "$(APP_PROJECT)" --env-file "$(ENV_FILE)" $(RELEASE_MANIFEST_ARG)

release-down: ## Stop the release project selected by the immutable manifest and keep named volumes
	@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
	@$(RELEASE) restore --action down --project "$(APP_PROJECT)" --env-file "$(ENV_FILE)" $(RELEASE_MANIFEST_ARG)

release-clean: ## Remove local release image references not retained by an immutable manifest
	@$(RELEASE) clean-images

demo-seed: ## Start the isolated demo stand when needed and seed its synthetic data (needs DEMO_MODE=isolated; RELEASE_MANIFEST or RELEASE_ID pins it to a release)
	@$(DEMO_SCRIPT_ENV) bash scripts/demo.sh seed

demo-reset: ## Discard only the isolated demo project when it holds no ordinary account, then seed it again (needs DEMO_MODE=isolated)
	@$(DEMO_SCRIPT_ENV) bash scripts/demo.sh reset

demo-down: ## Stop the isolated demo stand and keep its volumes (needs DEMO_MODE=isolated)
	@$(DEMO_SCRIPT_ENV) bash scripts/demo.sh down
