QUADLET_MGR := ./scripts/quadlet/manage.sh
MODULES ?= core.tests
TOKEN_USER ?= m
STACK_ID_CLEAN := $(shell sid="$${GIA_STACK_ID:-$${STACK_ID:-}}"; sid=$$(printf "%s" "$$sid" | tr -cs 'a-zA-Z0-9._-' '-' | sed 's/^-*//; s/-*$$//'); printf "%s" "$$sid")
STACK_SUFFIX := $(if $(STACK_ID_CLEAN),_$(STACK_ID_CLEAN),)
APP_CONTAINER := gia$(STACK_SUFFIX)
LOCAL_LIBC := $(shell if ldd --version 2>&1 | head -n1 | tr '[:upper:]' '[:lower:]' | grep -q musl; then printf musl; else printf glibc; fi)
LOCAL_VENV := $(if $(filter musl,$(LOCAL_LIBC)),env,genv)
PRE_COMMIT_BIN := $(firstword $(wildcard $(LOCAL_VENV)/bin/pre-commit) $(wildcard genv/bin/pre-commit) $(wildcard env/bin/pre-commit))

run:
	bash $(QUADLET_MGR) up

build:
	OPERATION=uwsgi podman build --build-arg OPERATION=uwsgi -t localhost/xf/gia:prod -f Dockerfile .

stop:
	bash $(QUADLET_MGR) down

log:
	bash $(QUADLET_MGR) logs

status:
	bash $(QUADLET_MGR) status

quadlet-install:
	bash $(QUADLET_MGR) install

test:
	@if podman ps --format '{{.Names}}' | grep -qx "$(APP_CONTAINER)"; then \
		podman exec "$(APP_CONTAINER)" sh -lc "cd /code && . /venv/bin/activate && python manage.py test $(MODULES) -v 2"; \
	else \
		echo "Container '$(APP_CONTAINER)' is not running. Start the stack first with 'make run'." >&2; \
		exit 125; \
	fi

pre-commit:
	@if [ -x "$(PRE_COMMIT_BIN)" ]; then \
		"$(PRE_COMMIT_BIN)" run -a; \
	else \
		echo "No local pre-commit executable found in $(LOCAL_VENV)/bin, genv/bin, or env/bin." >&2; \
		exit 127; \
	fi

pre-commit-glibc:
	@if [ -x "$(PRE_COMMIT_BIN)" ]; then \
		echo "Using $(LOCAL_VENV) ($(LOCAL_LIBC))"; \
		"$(PRE_COMMIT_BIN)" run -a; \
	else \
		echo "No local pre-commit executable found in $(LOCAL_VENV)/bin, genv/bin, or env/bin." >&2; \
		exit 127; \
	fi

migrate:
	@if podman ps --format '{{.Names}}' | grep -qx "$(APP_CONTAINER)"; then \
		podman exec "$(APP_CONTAINER)" sh -lc "cd /code && . /venv/bin/activate && python manage.py migrate"; \
	else \
		echo "Container '$(APP_CONTAINER)' is not running. Start the stack first with 'make run'." >&2; \
		exit 125; \
	fi

makemigrations:
	@if podman ps --format '{{.Names}}' | grep -qx "$(APP_CONTAINER)"; then \
		podman exec "$(APP_CONTAINER)" sh -lc "cd /code && . /venv/bin/activate && python manage.py makemigrations"; \
	else \
		echo "Container '$(APP_CONTAINER)' is not running. Start the stack first with 'make run'." >&2; \
		exit 125; \
	fi

auth:
	@if podman ps --format '{{.Names}}' | grep -qx "$(APP_CONTAINER)"; then \
		podman exec "$(APP_CONTAINER)" sh -lc "cd /code && . /venv/bin/activate && python manage.py createsuperuser"; \
	else \
		echo "Container '$(APP_CONTAINER)' is not running. Start the stack first with 'make run'." >&2; \
		exit 125; \
	fi

token:
	@if podman ps --format '{{.Names}}' | grep -qx "$(APP_CONTAINER)"; then \
		podman exec "$(APP_CONTAINER)" sh -lc "cd /code && . /venv/bin/activate && python manage.py addstatictoken $(TOKEN_USER)"; \
	else \
		echo "Container '$(APP_CONTAINER)' is not running. Start the stack first with 'make run'." >&2; \
		exit 125; \
	fi

mcp-rust-build:
	cd rust/manticore-mcp-worker && cargo build --release
