# MarkiTect Utils Capability Makefile
# Utility functions library for the MarkiTect ecosystem

# Capability metadata
CAPABILITY_NAME := markitect-utils
CAPABILITY_DESCRIPTION := Common utility functions for the MarkiTect ecosystem

# Default target
.PHONY: help
help: ## Show utils capability help
	@echo "🛠️  MarkiTect Utils Capability"
	@echo "==============================="
	@echo ""
	@echo "Library Testing:"
	@echo "  utils-test-string            Test string utility functions"
	@echo "  utils-test-file              Test file utility functions"
	@echo "  utils-test-validation        Test validation utility functions"
	@echo "  utils-test-api               Test complete API functionality"
	@echo ""
	@echo "Development & Setup:"
	@echo "  utils-install                Install utils capability"
	@echo "  utils-install-dev            Install with development dependencies"
	@echo "  utils-test                   Run utils capability tests"
	@echo "  utils-test-cov               Run tests with coverage report"
	@echo "  utils-lint                   Run code quality checks"
	@echo "  utils-clean                  Clean build artifacts"
	@echo ""
	@echo "Quality & Compliance:"
	@echo "  utils-validate-paradigm      Validate ComposableRepositoryParadigm compliance"
	@echo "  utils-check-dependencies     Verify zero external dependencies"

# Development and Setup
.PHONY: utils-install
utils-install: ## Install utils capability
	pip install -e capabilities/markitect-utils/

.PHONY: utils-install-dev
utils-install-dev: ## Install utils capability with development dependencies
	pip install -e "capabilities/markitect-utils/[dev]"

.PHONY: utils-test
utils-test: ## Run utils capability tests
	cd capabilities/markitect-utils && pytest tests/

.PHONY: utils-test-cov
utils-test-cov: ## Run tests with coverage report
	cd capabilities/markitect-utils && pytest tests/ --cov=markitect_utils --cov-report=html --cov-report=term

.PHONY: utils-lint
utils-lint: ## Run code quality checks
	@echo "🔍 Running code quality checks for markitect-utils..."
	cd capabilities/markitect-utils && python -m py_compile src/markitect_utils/*.py
	@echo "✅ Code quality checks passed"

.PHONY: utils-clean
utils-clean: ## Clean build artifacts
	cd capabilities/markitect-utils && rm -rf build/ dist/ *.egg-info/ __pycache__/ .pytest_cache/ htmlcov/ .coverage
	find capabilities/markitect-utils -name "*.pyc" -delete
	find capabilities/markitect-utils -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true

# Library Function Testing
.PHONY: utils-test-string
utils-test-string: ## Test string utility functions
	@echo "🧪 Testing string utilities..."
	cd capabilities/markitect-utils && python -c "from src.markitect_utils import slugify, truncate, camel_to_snake, snake_to_camel, strip_ansi_codes; print('slugify(\"Hello World!\"):', slugify('Hello World!')); print('truncate(\"This is a long string\", 10):', truncate('This is a long string', 10)); print('camel_to_snake(\"camelCase\"):', camel_to_snake('camelCase')); print('snake_to_camel(\"snake_case\"):', snake_to_camel('snake_case')); print('strip_ansi_codes(\"\\\\033[31mRed\\\\033[0m\"):', strip_ansi_codes('\\033[31mRed\\033[0m')); print('✅ String utilities working')"

.PHONY: utils-test-file
utils-test-file: ## Test file utility functions
	@echo "🧪 Testing file utilities..."
	cd capabilities/markitect-utils && python -c "from src.markitect_utils import safe_filename, ensure_extension, normalize_path; import tempfile, os; print('safe_filename(\"file<name>.txt\"):', safe_filename('file<name>.txt')); print('ensure_extension(\"document\", \".md\"):', ensure_extension('document', '.md')); print('normalize_path(\"./test/../file.txt\"):', normalize_path('./test/../file.txt')); print('✅ File utilities working')"

.PHONY: utils-test-validation
utils-test-validation: ## Test validation utility functions
	@echo "🧪 Testing validation utilities..."
	cd capabilities/markitect-utils && python -c "from src.markitect_utils import is_valid_email, is_valid_url, is_valid_semver, validate_required_fields; print('is_valid_email(\"user@example.com\"):', is_valid_email('user@example.com')); print('is_valid_url(\"https://example.com\"):', is_valid_url('https://example.com')); print('is_valid_semver(\"1.0.0\"):', is_valid_semver('1.0.0')); result = validate_required_fields({'name': 'John', 'email': '', 'age': 30}, ['name', 'email', 'phone']); print('validate_required_fields test:', result); print('✅ Validation utilities working')"

.PHONY: utils-test-api
utils-test-api: ## Test complete API functionality
	@echo "🧪 Testing complete utils API..."
	@$(MAKE) --no-print-directory utils-test-string
	@$(MAKE) --no-print-directory utils-test-file
	@$(MAKE) --no-print-directory utils-test-validation
	@echo "🎉 All utility functions tested successfully!"

# Quality & Compliance
.PHONY: utils-validate-paradigm
utils-validate-paradigm: ## Validate ComposableRepositoryParadigm compliance
	@echo "🏛️  Validating ComposableRepositoryParadigm compliance..."
	@echo "✅ Checking src layout structure..."
	test -d capabilities/markitect-utils/src/markitect_utils
	@echo "✅ Checking pyproject.toml exists..."
	test -f capabilities/markitect-utils/pyproject.toml
	@echo "✅ Checking README.md exists..."
	test -f capabilities/markitect-utils/README.md
	@echo "✅ Checking tests directory..."
	test -d capabilities/markitect-utils/tests
	@echo "✅ Verifying independent configuration..."
	cd capabilities/markitect-utils && python -c "import tomllib; f=open('pyproject.toml','rb'); data=tomllib.load(f); assert data['project']['name']=='markitect-utils'; print('✅ Independent pyproject.toml configuration verified')"
	@echo "🎉 ComposableRepositoryParadigm compliance validated!"

.PHONY: utils-check-dependencies
utils-check-dependencies: ## Verify zero external dependencies
	@echo "📦 Checking dependency compliance..."
	cd capabilities/markitect-utils && python -c "import tomllib; f=open('pyproject.toml','rb'); data=tomllib.load(f); deps=data.get('project',{}).get('dependencies',[]); print(f'❌ Found external dependencies: {deps}') if deps else print('✅ Zero external dependencies confirmed - paradigm compliant!'); exit(1) if deps else None"

# Demonstration Functions
.PHONY: utils-demo
utils-demo: ## Demonstrate utility functions with examples
	@echo "🎬 MarkiTect Utils Capability Demonstration"
	@echo "==========================================="
	@echo ""
	@echo "String Utilities:"
	@$(MAKE) --no-print-directory utils-test-string
	@echo ""
	@echo "File Utilities:"
	@$(MAKE) --no-print-directory utils-test-file
	@echo ""
	@echo "Validation Utilities:"
	@$(MAKE) --no-print-directory utils-test-validation

# Meta information for capability discovery
.PHONY: capability-info
capability-info: ## Show capability information
	@echo "Name: $(CAPABILITY_NAME)"
	@echo "Description: $(CAPABILITY_DESCRIPTION)"
	@echo "Type: Pure library capability (zero external dependencies)"
	@echo "Main modules: string_utils, file_utils, validation_utils"
	@echo "Paradigm role: Reference implementation for ComposableRepositoryParadigm"
	@echo "Dependencies: None (Python standard library only)"
	@echo "Targets:"
	@$(MAKE) --no-print-directory help | grep "^  " | sed 's/^  /    /'