# Makefile for MokoCassiopeia Joomla Template
# Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Build and validation powered by MokoStandards Enterprise API
# Install: composer install

# ==============================================================================
# CONFIGURATION
# ==============================================================================

EXTENSION_NAME    := mokocassiopeia
EXTENSION_TYPE    := template
EXTENSION_VERSION := $(shell grep -oP 'VERSION:\s*\K[0-9.]+' README.md 2>/dev/null || echo "0.0.0")

SRC_DIR   := src
BUILD_DIR := build
DIST_DIR  := dist

PHP      := php
COMPOSER := composer
MOKO     := vendor/bin/moko

# Colors
COLOR_RESET  := \033[0m
COLOR_GREEN  := \033[32m
COLOR_YELLOW := \033[33m
COLOR_BLUE   := \033[34m
COLOR_RED    := \033[31m

# ==============================================================================
# TARGETS
# ==============================================================================

.PHONY: help
help: ## Show this help message
	@echo "$(COLOR_BLUE)╔════════════════════════════════════════════════════════════╗$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)║         MokoCassiopeia Template Build                     ║$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)╚════════════════════════════════════════════════════════════╝$(COLOR_RESET)"
	@echo ""
	@echo "Extension: $(EXTENSION_NAME) ($(EXTENSION_TYPE)) v$(EXTENSION_VERSION)"
	@echo "Powered by: MokoStandards Enterprise API"
	@echo ""
	@echo "$(COLOR_GREEN)Available targets:$(COLOR_RESET)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(COLOR_BLUE)%-20s$(COLOR_RESET) %s\n", $$1, $$2}'
	@echo ""

# ── Dependencies ──────────────────────────────────────────────────────────────

.PHONY: install-deps
install-deps: ## Install Composer dependencies (includes MokoStandards API)
	@echo "$(COLOR_BLUE)Installing dependencies...$(COLOR_RESET)"
	@$(COMPOSER) install
	@echo "$(COLOR_GREEN)✓ Dependencies installed$(COLOR_RESET)"

.PHONY: update-deps
update-deps: ## Update Composer dependencies
	@echo "$(COLOR_BLUE)Updating dependencies...$(COLOR_RESET)"
	@$(COMPOSER) update
	@echo "$(COLOR_GREEN)✓ Dependencies updated$(COLOR_RESET)"

# ── Validation (MokoStandards API) ────────────────────────────────────────────

.PHONY: check-moko
check-moko:
	@if [ ! -f "$(MOKO)" ]; then \
		echo "$(COLOR_RED)✗ MokoStandards CLI not found. Run: make install-deps$(COLOR_RESET)"; \
		exit 1; \
	fi

.PHONY: lint
lint: check-moko ## PHP syntax check via MokoStandards
	@echo "$(COLOR_BLUE)Running PHP syntax check...$(COLOR_RESET)"
	@$(PHP) $(MOKO) check:syntax -- --path .
	@echo "$(COLOR_GREEN)✓ PHP syntax OK$(COLOR_RESET)"

.PHONY: check-joomla
check-joomla: check-moko ## Validate Joomla manifest via MokoStandards
	@echo "$(COLOR_BLUE)Validating Joomla manifest...$(COLOR_RESET)"
	@$(PHP) $(MOKO) check:joomla -- --path .
	@echo "$(COLOR_GREEN)✓ Joomla manifest valid$(COLOR_RESET)"

.PHONY: check-version
check-version: check-moko ## Verify version consistency across files
	@echo "$(COLOR_BLUE)Checking version consistency...$(COLOR_RESET)"
	@$(PHP) $(MOKO) check:version -- --path .
	@echo "$(COLOR_GREEN)✓ Versions consistent$(COLOR_RESET)"

.PHONY: check-headers
check-headers: check-moko ## Check license headers on source files
	@echo "$(COLOR_BLUE)Checking license headers...$(COLOR_RESET)"
	@$(PHP) $(MOKO) check:headers -- --path .
	@echo "$(COLOR_GREEN)✓ Headers OK$(COLOR_RESET)"

.PHONY: check-secrets
check-secrets: check-moko ## Scan for leaked credentials
	@echo "$(COLOR_BLUE)Scanning for secrets...$(COLOR_RESET)"
	@$(PHP) $(MOKO) check:secrets -- --path .
	@echo "$(COLOR_GREEN)✓ No secrets found$(COLOR_RESET)"

.PHONY: check-xml
check-xml: check-moko ## Validate XML files are well-formed
	@echo "$(COLOR_BLUE)Checking XML files...$(COLOR_RESET)"
	@$(PHP) $(MOKO) check:xml -- --path .
	@echo "$(COLOR_GREEN)✓ XML well-formed$(COLOR_RESET)"

.PHONY: validate
validate: lint check-joomla check-version check-xml check-headers check-secrets ## Run all MokoStandards validation checks
	@echo "$(COLOR_GREEN)✓ All validation checks passed$(COLOR_RESET)"

.PHONY: health
health: check-moko ## Full repository health check via MokoStandards
	@echo "$(COLOR_BLUE)Running full health check...$(COLOR_RESET)"
	@$(PHP) $(MOKO) health -- --path .

# ── Build ─────────────────────────────────────────────────────────────────────

.PHONY: clean
clean: ## Clean build artifacts
	@echo "$(COLOR_BLUE)Cleaning build artifacts...$(COLOR_RESET)"
	@rm -rf $(BUILD_DIR) $(DIST_DIR)
	@echo "$(COLOR_GREEN)✓ Build artifacts cleaned$(COLOR_RESET)"

.PHONY: build
build: clean ## Build template installable ZIP from src/
	@echo "$(COLOR_BLUE)Building $(EXTENSION_NAME) v$(EXTENSION_VERSION)...$(COLOR_RESET)"
	@mkdir -p $(BUILD_DIR)/package $(DIST_DIR)
	@cp -r $(SRC_DIR)/* $(BUILD_DIR)/package/
	@cd $(BUILD_DIR)/package && \
		if command -v zip >/dev/null 2>&1; then \
			zip -r "../../$(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION).zip" .; \
		elif command -v pwsh >/dev/null 2>&1; then \
			pwsh -Command "Compress-Archive -Path '*' -DestinationPath '../../$(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION).zip' -Force"; \
		elif command -v powershell >/dev/null 2>&1; then \
			powershell -Command "Compress-Archive -Path '*' -DestinationPath '../../$(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION).zip' -Force"; \
		else \
			echo "$(COLOR_RED)✗ No zip tool found (zip, pwsh, powershell)$(COLOR_RESET)"; \
			exit 1; \
		fi
	@echo "$(COLOR_GREEN)✓ Package: $(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION).zip$(COLOR_RESET)"

.PHONY: build-beta
build-beta: clean ## Build beta release ZIP
	@echo "$(COLOR_BLUE)Building $(EXTENSION_NAME) v$(EXTENSION_VERSION)-beta...$(COLOR_RESET)"
	@mkdir -p $(BUILD_DIR)/package $(DIST_DIR)
	@cp -r $(SRC_DIR)/* $(BUILD_DIR)/package/
	@cd $(BUILD_DIR)/package && \
		if command -v zip >/dev/null 2>&1; then \
			zip -r "../../$(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION)-beta.zip" .; \
		elif command -v pwsh >/dev/null 2>&1; then \
			pwsh -Command "Compress-Archive -Path '*' -DestinationPath '../../$(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION)-beta.zip' -Force"; \
		elif command -v powershell >/dev/null 2>&1; then \
			powershell -Command "Compress-Archive -Path '*' -DestinationPath '../../$(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION)-beta.zip' -Force"; \
		else \
			echo "$(COLOR_RED)✗ No zip tool found$(COLOR_RESET)"; \
			exit 1; \
		fi
	@echo "$(COLOR_GREEN)✓ Package: $(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION)-beta.zip$(COLOR_RESET)"

.PHONY: checksum
checksum: ## Generate SHA-256 checksums for dist packages
	@echo "$(COLOR_BLUE)Generating checksums...$(COLOR_RESET)"
	@for f in $(DIST_DIR)/*.zip; do \
		sha256sum "$$f" | tee "$${f}.sha256"; \
	done
	@echo "$(COLOR_GREEN)✓ Checksums generated$(COLOR_RESET)"

# ── Release ───────────────────────────────────────────────────────────────────

.PHONY: release
release: validate build checksum ## Full release pipeline (validate + build + checksum)
	@echo "$(COLOR_GREEN)✓ Release package ready$(COLOR_RESET)"
	@echo ""
	@echo "$(COLOR_BLUE)Next steps:$(COLOR_RESET)"
	@echo "  1. Tag: git tag $(EXTENSION_VERSION)"
	@echo "  2. Push: git push origin --tags"
	@echo "  3. Create Gitea release and attach $(DIST_DIR)/$(EXTENSION_NAME)-$(EXTENSION_VERSION).zip"
	@echo ""

# ── Info ──────────────────────────────────────────────────────────────────────

.PHONY: version
version: ## Display version and extension info
	@echo "$(COLOR_BLUE)Extension Information:$(COLOR_RESET)"
	@echo "  Name:    $(EXTENSION_NAME)"
	@echo "  Type:    $(EXTENSION_TYPE)"
	@echo "  Version: $(EXTENSION_VERSION)"

.PHONY: security-check
security-check: ## Run Composer security audit
	@echo "$(COLOR_BLUE)Running security checks...$(COLOR_RESET)"
	@$(COMPOSER) audit
	@echo "$(COLOR_GREEN)✓ Security check complete$(COLOR_RESET)"

.PHONY: all
all: install-deps validate build checksum ## Full pipeline: deps → validate → build → checksum
	@echo "$(COLOR_GREEN)✓ Complete build pipeline finished$(COLOR_RESET)"

.DEFAULT_GOAL := help
