# Makefile for lumelog - A lightweight, flexible logging library for Rust
# Author: Kyrylenko Yevhen <ceo@lineardev.net>

# Default target
.DEFAULT_GOAL := help

# Variables
CARGO = cargo
PROJECT_NAME = lumelog
TEST_THREADS = 1

# Colors for output
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
BLUE = \033[0;34m
PURPLE = \033[0;35m
CYAN = \033[0;36m
NC = \033[0m # No Color

##@ Development Commands

.PHONY: build
build: ## Build the project in debug mode
	@echo "$(BLUE)Building $(PROJECT_NAME) in debug mode...$(NC)"
	$(CARGO) build

.PHONY: build-release
build-release: ## Build the project in release mode
	@echo "$(BLUE)Building $(PROJECT_NAME) in release mode...$(NC)"
	$(CARGO) build --release

.PHONY: dev
dev: ## Run the project in debug mode
	@echo "$(BLUE)Running $(PROJECT_NAME) in debug mode...$(NC)"
	$(CARGO) run

.PHONY: watch
watch: ## Watch for changes and rebuild
	@echo "$(BLUE)Watching for changes...$(NC)"
	$(CARGO) watch -x run

.PHONY: check
check: ## Check the project for errors without building
	@echo "$(CYAN)Checking $(PROJECT_NAME) for errors...$(NC)"
	$(CARGO) check

.PHONY: fmt
fmt: ## Format the code using rustfmt
	@echo "$(PURPLE)Formatting code...$(NC)"
	$(CARGO) fmt

.PHONY: fmt-check
fmt-check: ## Check if code is properly formatted
	@echo "$(PURPLE)Checking code formatting...$(NC)"
	$(CARGO) fmt --check

.PHONY: clippy
clippy: ## Run clippy linter for code quality
	@echo "$(YELLOW)Running clippy linter...$(NC)"
	$(CARGO) clippy --all-targets --all-features -- -D warnings

.PHONY: clippy-fix
clippy-fix: ## Automatically fix clippy suggestions where possible
	@echo "$(YELLOW)Auto-fixing clippy suggestions...$(NC)"
	$(CARGO) clippy --fix --allow-dirty --allow-staged

##@ Testing Commands

.PHONY: test
test: ## Run all tests (sequential for OnceCell safety)
	@echo "$(GREEN)Running all tests sequentially...$(NC)"
	$(CARGO) test --test main -- --test-threads=$(TEST_THREADS)

.PHONY: test-all
test-all: ## Run all tests including unit tests and doc tests
	@echo "$(GREEN)Running all tests (unit, integration, doc)...$(NC)"
	$(CARGO) test -- --test-threads=$(TEST_THREADS)

.PHONY: test-unit
test-unit: ## Run unit tests only
	@echo "$(GREEN)Running unit tests...$(NC)"
	$(CARGO) test --lib

.PHONY: test-integration
test-integration: ## Run integration tests only
	@echo "$(GREEN)Running integration tests...$(NC)"
	$(CARGO) test --test main -- --test-threads=$(TEST_THREADS)

.PHONY: test-doc
test-doc: ## Run documentation tests only
	@echo "$(GREEN)Running documentation tests...$(NC)"
	$(CARGO) test --doc

.PHONY: test-verbose
test-verbose: ## Run tests with verbose output
	@echo "$(GREEN)Running tests with verbose output...$(NC)"
	$(CARGO) test --test main -- --test-threads=$(TEST_THREADS) --nocapture

.PHONY: test-coverage
test-coverage: ## Generate test coverage report (requires cargo-tarpaulin)
	@echo "$(GREEN)Generating test coverage report...$(NC)"
	$(CARGO) tarpaulin --out Html --output-dir coverage

##@ Documentation Commands

.PHONY: doc
doc: ## Generate documentation
	@echo "$(CYAN)Generating documentation...$(NC)"
	$(CARGO) doc --no-deps

.PHONY: doc-open
doc-open: ## Generate and open documentation in browser
	@echo "$(CYAN)Generating and opening documentation...$(NC)"
	$(CARGO) doc --no-deps --open

.PHONY: doc-all
doc-all: ## Generate documentation with dependencies
	@echo "$(CYAN)Generating documentation with dependencies...$(NC)"
	$(CARGO) doc

##@ Maintenance Commands

.PHONY: clean
clean: ## Clean build artifacts
	@echo "$(RED)Cleaning build artifacts...$(NC)"
	$(CARGO) clean
	@echo "$(RED)Removing test log files...$(NC)"
	@find . -name "test_*.txt" -type f -delete 2>/dev/null || true
	@find . -name "*.log" -type f -delete 2>/dev/null || true

.PHONY: update
update: ## Update dependencies
	@echo "$(YELLOW)Updating dependencies...$(NC)"
	$(CARGO) update

.PHONY: audit
audit: ## Audit dependencies for security vulnerabilities
	@echo "$(YELLOW)Auditing dependencies...$(NC)"
	$(CARGO) audit

.PHONY: outdated
outdated: ## Check for outdated dependencies (requires cargo-outdated)
	@echo "$(YELLOW)Checking for outdated dependencies...$(NC)"
	$(CARGO) outdated

##@ Publishing Commands

.PHONY: package
package: ## Package the crate for publishing
	@echo "$(BLUE)Packaging $(PROJECT_NAME)...$(NC)"
	$(CARGO) package

.PHONY: publish-dry-run
publish-dry-run: ## Dry run of publishing to crates.io
	@echo "$(BLUE)Dry run of publishing $(PROJECT_NAME)...$(NC)"
	$(CARGO) publish --dry-run

.PHONY: publish
publish: ## Publish to crates.io (use with caution)
	@echo "$(RED)Publishing $(PROJECT_NAME) to crates.io...$(NC)"
	@read -p "Are you sure you want to publish? (y/N): " confirm && [ "$$confirm" = "y" ]
	$(CARGO) publish

##@ CI/CD Commands

.PHONY: ci-check
ci-check: fmt-check clippy test ## Run all CI checks (format, lint, test)
	@echo "$(GREEN)All CI checks passed!$(NC)"

.PHONY: pre-commit
pre-commit: fmt clippy test git-clean-check ## Run pre-commit checks
	@echo "$(GREEN)Pre-commit checks completed successfully!$(NC)"

.PHONY: release-check
release-check: ci-check doc package ## Run full release validation
	@echo "$(GREEN)Release validation completed!$(NC)"

##@ Utility Commands

.PHONY: deps
deps: ## Install development dependencies
	@echo "$(YELLOW)Installing development dependencies...$(NC)"
	@command -v cargo-tarpaulin >/dev/null 2>&1 || cargo install cargo-tarpaulin
	@command -v cargo-outdated >/dev/null 2>&1 || cargo install cargo-outdated
	@command -v cargo-audit >/dev/null 2>&1 || cargo install cargo-audit

.PHONY: info
info: ## Show project information
	@echo "$(CYAN)Project Information:$(NC)"
	@echo "Name: $(PROJECT_NAME)"
	@echo "Version: $$($(CARGO) pkgid | cut -d# -f2)"
	@echo "Rust version: $$(rustc --version)"
	@echo "Cargo version: $$($(CARGO) --version)"
	@echo "Test threads: $(TEST_THREADS)"

.PHONY: example
example: ## Run example usage (creates example log files)
	@echo "$(GREEN)Running example usage...$(NC)"
	@$(CARGO) run --example basic_usage 2>/dev/null || echo "$(YELLOW)No examples found$(NC)"

.PHONY: benchmark
benchmark: ## Run benchmarks (if any)
	@echo "$(GREEN)Running benchmarks...$(NC)"
	@$(CARGO) bench 2>/dev/null || echo "$(YELLOW)No benchmarks found$(NC)"

.PHONY: help
help: ## Display this help message
	@awk 'BEGIN {FS = ":.*##"; printf "\n$(CYAN)$(PROJECT_NAME) - Makefile Commands$(NC)\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf "  $(GREEN)%-20s$(NC) %s\n", $$1, $$2 } /^##@/ { printf "\n$(PURPLE)%s$(NC)\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
	@echo ""

##@ Quick Commands (Aliases)

.PHONY: b r t c f
b: build    ## Alias for build
r: build-release ## Alias for build-release  
t: test     ## Alias for test
c: check    ## Alias for check
f: fmt      ## Alias for fmt