
CARGO          := cargo
CARGO_FLAGS    ?=
TARGET_FLAG    := $(if $(TARGET),--target $(TARGET),)
RELEASE_FLAG   := --release
BIN_NAME       := lcurs-admin
INSTALL_PATH   := $(shell cargo install --list 2>/dev/null | grep -E "^$(BIN_NAME) " | head -1 | awk '{print $$3}' || echo "~/.cargo/bin/$(BIN_NAME)")
.PHONY: all
all: check fmt build
	@echo "$(GREEN)✓ All tasks completed$(NC)"
.PHONY: build
build:
	@echo "$(YELLOW)🔨 Building debug binary...$(NC)"
	$(CARGO) build $(CARGO_FLAGS) $(TARGET_FLAG)
.PHONY: release
release:
	@echo "$(YELLOW)⚡ Building release binary...$(NC)"
	$(CARGO) build $(RELEASE_FLAG) $(CARGO_FLAGS) $(TARGET_FLAG)
.PHONY: check
check:
	@echo "$(YELLOW)🔍 Running cargo check...$(NC)"
	$(CARGO) check $(CARGO_FLAGS) $(TARGET_FLAG)
.PHONY: lint
lint:
	@echo "$(YELLOW)🧹 Running Clippy...$(NC)"
	$(CARGO) clippy -- -D warnings
.PHONY: fix
fix:
	@echo "$(YELLOW)🔧 Auto-fixing Clippy warnings...$(NC)"
	$(CARGO) clippy --fix --allow-dirty --allow-staged
.PHONY: fmt
fmt:
	@echo "$(YELLOW)🎨 Formatting code...$(NC)"
	$ rmcm -l rs -i -r -c 0 ./src/
	$(CARGO) fmt --all --verbose
.PHONY: fmt-check
fmt-check:
	@echo "$(YELLOW)🔎 Checking formatting...$(NC)"
	$(CARGO) fmt --all -- --check
.PHONY: clean
clean:
	@echo "$(YELLOW)🧹 Cleaning target/ directory...$(NC)"
	$(CARGO) clean
	@echo "$(GREEN)✓ Cleaned$(NC)"
.PHONY: install
install: build
	@echo "$(YELLOW)📦 Installing debug binary...$(NC)"
	$(CARGO) install --path . --force $(CARGO_FLAGS)
.PHONY: install-release
install-release: release
	@echo "$(YELLOW)📦 Installing release binary...$(NC)"
	$(CARGO) install --path . --force --verbose
.PHONY: uninstall
uninstall:
	@echo "$(YELLOW)🗑️  Uninstalling $(BIN_NAME)...$(NC)"
	-$(CARGO) uninstall $(BIN_NAME) 2>/dev/null || echo "$(RED)Binary not found$(NC)"
.PHONY: run
run: build
	@echo "$(YELLOW)▶️  Running debug binary...$(NC)"
	$(CARGO) run -- $(ARGS)
.PHONY: dev
dev: run
.PHONY: test
test:
	@echo "$(YELLOW)🧪 Running tests...$(NC)"
	$(CARGO) test $(CARGO_FLAGS)
.PHONY: test-verbose
test-verbose:
	@echo "$(YELLOW)🧪 Running tests (verbose)...$(NC)"
	$(CARGO) test -- --nocapture
.PHONY: doc
doc:
	@echo "$(YELLOW)📚 Generating documentation...$(NC)"
	$(CARGO) doc --no-deps --open
.PHONY: upgrade
upgrade:
	@echo "$(YELLOW)📦 Updating dependencies (cargo update)...$(NC)"
	$(CARGO) update
.PHONY: outdated
outdated:
	@echo "$(YELLOW)📊 Checking outdated dependencies...$(NC)"
	$(CARGO) outdated --exit-code 0 || true
.PHONY: audit
audit:
	@echo "$(YELLOW)🔒 Security audit...$(NC)"
	@if ! command -v cargo-audit >/dev/null 2>&1; then \
		echo "$(RED)cargo-audit not installed. Run: cargo install cargo-audit$(NC)"; \
		exit 1; \
	fi
	cargo audit
.PHONY: pre-commit
pre-commit: fmt-check check lint test
	@echo "$(GREEN)✓ All pre-commit checks passed$(NC)"
.PHONY: help
help:
	@echo "$(GREEN)LCURS Administrator Makefile$(NC)"
	@echo ""
	@echo "$(YELLOW)Usage: make [target] [ARGS=\"...\"]$(NC)"
	@echo ""
	@echo "$(YELLOW)Targets:$(NC)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(GREEN)%-18s$(NC) %s\n", $$1, $$2}'
	@echo ""
	@echo "$(YELLOW)Examples:$(NC)"
	@echo "  make release"
	@echo "  make run ARGS=\"encrypt -i input.txt -o output.age\""
	@echo "  make test-verbose"
	@echo "  make install-release"
	@echo "  make upgrade"
%:
	@echo "$(YELLOW)Unknown target '$@'. Use 'make help' for available targets.$(NC)"
	@exit 1