.PHONY: dev dev-down build test test-cover lint fmt proto migrate-up migrate-down health check docs load-test deploy-blue deploy-green deploy-rollback

# ─────────────────────────────────────────────
# Local development
# ─────────────────────────────────────────────

## dev: Start the full local stack (proxy + PostgreSQL + ClickHouse + Redis + Keycloak + PII)
dev:
	docker compose up --build

## dev-down: Stop and remove all containers and volumes
dev-down:
	docker compose down -v

## dev-logs: Tail logs from all services
dev-logs:
	docker compose logs -f

# ─────────────────────────────────────────────
# Go
# ─────────────────────────────────────────────

## build: Compile the Go proxy binary to bin/proxy
build:
	@mkdir -p bin
	go build -o bin/proxy ./cmd/proxy/

## test: Run all Go tests with race detector
test:
	go test -race ./...

## test-cover: Run tests with HTML coverage report
test-cover:
	go test -race -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report: coverage.html"

## lint: Run golangci-lint (Go) and black --check (Python)
lint:
	golangci-lint run
	black --check services/pii/
	ruff check services/pii/

## fmt: Auto-format Go and Python code
fmt:
	gofmt -w .
	black services/pii/

# ─────────────────────────────────────────────
# Proto (requires: brew install buf)
# ─────────────────────────────────────────────

## proto: Generate gRPC stubs for Go (gen/) and Python (services/pii/gen/)
proto:
	buf generate

## proto-lint: Lint the proto definitions
proto-lint:
	buf lint

# ─────────────────────────────────────────────
# Database migrations (requires: brew install golang-migrate)
# ─────────────────────────────────────────────

DB_URL ?= postgres://veylant:veylant_dev@localhost:5432/veylant?sslmode=disable

## migrate-up: Apply all pending migrations
migrate-up:
	migrate -path migrations -database "$(DB_URL)" up

## migrate-down: Roll back the last migration
migrate-down:
	migrate -path migrations -database "$(DB_URL)" down 1

## migrate-status: Show migration status
migrate-status:
	migrate -path migrations -database "$(DB_URL)" version

# ─────────────────────────────────────────────
# Checks & utilities
# ─────────────────────────────────────────────

## docs: Open the API documentation in the browser (proxy must be running)
docs:
	@echo "API docs available at http://localhost:8090/docs"
	@echo "OpenAPI spec:        http://localhost:8090/docs/openapi.yaml"
	@open http://localhost:8090/docs 2>/dev/null || xdg-open http://localhost:8090/docs 2>/dev/null || true

## health: Check the proxy health endpoint
health:
	@curl -sf http://localhost:8090/healthz | python3 -m json.tool

## check: Run build + vet + lint + test (full pre-commit check)
check: build
	go vet ./...
	golangci-lint run
	go test -race ./...

## test-integration: Run integration tests (requires Docker)
test-integration:
	go test -tags integration -v -timeout 10m ./test/integration/...

# ─────────────────────────────────────────────
# Helm (requires: helm)
# ─────────────────────────────────────────────

## helm-dry-run: Render Helm templates without deploying
helm-dry-run:
	helm template veylant-proxy deploy/helm/veylant-proxy

## helm-deploy: Deploy to staging (requires KUBECONFIG and IMAGE_TAG env vars)
helm-deploy:
	helm upgrade --install veylant-proxy deploy/helm/veylant-proxy \
		--namespace veylant \
		--create-namespace \
		--set image.tag=$(IMAGE_TAG) \
		--wait --timeout 5m

# ─────────────────────────────────────────────
# Load tests (requires: brew install k6)
# ─────────────────────────────────────────────

SCENARIO ?= smoke
VEYLANT_URL ?= http://localhost:8090
VEYLANT_TOKEN ?= dev-token

## load-test: Run k6 load tests (SCENARIO=smoke|load|stress|soak, default: smoke)
load-test:
	k6 run \
		--env VEYLANT_URL=$(VEYLANT_URL) \
		--env VEYLANT_TOKEN=$(VEYLANT_TOKEN) \
		--env SCENARIO=$(SCENARIO) \
		test/k6/k6-load-test.js

# ─────────────────────────────────────────────
# Blue/Green deployment (requires: kubectl + helm + Istio)
# ─────────────────────────────────────────────

NAMESPACE ?= veylant
ACTIVE_SLOT ?= blue

## deploy-blue: Deploy IMAGE_TAG to the blue slot
deploy-blue:
	IMAGE_TAG=$(IMAGE_TAG) NAMESPACE=$(NAMESPACE) ACTIVE_SLOT=green \
		./deploy/scripts/blue-green.sh

## deploy-green: Deploy IMAGE_TAG to the green slot
deploy-green:
	IMAGE_TAG=$(IMAGE_TAG) NAMESPACE=$(NAMESPACE) ACTIVE_SLOT=blue \
		./deploy/scripts/blue-green.sh

## deploy-rollback: Roll back to the previous active slot
deploy-rollback:
	@echo "Rolling back: switching traffic back to $(ACTIVE_SLOT)..."
	kubectl patch virtualservice veylant-proxy -n $(NAMESPACE) --type merge \
		-p '{"spec":{"http":[{"route":[{"destination":{"host":"veylant-proxy","subset":"$(ACTIVE_SLOT)"},"weight":100}]}]}}'
	@echo "Rollback complete. Active slot: $(ACTIVE_SLOT)"

## help: Show this help message
help:
	@grep -E '^## ' Makefile | sed 's/## /  /'
