# Rook — common dev tasks. Run `make` to see all commands.

.PHONY: help setup install test lint fix typecheck build clean commit docs release-check

# Colors for help
CYAN  := \033[36m
RESET := \033[0m

help:  ## Show this help
	@echo ""
	@echo "Rook dev commands:"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  $(CYAN)%-18s$(RESET) %s\n", $$1, $$2}'
	@echo ""

# -----------------------------------------------------------------------------
# Setup (run once)
# -----------------------------------------------------------------------------

setup:  ## First-time dev setup: install pipx rook + pre-commit hooks
	@echo "→ Installing Rook via pipx (editable)..."
	@if ! command -v pipx >/dev/null 2>&1; then \
		echo "pipx not installed. Run: brew install pipx && pipx ensurepath"; \
		exit 1; \
	fi
	pipx install -e . --force
	@echo "→ Installing pre-commit hooks..."
	pip install --user pre-commit commitizen
	pre-commit install
	pre-commit install --hook-type commit-msg
	@echo ""
	@echo "✓ Done. Now: rook --help"

install:  ## Install project in editable mode with all extras
	pip install -e ".[dev,mcp,agent]"

# -----------------------------------------------------------------------------
# Daily dev
# -----------------------------------------------------------------------------

test:  ## Run fast unit tests
	pytest tests/ --ignore=tests/integration -q --tb=short

test-all:  ## Run all tests including integration (needs Docker)
	pytest tests/ -q --tb=short

lint:  ## Check formatting + lint (same as CI)
	ruff format --check src/ tests/
	ruff check src/ tests/

fix:  ## Auto-fix formatting + lint issues
	ruff format src/ tests/
	ruff check src/ tests/ --fix

typecheck:  ## Run mypy
	mypy src/rook

# -----------------------------------------------------------------------------
# Committing (guided)
# -----------------------------------------------------------------------------

commit:  ## Guided conventional commit (uses commitizen if installed)
	@if command -v cz >/dev/null 2>&1; then \
		cz commit; \
	else \
		echo "Install commitizen for guided commits: pip install --user commitizen"; \
		echo "Or commit manually with conventional format:"; \
		echo "  feat: add time-travel command"; \
		echo "  fix: panel alignment on long names"; \
		echo "  chore: bump deps"; \
	fi

# -----------------------------------------------------------------------------
# Building + releasing
# -----------------------------------------------------------------------------

build:  ## Build sdist + wheel
	rm -rf dist/
	python -m build

clean:  ## Remove build artifacts + caches
	rm -rf dist/ build/ *.egg-info .pytest_cache .ruff_cache .mypy_cache .coverage

release-check:  ## Verify repo is ready to release (lint + tests + build)
	@echo "→ Running lint..."
	@$(MAKE) lint
	@echo "→ Running tests..."
	@$(MAKE) test
	@echo "→ Verifying build..."
	@$(MAKE) build
	@twine check dist/*
	@echo ""
	@echo "✓ Ready. Release happens automatically when you merge the"
	@echo "  'chore: release X.Y.Z' PR that Release Please maintains."

# -----------------------------------------------------------------------------
# Docs + utilities
# -----------------------------------------------------------------------------

docs:  ## Open release playbook
	@less docs/RELEASING.md

version:  ## Print current version
	@grep -E '^version' pyproject.toml | head -1
