ifneq (,$(wildcard ./.env))
include .env
export
endif

PORT ?= 8080
HOST ?= 0.0.0.0

CONTAINER_NAME ?= va_llm

.PHONY: ensure-env install run start stop restart test smoke docker-build llm-up llm-down llm-status

ensure-env:
	@if [ ! -f .env ] && [ -f .env.example ]; then \
		cp .env.example .env; \
		echo "Created .env from .env.example"; \
	fi

install: ensure-env
	python3 -m venv .venv
	. .venv/bin/activate && pip install -U pip && pip install -e .[test]

run: ensure-env
	. .venv/bin/activate && uvicorn app.main:app --host $(HOST) --port $(PORT) --reload

test: ensure-env
	. .venv/bin/activate && pytest tests/

# Echter End-to-End-Test gegen OpenRouter (macht Netz-Aufrufe, kostet wenig).
smoke: ensure-env
	. .venv/bin/activate && python scripts/smoke_e2e.py

docker-build:
	docker build -t voice-assistant-gateway .

# --- Komplett-Befehle (Start / Stop / Restart) --------------------------------

# Stoppt alle Voice-Assistant-Komponenten: Gateway (Vordergrund + systemd) und
# llama.cpp-Container. Ollama (systemd) separat: sudo systemctl stop ollama
stop:
	-pkill -f "uvicorn app.main:app" 2>/dev/null; true
	-systemctl --user stop voice-assistant 2>/dev/null; true
	-docker rm -f "$(CONTAINER_NAME)" 2>/dev/null; true
	@echo "[*] Voice-Assistant gestoppt (Gateway + llama.cpp)."

# Startet llama.cpp-LLM-Server und danach das Gateway (Profil hybrid/local-dev).
# Fuer Profil cloud (kein lokales LLM): einfach 'make run'.
start: ensure-env
	$(MAKE) llm-up
	$(MAKE) run

# Faehrt alles herunter und startet neu (llama.cpp + Gateway).
restart: stop
	$(MAKE) start

# --- Lokales LLM (llama.cpp-Server, zentrale unzensierte KI) -----------------
# Konfig per ENV ueberschreibbar, z. B.:  GPU_DEVICE=2 HOST_PORT=8101 make llm-up
llm-up:
	bash scripts/llm-server/start-llm-server.sh

llm-down:
	bash scripts/llm-server/stop-llm-server.sh

llm-status:
	bash scripts/llm-server/status-llm-server.sh
