TEST?=./...

.PHONY: test
test:
	@echo Running tests...
	go test -v $(TEST)

# Gateway-focused E2E tests assume gateway and nodes are already running
# Auto-discovers configuration from ~/.orama and queries database for API key
# No environment variables required
.PHONY: test-e2e test-e2e-deployments test-e2e-fullstack test-e2e-https test-e2e-quick test-e2e-local test-e2e-prod test-e2e-shared test-e2e-cluster test-e2e-integration test-e2e-production

# Check if gateway is running (helper)
.PHONY: check-gateway
check-gateway:
	@if ! curl -sf http://localhost:6001/v1/health > /dev/null 2>&1; then \
		echo "❌ Gateway not running on localhost:6001"; \
		echo ""; \
		echo "To run tests locally:"; \
		echo "  1. Start the dev environment: make dev"; \
		echo "  2. Wait for all services to start (~30 seconds)"; \
		echo "  3. Run tests: make test-e2e-local"; \
		echo ""; \
		echo "To run tests against production:"; \
		echo "  ORAMA_GATEWAY_URL=https://dbrs.space make test-e2e"; \
		exit 1; \
	fi
	@echo "✅ Gateway is running"

# Local E2E tests - checks gateway first
test-e2e-local: check-gateway
	@echo "Running E2E tests against local dev environment..."
	go test -v -tags e2e -timeout 30m ./e2e/...

# Production E2E tests - includes production-only tests
test-e2e-prod:
	@if [ -z "$$ORAMA_GATEWAY_URL" ]; then \
		echo "❌ ORAMA_GATEWAY_URL not set"; \
		echo "Usage: ORAMA_GATEWAY_URL=https://dbrs.space make test-e2e-prod"; \
		exit 1; \
	fi
	@echo "Running E2E tests (including production-only) against $$ORAMA_GATEWAY_URL..."
	go test -v -tags "e2e production" -timeout 30m ./e2e/...

# Generic e2e target (works with both local and production)
test-e2e:
	@echo "Running comprehensive E2E tests..."
	@echo "Auto-discovering configuration from ~/.orama..."
	@echo "Tip: Use 'make test-e2e-local' for local or 'make test-e2e-prod' for production"
	go test -v -tags e2e -timeout 30m ./e2e/...

test-e2e-deployments:
	@echo "Running deployment E2E tests..."
	go test -v -tags e2e -timeout 15m ./e2e/deployments/...

test-e2e-fullstack:
	@echo "Running fullstack E2E tests..."
	go test -v -tags e2e -timeout 20m -run "TestFullStack" ./e2e/...

test-e2e-https:
	@echo "Running HTTPS/external access E2E tests..."
	go test -v -tags e2e -timeout 10m -run "TestHTTPS" ./e2e/...

test-e2e-shared:
	@echo "Running shared E2E tests..."
	go test -v -tags e2e -timeout 10m ./e2e/shared/...

test-e2e-cluster:
	@echo "Running cluster E2E tests..."
	go test -v -tags e2e -timeout 15m ./e2e/cluster/...

test-e2e-integration:
	@echo "Running integration E2E tests..."
	go test -v -tags e2e -timeout 20m ./e2e/integration/...

test-e2e-production:
	@echo "Running production-only E2E tests..."
	go test -v -tags "e2e production" -timeout 15m ./e2e/production/...

test-e2e-quick:
	@echo "Running quick E2E smoke tests..."
	go test -v -tags e2e -timeout 5m -run "TestStatic|TestHealth" ./e2e/...

# Network - Distributed P2P Database System
# Makefile for development and build tasks

.PHONY: build clean test run-node run-node2 run-node3 run-example deps tidy fmt vet lint clear-ports install-hooks kill

VERSION := 0.100.0
COMMIT  ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
DATE    ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS := -X 'main.version=$(VERSION)' -X 'main.commit=$(COMMIT)' -X 'main.date=$(DATE)'

# Build targets
build: deps
	@echo "Building network executables (version=$(VERSION))..."
	@mkdir -p bin
	go build -ldflags "$(LDFLAGS)" -o bin/identity ./cmd/identity
	go build -ldflags "$(LDFLAGS)" -o bin/orama-node ./cmd/node
	go build -ldflags "$(LDFLAGS)" -o bin/orama cmd/cli/main.go
	go build -ldflags "$(LDFLAGS)" -o bin/rqlite-mcp ./cmd/rqlite-mcp
	# Inject gateway build metadata via pkg path variables
	go build -ldflags "$(LDFLAGS) -X 'github.com/DeBrosOfficial/network/pkg/gateway.BuildVersion=$(VERSION)' -X 'github.com/DeBrosOfficial/network/pkg/gateway.BuildCommit=$(COMMIT)' -X 'github.com/DeBrosOfficial/network/pkg/gateway.BuildTime=$(DATE)'" -o bin/gateway ./cmd/gateway
	@echo "Build complete! Run ./bin/orama version"

# Cross-compile all binaries for Linux (used with --pre-built flag on VPS)
# Builds: DeBros binaries + Olric + CoreDNS (with rqlite plugin) + Caddy (with orama DNS module)
build-linux: deps
	@echo "Cross-compiling all binaries for linux/amd64 (version=$(VERSION))..."
	@mkdir -p bin-linux
	GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o bin-linux/identity ./cmd/identity
	GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o bin-linux/orama-node ./cmd/node
	GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o bin-linux/orama cmd/cli/main.go
	GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o bin-linux/rqlite-mcp ./cmd/rqlite-mcp
	GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS) -X 'github.com/DeBrosOfficial/network/pkg/gateway.BuildVersion=$(VERSION)' -X 'github.com/DeBrosOfficial/network/pkg/gateway.BuildCommit=$(COMMIT)' -X 'github.com/DeBrosOfficial/network/pkg/gateway.BuildTime=$(DATE)'" -o bin-linux/gateway ./cmd/gateway
	GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o bin-linux/orama-cli ./cmd/cli
	@echo "Installing Olric for linux/amd64..."
	GOOS=linux GOARCH=amd64 GOBIN=$(CURDIR)/bin-linux go install github.com/olric-data/olric/cmd/olric-server@v0.7.0
	@echo "✓ All Linux binaries built in bin-linux/"
	@echo ""
	@echo "Next steps:"
	@echo "  1. Build CoreDNS:  make build-linux-coredns"
	@echo "  2. Build Caddy:    make build-linux-caddy"
	@echo "  3. Or build all:   make build-linux-all"

# Build CoreDNS with rqlite plugin for Linux
build-linux-coredns:
	@echo "Building CoreDNS with rqlite plugin for linux/amd64..."
	@rm -rf /tmp/coredns-build-linux
	@mkdir -p /tmp/coredns-build-linux
	git clone --depth 1 --branch v1.12.0 https://github.com/coredns/coredns.git /tmp/coredns-build-linux
	@mkdir -p /tmp/coredns-build-linux/plugin/rqlite
	@cp pkg/coredns/rqlite/*.go /tmp/coredns-build-linux/plugin/rqlite/
	@echo 'metadata:metadata\ncancel:cancel\ntls:tls\nreload:reload\nnsid:nsid\nbufsize:bufsize\nroot:root\nbind:bind\ndebug:debug\ntrace:trace\nready:ready\nhealth:health\npprof:pprof\nprometheus:metrics\nerrors:errors\nlog:log\ndnstap:dnstap\nlocal:local\ndns64:dns64\nacl:acl\nany:any\nchaos:chaos\nloadbalance:loadbalance\ncache:cache\nrewrite:rewrite\nheader:header\ndnssec:dnssec\nautopath:autopath\nminimal:minimal\ntemplate:template\ntransfer:transfer\nhosts:hosts\nfile:file\nauto:auto\nsecondary:secondary\nloop:loop\nforward:forward\ngrpc:grpc\nerratic:erratic\nwhoami:whoami\non:github.com/coredns/caddy/onevent\nsign:sign\nview:view\nrqlite:rqlite' > /tmp/coredns-build-linux/plugin.cfg
	cd /tmp/coredns-build-linux && go get github.com/miekg/dns@latest && go get go.uber.org/zap@latest && go mod tidy && go generate && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o coredns
	@cp /tmp/coredns-build-linux/coredns bin-linux/coredns
	@rm -rf /tmp/coredns-build-linux
	@echo "✓ CoreDNS built: bin-linux/coredns"

# Build Caddy with orama DNS module for Linux
build-linux-caddy:
	@echo "Building Caddy with orama DNS module for linux/amd64..."
	@which xcaddy > /dev/null 2>&1 || (echo "Installing xcaddy..." && go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest)
	@rm -rf /tmp/caddy-build-linux
	@mkdir -p /tmp/caddy-build-linux/caddy-dns-orama
	@# Generate the module source
	@echo 'module github.com/DeBrosOfficial/caddy-dns-orama\n\ngo 1.22\n\nrequire (\n\tgithub.com/caddyserver/caddy/v2 v2.10.2\n\tgithub.com/libdns/libdns v1.1.0\n)' > /tmp/caddy-build-linux/caddy-dns-orama/go.mod
	@cp pkg/caddy/dns/orama/provider.go /tmp/caddy-build-linux/caddy-dns-orama/ 2>/dev/null || true
	cd /tmp/caddy-build-linux/caddy-dns-orama && go mod tidy
	GOOS=linux GOARCH=amd64 xcaddy build v2.10.2 --with github.com/DeBrosOfficial/caddy-dns-orama=/tmp/caddy-build-linux/caddy-dns-orama --output bin-linux/caddy
	@rm -rf /tmp/caddy-build-linux
	@echo "✓ Caddy built: bin-linux/caddy"

# Build everything for Linux (all binaries + CoreDNS + Caddy)
build-linux-all: build-linux build-linux-coredns build-linux-caddy
	@echo ""
	@echo "✅ All Linux binaries ready in bin-linux/:"
	@ls -la bin-linux/
	@echo ""
	@echo "Deploy to VPS:"
	@echo "  scp bin-linux/* ubuntu@<ip>:/home/debros/bin/"
	@echo "  scp bin-linux/coredns ubuntu@<ip>:/usr/local/bin/coredns"
	@echo "  scp bin-linux/caddy ubuntu@<ip>:/usr/bin/caddy"
	@echo "  sudo orama install --pre-built --no-pull ..."

# Install git hooks
install-hooks:
	@echo "Installing git hooks..."
	@bash scripts/install-hooks.sh

# Clean build artifacts
clean:
	@echo "Cleaning build artifacts..."
	rm -rf bin/
	rm -rf data/
	@echo "Clean complete!"

# Run bootstrap node (auto-selects identity and data dir)
run-node:
	@echo "Starting node..."
	@echo "Config: ~/.orama/node.yaml"
	go run ./cmd/orama-node --config node.yaml

# Run second node - requires join address
run-node2:
	@echo "Starting second node..."
	@echo "Config: ~/.orama/node2.yaml"
	go run ./cmd/orama-node --config node2.yaml

# Run third node - requires join address
run-node3:
	@echo "Starting third node..."
	@echo "Config: ~/.orama/node3.yaml"
	go run ./cmd/orama-node --config node3.yaml

# Run gateway HTTP server
run-gateway:
	@echo "Starting gateway HTTP server..."
	@echo "Note: Config must be in ~/.orama/data/gateway.yaml"
	go run ./cmd/orama-gateway

# Development environment target
# Uses orama dev up to start full stack with dependency and port checking
dev: build
	@./bin/orama dev up

# Graceful shutdown of all dev services
stop:
	@if [ -f ./bin/orama ]; then \
		./bin/orama dev down || true; \
	fi
	@bash scripts/dev-kill-all.sh

# Force kill all processes (immediate termination)
kill:
	@bash scripts/dev-kill-all.sh

# Help
help:
	@echo "Available targets:"
	@echo "  build         - Build all executables"
	@echo "  clean         - Clean build artifacts"
	@echo "  test          - Run unit tests"
	@echo ""
	@echo "Local Development (Recommended):"
	@echo "  make dev      - Start full development stack with one command"
	@echo "                 - Checks dependencies and available ports"
	@echo "                 - Generates configs and starts all services"
	@echo "                 - Validates cluster health"
	@echo "  make stop     - Gracefully stop all development services"
	@echo "  make kill     - Force kill all development services (use if stop fails)"
	@echo ""
	@echo "E2E Testing:"
	@echo "  make test-e2e-local       - Run E2E tests against local dev (checks gateway first)"
	@echo "  make test-e2e-prod        - Run all E2E tests incl. production-only (needs ORAMA_GATEWAY_URL)"
	@echo "  make test-e2e-shared      - Run shared E2E tests (cache, storage, pubsub, auth)"
	@echo "  make test-e2e-cluster     - Run cluster E2E tests (libp2p, olric, rqlite, namespace)"
	@echo "  make test-e2e-integration - Run integration E2E tests (fullstack, persistence, concurrency)"
	@echo "  make test-e2e-deployments - Run deployment E2E tests"
	@echo "  make test-e2e-production  - Run production-only E2E tests (DNS, HTTPS, cross-node)"
	@echo "  make test-e2e-quick       - Quick smoke tests (static deploys, health checks)"
	@echo "  make test-e2e             - Generic E2E tests (auto-discovers config)"
	@echo ""
	@echo "  Example production test:"
	@echo "    ORAMA_GATEWAY_URL=https://dbrs.space make test-e2e-prod"
	@echo ""
	@echo "Development Management (via orama):"
	@echo "  ./bin/orama dev status  - Show status of all dev services"
	@echo "  ./bin/orama dev logs <component> [--follow]"
	@echo ""
	@echo "Individual Node Targets (advanced):"
	@echo "  run-node      - Start first node directly"
	@echo "  run-node2     - Start second node directly"
	@echo "  run-node3     - Start third node directly"
	@echo "  run-gateway   - Start HTTP gateway directly"
	@echo ""
	@echo "Maintenance:"
	@echo "  deps          - Download dependencies"
	@echo "  tidy          - Tidy dependencies"
	@echo "  fmt           - Format code"
	@echo "  vet           - Vet code"
	@echo "  lint          - Lint code (fmt + vet)"
	@echo "  help          - Show this help"
