.PHONY: help build test run install clean check format lint publish bump-patch bump-minor bump-major version

# Default target
.DEFAULT_GOAL := help

# Colors for output
CYAN := \033[0;36m
GREEN := \033[0;32m
YELLOW := \033[0;33m
RED := \033[0;31m
RESET := \033[0m

help: ## Show this help message
	@echo "$(CYAN)qlink - Quick Link Browser Launcher$(RESET)"
	@echo ""
	@echo "$(GREEN)Available targets:$(RESET)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(CYAN)%-15s$(RESET) %s\n", $$1, $$2}'

version: ## Show current version
	@grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2

build: ## Build release binary
	@echo "$(GREEN)Building release binary...$(RESET)"
	cargo build --release
	@echo "$(GREEN)✓ Binary built: target/release/qlink$(RESET)"

build-dev: ## Build debug binary
	@echo "$(GREEN)Building debug binary...$(RESET)"
	cargo build
	@echo "$(GREEN)✓ Binary built: target/debug/qlink$(RESET)"

test: ## Run all tests
	@echo "$(GREEN)Running tests...$(RESET)"
	cargo test

test-verbose: ## Run tests with verbose output
	@echo "$(GREEN)Running tests (verbose)...$(RESET)"
	cargo test -- --nocapture

run: ## Run the application
	cargo run

check: ## Run cargo check
	@echo "$(GREEN)Checking code...$(RESET)"
	cargo check

format: ## Format code with rustfmt
	@echo "$(GREEN)Formatting code...$(RESET)"
	cargo fmt

format-check: ## Check code formatting
	@echo "$(GREEN)Checking code format...$(RESET)"
	cargo fmt -- --check

lint: ## Run clippy linter
	@echo "$(GREEN)Running clippy...$(RESET)"
	cargo clippy -- -D warnings

lint-fix: ## Run clippy with automatic fixes
	@echo "$(GREEN)Running clippy with fixes...$(RESET)"
	cargo clippy --fix --allow-dirty --allow-staged

ci: format-check lint test ## Run all CI checks

install: ## Install binary to ~/.cargo/bin
	@echo "$(GREEN)Installing qlink...$(RESET)"
	cargo install --path .
	@echo "$(GREEN)✓ qlink installed$(RESET)"
	@echo "Run: qlink init"

install-local: build ## Install binary to ~/.local/bin
	@echo "$(GREEN)Installing to ~/.local/bin...$(RESET)"
	mkdir -p ~/.local/bin
	cp target/release/qlink ~/.local/bin/
	@echo "$(GREEN)✓ qlink installed to ~/.local/bin/qlink$(RESET)"

uninstall: ## Uninstall binary
	@echo "$(YELLOW)Uninstalling qlink...$(RESET)"
	cargo uninstall qlink || true
	rm -f ~/.local/bin/qlink
	@echo "$(GREEN)✓ qlink uninstalled$(RESET)"

clean: ## Clean build artifacts
	@echo "$(GREEN)Cleaning build artifacts...$(RESET)"
	cargo clean
	@echo "$(GREEN)✓ Clean complete$(RESET)"

doc: ## Generate and open documentation
	@echo "$(GREEN)Generating documentation...$(RESET)"
	cargo doc --no-deps --open

doc-build: ## Generate documentation without opening
	@echo "$(GREEN)Generating documentation...$(RESET)"
	cargo doc --no-deps

# Version bumping targets
bump-patch: ## Bump patch version (1.0.0 -> 1.0.1)
	@echo "$(YELLOW)Bumping patch version...$(RESET)"
	@$(MAKE) _bump-version TYPE=patch

bump-minor: ## Bump minor version (1.0.0 -> 1.1.0)
	@echo "$(YELLOW)Bumping minor version...$(RESET)"
	@$(MAKE) _bump-version TYPE=minor

bump-major: ## Bump major version (1.0.0 -> 2.0.0)
	@echo "$(YELLOW)Bumping major version...$(RESET)"
	@$(MAKE) _bump-version TYPE=major

_bump-version:
	@CURRENT_VERSION=$$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2); \
	MAJOR=$$(echo $$CURRENT_VERSION | cut -d. -f1); \
	MINOR=$$(echo $$CURRENT_VERSION | cut -d. -f2); \
	PATCH=$$(echo $$CURRENT_VERSION | cut -d. -f3); \
	if [ "$(TYPE)" = "major" ]; then \
		NEW_VERSION=$$((MAJOR + 1)).0.0; \
	elif [ "$(TYPE)" = "minor" ]; then \
		NEW_VERSION=$$MAJOR.$$((MINOR + 1)).0; \
	elif [ "$(TYPE)" = "patch" ]; then \
		NEW_VERSION=$$MAJOR.$$MINOR.$$((PATCH + 1)); \
	fi; \
	echo "$(CYAN)Current version: $$CURRENT_VERSION$(RESET)"; \
	echo "$(GREEN)New version:     $$NEW_VERSION$(RESET)"; \
	sed -i.bak "s/^version = \"$$CURRENT_VERSION\"/version = \"$$NEW_VERSION\"/" Cargo.toml && rm Cargo.toml.bak; \
	echo "$(GREEN)✓ Version updated in Cargo.toml$(RESET)"; \
	echo "$(YELLOW)Run 'git add Cargo.toml && git commit -m \"chore: bump version to $$NEW_VERSION\"'$(RESET)"

# Publishing targets
publish-dry-run: ## Run publish dry-run (validate package)
	@echo "$(GREEN)Running publish dry-run...$(RESET)"
	cargo publish --dry-run

publish-check: ci publish-dry-run ## Run all checks before publishing
	@echo "$(GREEN)✓ Ready to publish$(RESET)"

publish: publish-check ## Publish to crates.io
	@echo "$(YELLOW)Publishing to crates.io...$(RESET)"
	@read -p "Are you sure you want to publish? [y/N] " -n 1 -r; \
	echo; \
	if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
		cargo publish; \
		echo "$(GREEN)✓ Published successfully$(RESET)"; \
		echo "$(CYAN)Create a git tag:$(RESET)"; \
		VERSION=$$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2); \
		echo "  git tag v$$VERSION"; \
		echo "  git push origin v$$VERSION"; \
	else \
		echo "$(RED)Publish cancelled$(RESET)"; \
	fi

# Release workflow
release-patch: bump-patch build test ## Bump patch version, build, and test
	@echo "$(GREEN)✓ Patch release ready$(RESET)"
	@$(MAKE) _show-release-steps

release-minor: bump-minor build test ## Bump minor version, build, and test
	@echo "$(GREEN)✓ Minor release ready$(RESET)"
	@$(MAKE) _show-release-steps

release-major: bump-major build test ## Bump major version, build, and test
	@echo "$(GREEN)✓ Major release ready$(RESET)"
	@$(MAKE) _show-release-steps

_show-release-steps:
	@VERSION=$$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2); \
	echo ""; \
	echo "$(CYAN)Next steps:$(RESET)"; \
	echo "  1. Review changes: git diff"; \
	echo "  2. Commit: git add Cargo.toml && git commit -m \"chore: bump version to $$VERSION\""; \
	echo "  3. Tag: git tag v$$VERSION"; \
	echo "  4. Push: git push && git push --tags"; \
	echo "  5. Publish: make publish"

# Development helpers
dev: ## Start development mode with auto-reload (requires cargo-watch)
	@if command -v cargo-watch > /dev/null; then \
		cargo watch -x run; \
	else \
		echo "$(RED)cargo-watch not installed$(RESET)"; \
		echo "Install with: cargo install cargo-watch"; \
	fi

init-dev: ## Install development tools
	@echo "$(GREEN)Installing development tools...$(RESET)"
	cargo install cargo-watch
	cargo install cargo-edit
	rustup component add rustfmt clippy
	@echo "$(GREEN)✓ Development tools installed$(RESET)"

size: build ## Show binary size
	@echo "$(CYAN)Binary sizes:$(RESET)"
	@ls -lh target/release/qlink | awk '{print "  Release: " $$5}'
	@if [ -f target/debug/qlink ]; then \
		ls -lh target/debug/qlink | awk '{print "  Debug:   " $$5}'; \
	fi

tree: ## Show project structure
	@if command -v tree > /dev/null; then \
		tree -I 'target|.git' -L 3; \
	else \
		find . -not -path '*/target/*' -not -path '*/.git/*' | head -50; \
	fi

audit: ## Run security audit
	@echo "$(GREEN)Running security audit...$(RESET)"
	@if command -v cargo-audit > /dev/null; then \
		cargo audit; \
	else \
		echo "$(YELLOW)cargo-audit not installed$(RESET)"; \
		echo "Install with: cargo install cargo-audit"; \
	fi

outdated: ## Check for outdated dependencies
	@echo "$(GREEN)Checking for outdated dependencies...$(RESET)"
	@if command -v cargo-outdated > /dev/null; then \
		cargo outdated; \
	else \
		echo "$(YELLOW)cargo-outdated not installed$(RESET)"; \
		echo "Install with: cargo install cargo-outdated"; \
	fi

update: ## Update dependencies
	@echo "$(GREEN)Updating dependencies...$(RESET)"
	cargo update

# Benchmarking
bench: ## Run benchmarks (if any)
	cargo bench

# Coverage (requires cargo-tarpaulin)
coverage: ## Generate test coverage report
	@if command -v cargo-tarpaulin > /dev/null; then \
		cargo tarpaulin --out Html --output-dir coverage; \
		@echo "$(GREEN)✓ Coverage report: coverage/index.html$(RESET)"; \
	else \
		echo "$(YELLOW)cargo-tarpaulin not installed$(RESET)"; \
		echo "Install with: cargo install cargo-tarpaulin"; \
	fi

# Git helpers
git-tag: ## Create git tag from current version
	@VERSION=$$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2); \
	echo "$(CYAN)Creating tag v$$VERSION$(RESET)"; \
	git tag -a "v$$VERSION" -m "Release v$$VERSION"; \
	echo "$(GREEN)✓ Tag created$(RESET)"; \
	echo "Push with: git push origin v$$VERSION"
