IMAGE   := ilsp-local
TAG     := dev
CONTAINER := ilsp-dev

PYTHON_PORT := 2087
BICEP_PORT  := 2088
HEALTH_PORT := 2089

# DevOpsMCP repo — source of fresh catalog JSON files
DEVOPS_MCP_REPO ?= $(HOME)/Projects/DevOpsMCP

.PHONY: build run stop restart logs shell test smoke clean help

## ── Build ────────────────────────────────────────────────────────────────────

build:                ## Build Docker image locally (no push)
	docker build -t $(IMAGE):$(TAG) .

rebuild:              ## Force rebuild (no cache)
	docker build --no-cache -t $(IMAGE):$(TAG) .

## ── Run ──────────────────────────────────────────────────────────────────────

run: stop             ## Build and start container (rebuilds image)
	$(MAKE) build
	docker run -d \
		--name $(CONTAINER) \
		-p $(PYTHON_PORT):$(PYTHON_PORT) \
		-p $(BICEP_PORT):$(BICEP_PORT) \
		-p $(HEALTH_PORT):$(HEALTH_PORT) \
		-e HTTP_PORT=$(HEALTH_PORT) \
		-e DEVOPS_MCP_URL=$(DEVOPS_MCP_URL) \
		-e PYTHONUNBUFFERED=1 \
		$(IMAGE):$(TAG)
	@echo ""
	@echo "  Container started — waiting for health check..."
	@sleep 5
	@$(MAKE) health

run-quick:            ## Start container without rebuilding image
	$(MAKE) stop
	docker run -d \
		--name $(CONTAINER) \
		-p $(PYTHON_PORT):$(PYTHON_PORT) \
		-p $(BICEP_PORT):$(BICEP_PORT) \
		-p $(HEALTH_PORT):$(HEALTH_PORT) \
		-e HTTP_PORT=$(HEALTH_PORT) \
		-e PYTHONUNBUFFERED=1 \
		$(IMAGE):$(TAG)
	@sleep 5
	@$(MAKE) health

run-with-data:        ## Start container with local data dir mounted (test volume path)
	$(MAKE) stop
	docker run -d \
		--name $(CONTAINER) \
		-p $(PYTHON_PORT):$(PYTHON_PORT) \
		-p $(BICEP_PORT):$(BICEP_PORT) \
		-p $(HEALTH_PORT):$(HEALTH_PORT) \
		-e HTTP_PORT=$(HEALTH_PORT) \
		-e PYTHONUNBUFFERED=1 \
		-v "$(DEVOPS_MCP_REPO):/data:ro" \
		$(IMAGE):$(TAG)
	@sleep 5
	@$(MAKE) health

stop:                 ## Stop and remove container (if running)
	-docker stop $(CONTAINER) 2>/dev/null
	-docker rm   $(CONTAINER) 2>/dev/null

restart: stop run-quick  ## Restart container without rebuilding

logs:                 ## Follow container logs
	docker logs -f $(CONTAINER)

logs-tail:            ## Show last 50 lines of logs
	docker logs --tail 50 $(CONTAINER)

shell:                ## Open bash inside running container
	docker exec -it $(CONTAINER) bash

## ── Health & Testing ─────────────────────────────────────────────────────────

health:               ## Check health endpoint (local container)
	@curl -sf http://localhost:$(HEALTH_PORT)/health | python3 -m json.tool \
		|| echo "  ✗ Health endpoint not reachable — is the container running? (make run)"

health-prod:          ## Check health endpoint (production lsp.i80.dk)
	@curl -sf https://lsp.i80.dk/health | python3 -m json.tool \
		|| echo "  ✗ lsp.i80.dk not reachable"

push-catalogs:        ## Push fresh Bicep catalogs to autobox + hot-reload iLSP
	bash scripts/push_catalogs.sh

smoke:                ## Run end-to-end smoke test against local container
	bash scripts/smoke_test.sh localhost

test:                 ## Run unit tests (local Python, no Docker needed)
	python3 -m pytest tests/ -v

## ── Status ───────────────────────────────────────────────────────────────────

ps:                   ## Show running container status
	@docker ps --filter name=$(CONTAINER) --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

ports:                ## Show port mappings
	@echo "  Python LSP : localhost:$(PYTHON_PORT)  (TCP)"
	@echo "  Bicep  LSP : localhost:$(BICEP_PORT)   (TCP)"
	@echo "  Health     : http://localhost:$(HEALTH_PORT)/health"

## ── Cleanup ──────────────────────────────────────────────────────────────────

clean: stop           ## Stop container and remove local image
	-docker rmi $(IMAGE):$(TAG) 2>/dev/null

## ── Help ─────────────────────────────────────────────────────────────────────

help:                 ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile \
		| awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-14s\033[0m %s\n", $$1, $$2}'
	@echo ""
	@echo "  Env vars:"
	@echo "    DEVOPS_MCP_URL  (default: $(DEVOPS_MCP_URL))"
	@echo ""
	@echo "  Examples:"
	@echo "    make run              # build + start"
	@echo "    make logs             # follow logs"
	@echo "    make smoke            # end-to-end test"
	@echo "    DEVOPS_MCP_URL=http://localhost:8000 make run  # point at local MCP"

.DEFAULT_GOAL := help
