# DNS Management Makefile
# Manages CULTSCALE DNS zones via OctoDNS

.PHONY: help validate sync dump report status install

# Default target
help:
	@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
	@echo "  CULTSCALE DNS Management"
	@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
	@echo ""
	@echo "Available targets:"
	@echo ""
	@echo "  make validate    - Validate changes (dry-run, shows plan)"
	@echo "  make sync        - Apply changes to Cloudflare"
	@echo "  make dump        - Export Cloudflare state to YAML"
	@echo "  make report      - Show detailed zone information"
	@echo "  make status      - Quick status check"
	@echo "  make install     - Install OctoDNS dependencies"
	@echo ""
	@echo "Environment variables required:"
	@echo "  CLOUDFLARE_TOKEN - API token (Zone:Read, DNS:Edit)"
	@echo ""
	@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Check if CLOUDFLARE_TOKEN is set
check-token:
	@if [ -z "$$CLOUDFLARE_TOKEN" ]; then \
		echo "❌ Error: CLOUDFLARE_TOKEN not set"; \
		echo ""; \
		echo "Get token: https://dash.cloudflare.com/profile/api-tokens"; \
		echo "Run: export CLOUDFLARE_TOKEN=\"your_token\""; \
		exit 1; \
	fi

# Check if OctoDNS is installed
check-octodns:
	@if ! command -v octodns-sync >/dev/null 2>&1; then \
		echo "❌ OctoDNS not installed"; \
		echo "Run: make install"; \
		exit 1; \
	fi

# Install OctoDNS
install:
	@echo "Installing OctoDNS..."
	pip install --user octodns octodns-cloudflare
	@echo "✅ Installation complete"

# Validate DNS configuration (dry-run)
validate: check-token check-octodns
	@echo "Validating DNS configuration..."
	@if octodns-sync --config-file=config.yaml >/dev/null 2>&1; then \
		echo "✅ DNS validation passed - no errors"; \
		echo ""; \
		octodns-sync --config-file=config.yaml 2>&1 | awk '/^\* [a-z].*\.$$/{ zone=$$2 } /Summary:/ { print "  " zone " - " $$0 }' | sed 's/\*   Summary: //' || echo "  No changes planned"; \
	else \
		echo "❌ DNS validation failed:"; \
		octodns-sync --config-file=config.yaml 2>&1 | grep -E "(ERROR|ValidationError|WARNING)" | head -10; \
		exit 1; \
	fi

# Apply changes to Cloudflare
sync: check-token check-octodns
	@echo "Syncing DNS to Cloudflare..."
	@octodns-sync --config-file=config.yaml --doit 2>&1 | grep -E "(Summary:|total changes)" || true
	@echo "✅ DNS sync complete"

# Dump Cloudflare state to YAML (export)
dump: check-token check-octodns
	@octodns-dump --config-file=config.yaml --output-dir=zones --output-provider=config '*' cloudflare >/dev/null 2>&1 && echo "✅ Export complete"

# Show detailed zone report
report: check-token check-octodns
	@octodns-sync --config-file=config.yaml 2>&1 | grep "zone=" | sed 's/.*zone=//' | sed 's/\.$$//' | sort | uniq | awk '{print "  ✓ " $$1}'

# Quick status check
status: check-token check-octodns
	@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
	@echo "  DNS Management Status"
	@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
	@echo ""
	@echo "Configuration: config.yaml"
	@echo "Zones directory: zones/"
	@echo ""
	@echo "Configured zones:"
	@yq eval '.zones | keys | .[] | "  • " + sub("\\.$$", "")' config.yaml
	@echo ""
	@echo "Zone files present:"
	@ls -1 zones/*.yaml 2>/dev/null | xargs -n1 basename | sed 's/^/  ✓ /' || echo "  (none)"
	@echo ""
	@echo "✓ Token configured"
	@echo "✓ OctoDNS installed"
