GO       ?= go
BIN_DIR  ?= bin

.PHONY: all build test vet fmt tidy run clean seed check e2e

all: build

## build: compile the service and the CLI as static binaries
build:
	CGO_ENABLED=0 $(GO) build -trimpath -o $(BIN_DIR)/addrkitd ./cmd/addrkitd
	CGO_ENABLED=0 $(GO) build -trimpath -o $(BIN_DIR)/addrkit ./cmd/addrkit

## test: run the full test suite with the race detector
test:
	$(GO) test -race ./...

## vet: static analysis
vet:
	$(GO) vet ./...

## check: vet + race tests + architecture guard (the address core must not
## depend on any events/outages adapter)
check: vet test
	@if $(GO) list -deps ./cmd/addrkitd | grep -Eq 'domain/outages|adapters/arcgis'; then \
		echo "addrkitd must not depend on events/outages packages" >&2; \
		exit 1; \
	fi

## e2e: process-level tests against a real registry snapshot.
## Set ADDRKIT_E2E_REGISTRY to a Bulk NDJSON dump (and optionally
## ADDRKIT_E2E_LOG to an EДДС address log). Data is never copied into the repo.
e2e:
	@test -n "$(ADDRKIT_E2E_REGISTRY)" || { \
		echo "set ADDRKIT_E2E_REGISTRY=/path/to/address_registry.ndjson" >&2; \
		exit 1; \
	}
	$(GO) test -tags e2e -count=1 -v ./test/e2e

## fmt: format all Go sources
fmt:
	gofmt -l -w .

## tidy: sync go.mod/go.sum
tidy:
	$(GO) mod tidy

## run: build and start the service with the local config
run: build
	$(BIN_DIR)/addrkitd -config config.json

## seed: build registry.db from a registry dump (override IN=/OUT=/CITY=/CITIES=)
IN     ?= registry.ndjson
OUT    ?= registry.db
CITY   ?=
CITIES ?=
seed:
	$(BIN_DIR)/addrkit seed -in $(IN) -out $(OUT) \
		$(if $(CITY),-city "$(CITY)") $(if $(CITIES),-cities "$(CITIES)")

## clean: remove build artifacts
clean:
	rm -rf $(BIN_DIR)
