# kb-gateway Makefile
# Dependency management via uv; image build via docker.

IMAGE        ?= kb-gateway
TAG          ?= latest
NAME         ?= kb-gateway
PORT         ?= 8081
CONFIG       ?= config.yaml

# Image base + proxy for builds behind a corporate mirror. Override as needed.
PYTHON_IMAGE ?= hub.yangzhiqiang.tech/library/python:3.12-slim
HTTP_PROXY   ?= http://172.16.40.16:7280
HTTPS_PROXY  ?= http://172.16.40.16:7280

.DEFAULT_GOAL := help

.PHONY: help
help: ## Show this help
	@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
		| awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-14s\033[0m %s\n", $$1, $$2}'

.PHONY: install
install: ## Sync dependencies (incl. dev) into a local venv with uv
	uv sync --extra dev

.PHONY: run
run: ## Run the gateway locally (reload) using config.yaml
	KB_GATEWAY_CONFIG=$(CONFIG) uv run uvicorn gateway.asgi:app --host 0.0.0.0 --port $(PORT) --reload

.PHONY: test
test: ## Unit tests (pytest tests/unit)
	uv run pytest -q tests/unit

.PHONY: test-unit
test-unit: test ## Alias for `make test`

.PHONY: test-integration
test-integration: ## Live pytest vs gateway / Redis / MySQL (not run by CI)
	uv run pytest -q tests/integration --run-integration

.PHONY: test-all
test-all: ## Unit tests + collect integration (integration skipped unless RUN_INTEGRATION=1)
	uv run pytest -q $(if $(RUN_INTEGRATION),--run-integration,)

.PHONY: tests
tests: test ## Alias for `make test`

.PHONY: perf
perf: ## Mock-backend proxy bench -> docs/performance-report.md
	uv run python -m tests.perf.benchmark

.PHONY: perf-live
perf-live: ## Live GET /api/v1/datasets (gateway vs Ragflow) -> docs/performance-report/
	uv run python -m tests.perf.datasets

.PHONY: integration-perf
integration-perf: perf-live ## Alias for `make perf-live`

.PHONY: bench
bench: perf ## Alias for `make perf`

.PHONY: lint
lint: ## Lint with ruff
	uv run ruff check gateway tests

.PHONY: fmt
fmt: ## Format with ruff
	uv run ruff format gateway tests

.PHONY: build
docker: ## Build the docker image ($(IMAGE):$(TAG))
	docker build \
		--build-arg PYTHON_IMAGE=$(PYTHON_IMAGE) \
		--build-arg http_proxy=$(HTTP_PROXY) \
		--build-arg https_proxy=$(HTTPS_PROXY) \
		-t $(IMAGE):$(TAG) .

.PHONY: run
run: ## Run the image detached ($(NAME)), mounting config.yaml and injecting .env
	docker rm -f $(NAME) 2>/dev/null || true
	docker run -d --name $(NAME) -p $(PORT):8080 \
		--env-file .env \
		-e PYTHONPATH=/app \
		$(IMAGE):$(TAG)
	@echo "started $(NAME) → http://127.0.0.1:$(PORT)  (logs: make docker-logs)"

.PHONY: up
up: ## Copy local ./gateway into the container and restart (no image rebuild)
	@docker inspect $(NAME) >/dev/null 2>&1 || \
		{ echo "error: container $(NAME) not found; run 'make docker-run' first"; exit 1; }
	# The image installs the package into site-packages; ``kb-gateway`` loads from
	# there (sys.path[0] is .venv/bin), not /app/gateway. Sync both trees.
	docker cp $(PWD)/gateway/. $(NAME):/app/gateway/
	@SITE=$$(docker exec $(NAME) .venv/bin/python -c 'import site; print(site.getsitepackages()[0])') && \
		docker cp $(PWD)/gateway/. $(NAME):$$SITE/gateway/
	docker restart $(NAME)
	@echo "updated ./gateway (+ site-packages) in $(NAME) and restarted → http://127.0.0.1:$(PORT)  (logs: make docker-logs)"

.PHONY: logs
logs: ## Follow the running container's logs
	docker logs -f $(NAME) --tail 100

.PHONY: stop
stop: ## Stop and remove the running container
	docker stop $(NAME) 2>/dev/null || true

.PHONY: start
start: docker start kb-gateway

.PHONY: clean
clean: ## Remove caches and build artifacts
	rm -rf .pytest_cache .ruff_cache dist build *.egg-info
	find . -type d -name __pycache__ -prune -exec rm -rf {} +
