SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c

# A temporary EKS cluster for poc/quickstart.py. Create it, run the script, destroy it.
KIT         := $(notdir $(CURDIR))
PROJECT     := field-kit-$(KIT)
TF          := terraform -chdir=poc/terraform
PROFILE     ?= supernova
REGION      ?= us-east-1
COST_CENTER ?= poc
# The cluster API endpoint only accepts these CIDRs. Defaults to every public address this machine
# is seen leaving from across 30 requests, because a corporate network can rotate through several.
ADMIN_CIDRS ?= $(shell for i in $$(seq 1 30); do curl -s -4 -m 5 https://checkip.amazonaws.com; done | sort -u | sed 's|$$|/32|' | jq -R . | jq -sc .)
PY          ?= python3

export AWS_PROFILE := $(PROFILE)
export AWS_REGION  := $(REGION)
export TF_VAR_project_name := $(PROJECT)
export TF_VAR_cost_center  := $(COST_CENTER)
export TF_VAR_admin_cidrs  := $(ADMIN_CIDRS)

.DEFAULT_GOAL := help
.PHONY: help preflight deploy verify screenshots destroy

help: ## Show targets
	@grep -E '^[a-zA-Z0-9_-]+:.*?## ' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  %-12s %s\n", $$1, $$2}'
	@echo "  The cluster costs about \$$0.19 per hour (control plane, one t3.medium, NAT gateway). Destroy it when done."

preflight: ## Check credentials and tools
	@out=$$(aws sts get-caller-identity --query Arn --output text 2>&1) || { echo "AWS profile $(PROFILE) has no valid credentials."; exit 1; }; \
	  echo "AWS identity: $$(echo "$$out" | sed -E 's/[0-9]{12}/<account>/')"
	@for t in terraform aws python3 curl; do command -v $$t >/dev/null || { echo "missing tool: $$t"; exit 1; }; done
	@echo 'Cluster API endpoint will accept: $(ADMIN_CIDRS)'

deploy: preflight ## Create the cluster (about 15 minutes, run in background)
	$(TF) init -input=false
	$(TF) apply -input=false -auto-approve

verify: ## Run the POC script against the cluster
	CLUSTER=$(PROJECT) poc/verify.sh

screenshots: ## Capture docs/screenshots/01-quickstart.png from a run of the POC script
	CLUSTER=$(PROJECT) poc/screenshots.sh

destroy: preflight ## Remove the cluster and list leftovers (each list should be empty)
	$(TF) init -input=false
	@$(TF) apply -destroy -input=false -auto-approve || { echo "destroy failed; retrying once"; $(TF) apply -destroy -input=false -auto-approve; }
	@echo "== Leftover check for tag Project=$(PROJECT) (KMS keys pending deletion are expected)"
	@aws resourcegroupstaggingapi get-resources --tag-filters Key=Project,Values=$(PROJECT) \
	  --query 'ResourceTagMappingList[].ResourceARN' --output text | tr '\t' '\n' | grep -v ':kms:' || true
	@echo "-- clusters:"; aws eks list-clusters --query "clusters[?@=='$(PROJECT)']" --output text
	@echo "-- log groups:"; for p in /$(PROJECT)/ /aws/eks/$(PROJECT)/; do aws logs describe-log-groups --log-group-name-prefix $$p --query 'logGroups[].logGroupName' --output text; done
	@echo "-- elastic IPs:"; aws ec2 describe-addresses --filters Name=tag:Project,Values=$(PROJECT) --query 'Addresses[].AllocationId' --output text
	@echo "-- terraform state entries: $$($(TF) state list | wc -l | tr -d ' ')"
