run:
	.venv/bin/python flows/embedding_flow.py

web:
	.venv/bin/python -m uvicorn app.web.main:app --host 0.0.0.0 --port 8001 --reload

prod:
	.venv/bin/python -m uvicorn app.web.main:app --host 0.0.0.0 --port 8000

demo:
	.venv/bin/python -m uvicorn app.demo.main:app --host 0.0.0.0 --port 8010 --reload

compile:
	uv pip compile pyproject.toml -o requirements-frozen.txt

# ------------------------------------------------------------------------
# systemd user services. User-scope, no sudo needed.
#   worker-*  — Prefect worker for the embedding_flow deployment (make run)
#   web-*     — uvicorn web app on :8000 (make prod)
# ------------------------------------------------------------------------

SERVICE_DIR := $(HOME)/.config/systemd/user

WORKER_NAME := dr-sandbox-worker.service
WORKER_TMPL := deploy/dr-sandbox-worker.service.in
WORKER_PATH := $(SERVICE_DIR)/$(WORKER_NAME)

WEB_NAME := dr-sandbox-web.service
WEB_TMPL := deploy/dr-sandbox-web.service.in
WEB_PATH := $(SERVICE_DIR)/$(WEB_NAME)

define install_service
	@mkdir -p $(SERVICE_DIR)
	sed 's|@ROOT@|$(CURDIR)|g' $(1) > $(2)
	systemctl --user daemon-reload
	systemctl --user enable --now $(3)
	systemctl --user restart $(3)
	@echo
	@echo "installed $(3) -> $(2)"
	@echo "  status:  make $(4)-status"
	@echo "  logs:    make $(4)-logs"
	@loginctl show-user $$USER 2>/dev/null | grep -q "Linger=yes" || \
	  echo "  linger:  sudo loginctl enable-linger $$USER   # to survive logout / reboot"
endef

.PHONY: worker-install worker-uninstall worker-start worker-stop worker-restart worker-status worker-logs
.PHONY: web-install web-uninstall web-start web-stop web-restart web-status web-logs

worker-install:
	$(call install_service,$(WORKER_TMPL),$(WORKER_PATH),$(WORKER_NAME),worker)

worker-uninstall:
	-systemctl --user disable --now $(WORKER_NAME)
	-rm -f $(WORKER_PATH)
	-systemctl --user daemon-reload
	@echo "removed $(WORKER_NAME)"

worker-start:    ; systemctl --user start    $(WORKER_NAME)
worker-stop:     ; systemctl --user stop     $(WORKER_NAME)
worker-restart:  ; systemctl --user restart  $(WORKER_NAME)
worker-status:   ; systemctl --user status   $(WORKER_NAME)
worker-logs:     ; journalctl --user -u      $(WORKER_NAME) -f

web-install:
	$(call install_service,$(WEB_TMPL),$(WEB_PATH),$(WEB_NAME),web)

web-uninstall:
	-systemctl --user disable --now $(WEB_NAME)
	-rm -f $(WEB_PATH)
	-systemctl --user daemon-reload
	@echo "removed $(WEB_NAME)"

web-start:    ; systemctl --user start    $(WEB_NAME)
web-stop:     ; systemctl --user stop     $(WEB_NAME)
web-restart:  ; systemctl --user restart  $(WEB_NAME)
web-status:   ; systemctl --user status   $(WEB_NAME)
web-logs:     ; journalctl --user -u      $(WEB_NAME) -f
