# Makefile for Paysera Checkout Integration SDK

# Configuration variables
PHP_VERSION ?= 7.4
IMAGE_NAME = lib-checkout-integration-sdk
DOCKER_RUN = docker run -it -u $$(id -u) -v $$(PWD):/app -w /app

# Colors for output
CYAN = \033[0;36m
GREEN = \033[0;32m
YELLOW = \033[0;33m
NC = \033[0m # No Color

.PHONY: help
help: ## Show this help message
	@echo "$(CYAN)Available commands:$(NC)"
	@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(GREEN)%-20s$(NC) %s\n", $$1, $$2}'

.PHONY: build
build: ## Build Docker image (use PHP_VERSION=8.4 for different version)
	@echo "$(CYAN)Building Docker image with PHP $(PHP_VERSION)...$(NC)"
	@rm composer.lock 2>/dev/null || true
	@docker build --build-arg PHP_VERSION=$(PHP_VERSION) -t $(IMAGE_NAME) --quiet .
	@$(DOCKER_RUN) $(IMAGE_NAME) composer install --no-interaction --quiet
	@echo "$(GREEN)✓ Image built successfully$(NC)"

.PHONY: build-php74
build-php74: ## Build Docker image with PHP 7.4
	@$(MAKE) build PHP_VERSION=7.4

.PHONY: build-php80
build-php80: ## Build Docker image with PHP 8.0
	@$(MAKE) build PHP_VERSION=8.0

.PHONY: build-php81
build-php81: ## Build Docker image with PHP 8.1
	@$(MAKE) build PHP_VERSION=8.1

.PHONY: build-php82
build-php82: ## Build Docker image with PHP 8.2
	@$(MAKE) build PHP_VERSION=8.2

.PHONY: build-php83
build-php83: ## Build Docker image with PHP 8.3
	@$(MAKE) build PHP_VERSION=8.3

.PHONY: build-php84
build-php84: ## Build Docker image with PHP 8.4
	@$(MAKE) build PHP_VERSION=8.4

.PHONY: php-version
php-version: ## Show PHP version in current Docker image
	@echo "$(CYAN)PHP version in Docker image:$(NC)"
	@docker run --rm $(IMAGE_NAME) php -v || echo "$(YELLOW)Image not built yet. Run 'make build' first.$(NC)"

.PHONY: test
test: ## Run PHPUnit tests
	@echo "$(CYAN)Running tests...$(NC)"
	@$(DOCKER_RUN) $(IMAGE_NAME) composer phpunit

.PHONY: coverage
coverage: ## Generate test coverage (coverage/ directory)
	@echo "$(CYAN)Generating test coverage...$(NC)"
	@$(DOCKER_RUN) -e XDEBUG_MODE=coverage $(IMAGE_NAME) composer test-coverage
	@echo "$(GREEN)✓ Coverage saved to coverage/ directory$(NC)"

.PHONY: analyse
analyse: ## Run PHPStan analysis
	@echo "$(CYAN)Running PHPStan analysis...$(NC)"
	@$(DOCKER_RUN) $(IMAGE_NAME) composer analyse

.PHONY: baseline
baseline: ## Generate PHPStan baseline (ignore current errors)
	@echo "$(CYAN)Generating PHPStan baseline...$(NC)"
	@$(DOCKER_RUN) $(IMAGE_NAME) composer baseline
	@echo "$(GREEN)✓ Baseline generated$(NC)"

.PHONY: format
format: ## Fix code formatting issues (php-cs-fixer)
	@echo "$(CYAN)Fixing code formatting...$(NC)"
	@$(DOCKER_RUN) $(IMAGE_NAME) composer format
	@echo "$(GREEN)✓ Code formatted$(NC)"

.PHONY: format-check
format-check: ## Check code formatting without making changes
	@echo "$(CYAN)Checking code formatting...$(NC)"
	@$(DOCKER_RUN) $(IMAGE_NAME) vendor/bin/php-cs-fixer fix --dry-run --diff

.PHONY: check
check: format-check analyse test ## Full check check (formatting + analysis + tests)

# Default command
.DEFAULT_GOAL := help
