.PHONY: install format sort-imports lint test test-verbose test-cov clean check-types check-format check-sort-imports check-ruff build help check
.DEFAULT_GOAL := help

ARGS ?=
SYNC_ARGS := --extra dev --extra api --extra cli --extra mcp
PYTHON_PATHS := redis_agent_kit tests

install: ## Install project and development dependencies
	@echo "🚀 Installing project dependencies with uv"
	uv sync $(SYNC_ARGS)

format: ## Format code with ruff
	@echo "🎨 Formatting code"
	uv run ruff format $(PYTHON_PATHS)
	uv run ruff check --fix $(PYTHON_PATHS)

check-format: ## Check formatting
	@echo "🔍 Checking formatting"
	uv run ruff format --check $(PYTHON_PATHS)

sort-imports: ## Sort imports with ruff
	@echo "📦 Sorting imports"
	uv run ruff check --select I --fix $(PYTHON_PATHS)

check-sort-imports: ## Check import sorting
	@echo "🔍 Checking import sorting"
	uv run ruff check --select I $(PYTHON_PATHS)

check-ruff: ## Run ruff lint checks
	@echo "🔍 Running ruff"
	uv run ruff check $(PYTHON_PATHS)

check-types: ## Run mypy type checking
	@echo "🔍 Running mypy"
	uv run python -m mypy redis_agent_kit

lint: check-format check-sort-imports check-ruff check-types ## Run linting checks

test: ## Run tests (pass extra args with ARGS="...")
	@echo "🧪 Running tests"
	uv run python -m pytest $(ARGS)

test-verbose: ## Run tests with verbose output
	@echo "🧪 Running tests (verbose)"
	uv run python -m pytest -vv -s $(ARGS)

test-cov: ## Run tests with coverage
	@echo "🧪 Running tests with coverage"
	uv run python -m pytest --cov=redis_agent_kit --cov-report=term-missing --cov-report=html $(ARGS)

check: lint test ## Run lint and tests

build: ## Build wheel and source distribution
	@echo "🏗️ Building distribution packages"
	uv build

clean: ## Clean build artifacts and caches
	@echo "🧹 Cleaning up"
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".ruff_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "htmlcov" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "dist" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "build" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name ".coverage" -delete 2>/dev/null || true

help: ## Show this help message
	@echo "Available commands:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2}'
