# Capability Discovery System
# Automatically discovers and includes capability Makefiles

# Find all capability directories that contain Makefiles
CAPABILITY_DIRS := $(dir $(wildcard capabilities/*/Makefile))
CAPABILITY_MAKEFILES := $(foreach dir,$(CAPABILITY_DIRS),$(dir)Makefile)

# DO NOT include capability Makefiles directly to avoid target conflicts
# Instead, use delegation patterns below for accessing capability targets

# Capability discovery and delegation system
.PHONY: capabilities-list
capabilities-list: ## List all available capabilities
	@echo "🧩 Available Capabilities:"
	@echo "=========================="
	@for dir in $(CAPABILITY_DIRS); do \
		if [ -f "$$dir/Makefile" ]; then \
			echo ""; \
			capability=$$(basename $$dir); \
			echo "📦 $$capability"; \
			echo "   Location: $$dir"; \
			if [ -f "$$dir/README.md" ]; then \
				desc=$$(head -5 "$$dir/README.md" | grep -E "^[A-Z].*" | head -1 || echo "No description"); \
				echo "   Description: $$desc"; \
			fi; \
			echo "   Makefile: Available"; \
		fi; \
	done
	@echo ""
	@echo "Use 'make capabilities-help' to see all capability targets"

.PHONY: capabilities-help
capabilities-help: ## Show help for all capabilities
	@echo "🧩 All Capability Targets:"
	@echo "=========================="
	@for dir in $(CAPABILITY_DIRS); do \
		if [ -f "$$dir/Makefile" ]; then \
			echo ""; \
			capability=$$(basename $$dir); \
			echo "📦 $$capability capability:"; \
			echo ""; \
			$(MAKE) -C "$$dir" help 2>/dev/null | grep -E "^  " || echo "   No help available"; \
		fi; \
	done

.PHONY: capabilities-install
capabilities-install: ## Install all capabilities
	@echo "🔧 Installing all capabilities..."
	@for dir in $(CAPABILITY_DIRS); do \
		if [ -f "$$dir/pyproject.toml" ]; then \
			capability=$$(basename $$dir); \
			echo "Installing $$capability..."; \
			pip install -e "$$dir" || echo "Failed to install $$capability"; \
		fi; \
	done

.PHONY: capabilities-test
capabilities-test: ## Run tests for all capabilities
	@echo "🧪 Testing all capabilities..."
	@for dir in $(CAPABILITY_DIRS); do \
		if [ -f "$$dir/Makefile" ]; then \
			capability=$$(basename $$dir); \
			echo ""; \
			echo "Testing $$capability..."; \
			$(MAKE) -C "$$dir" test 2>/dev/null || echo "No tests or test failed for $$capability"; \
		fi; \
	done

# Create delegation targets for common patterns
# This allows calling capability targets directly from main makefile

# Release management delegation (special handling since it's commonly used)
.PHONY: release-%
release-%:
	@if [ -f "capabilities/release-management/Makefile" ]; then \
		$(MAKE) -C capabilities/release-management $@; \
	else \
		echo "❌ Release management capability not found"; \
		exit 1; \
	fi

# Generic capability delegation pattern
# Format: capability-name-target-name
define CAPABILITY_DELEGATION
.PHONY: $(1)-%
$(1)-%:
	@if [ -f "capabilities/$(1)/Makefile" ]; then \
		target_name=$$$$(echo "$$@" | sed 's/$(1)-//'); \
		$(MAKE) -C "capabilities/$(1)" "$$$$target_name" || \
		$(MAKE) -C "capabilities/$(1)" "$(1)-$$$$target_name" 2>/dev/null || \
		echo "❌ Target '$$$$target_name' not found in $(1) capability"; \
	else \
		echo "❌ Capability '$(1)' not found or has no Makefile"; \
		exit 1; \
	fi
endef

# Generate delegation targets for each capability
$(foreach cap,$(patsubst capabilities/%/,%,$(CAPABILITY_DIRS)),$(eval $(call CAPABILITY_DELEGATION,$(cap))))

# Show available capability targets
.PHONY: capabilities-targets
capabilities-targets: ## Show all available capability targets
	@echo "🎯 Available Capability Targets:"
	@echo "================================"
	@for dir in $(CAPABILITY_DIRS); do \
		if [ -f "$$dir/Makefile" ]; then \
			capability=$$(basename $$dir); \
			echo ""; \
			echo "📦 $$capability:"; \
			$(MAKE) -C "$$dir" -qp 2>/dev/null | \
			grep -E "^[a-zA-Z][^:]*:.*##" | \
			sed 's/:.*##//' | \
			while read target; do \
				echo "   $$capability-$$target"; \
				echo "   $@"; \
			done || echo "   No documented targets"; \
		fi; \
	done

# Capability status check
.PHONY: capabilities-status
capabilities-status: ## Show status of all capabilities
	@echo "📊 Capability Status:"
	@echo "===================="
	@for dir in $(CAPABILITY_DIRS); do \
		capability=$$(basename $$dir); \
		echo ""; \
		echo "📦 $$capability:"; \
		if [ -f "$$dir/Makefile" ]; then \
			echo "   ✅ Makefile: Available"; \
		else \
			echo "   ❌ Makefile: Missing"; \
		fi; \
		if [ -f "$$dir/pyproject.toml" ]; then \
			echo "   ✅ Package: Available"; \
			if pip show "$$(echo $$capability | tr '-' '_')" >/dev/null 2>&1 || \
			   pip show "$$capability" >/dev/null 2>&1; then \
				echo "   ✅ Installed: Yes"; \
			else \
				echo "   ❌ Installed: No"; \
			fi; \
		else \
			echo "   ❌ Package: Not available"; \
		fi; \
		if [ -f "$$dir/README.md" ]; then \
			echo "   ✅ Documentation: Available"; \
		else \
			echo "   ❌ Documentation: Missing"; \
		fi; \
	done

# Auto-complete help integration
.PHONY: help-capabilities
help-capabilities: ## Show capability system help
	@echo ""
	@echo "🧩 Capability System:"
	@echo "  capabilities-list            List all available capabilities"
	@echo "  capabilities-help            Show help for all capabilities"
	@echo "  capabilities-status          Show status of all capabilities"
	@echo "  capabilities-install         Install all capabilities"
	@echo "  capabilities-test            Test all capabilities"
	@echo "  capabilities-targets         Show all available capability targets"
	@echo ""
	@echo "🎯 Capability Delegation:"
	@echo "  Use 'capability-name-target' to call targets from capabilities"
	@echo "  Example: 'make release-management-status' calls status from release-management"
	@echo "  Special: release-* targets are delegated to release-management capability"
	@echo ""

# Export capability information for use by main Makefile
CAPABILITIES_FOUND := $(words $(CAPABILITY_DIRS))
export CAPABILITIES_FOUND
export CAPABILITY_DIRS