# Container runtime (podman or docker)
CONTAINER_RUNTIME ?= $(shell command -v podman 2>/dev/null || echo docker)

# Container images
IMAGE_TAG ?= latest
CLAUDE_IMAGE_NAME ?= ai-helpers
CURSOR_IMAGE_NAME ?= ai-helpers-cursor

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

.PHONY: skillsaw
skillsaw: ## Run skillsaw linter on skills and plugins
	@echo "Running skillsaw..."
	@if [ -n "$${SKILLSAW_BIN:-}" ]; then \
		"$${SKILLSAW_BIN}"; \
	else \
		uvx skillsaw; \
	fi

.PHONY: skillsaw-fix
skillsaw-fix: ## Auto-fix fixable skillsaw issues
	@echo "Fixing skillsaw issues..."
	@if [ -n "$${SKILLSAW_BIN:-}" ]; then \
		"$${SKILLSAW_BIN}" fix; \
	else \
		uvx skillsaw fix; \
	fi

.PHONY: lint
lint: ## Run skillsaw, ruff syntax checker and formatter, and shellcheck
	@$(MAKE) skillsaw
	@echo "Running ruff syntax checker on Python scripts..."
	@if command -v ruff >/dev/null 2>&1; then \
		ruff check .; \
	else \
		echo "ruff not found, skipping Python syntax checking. Install with: pip install ruff"; \
		exit 1; \
	fi
	@echo "Running ruff format checker on Python scripts..."
	@ruff format --check --diff .
	@echo "Running shellcheck on shell scripts..."
	@if command -v shellcheck >/dev/null 2>&1; then \
		find . -name '*.sh' -type f -exec shellcheck {} + && echo "All checks passed!"; \
	else \
		echo "shellcheck not found, skipping shell script linting. Install with: dnf install ShellCheck"; \
		exit 1; \
	fi
	@echo "Checking that 'make update' doesn't generate uncommitted changes..."
	@$(MAKE) update
	@if ! git diff --quiet; then \
		echo "Error: 'make update' generated uncommitted changes. Please commit these changes:"; \
		git diff --name-only; \
		exit 1; \
	else \
		echo "✓ No uncommitted changes after 'make update'"; \
	fi

.PHONY: update
update: ## Update Claude settings and website data
	@if ! command -v uv >/dev/null 2>&1; then \
		echo "Error: uv is not installed. Install it with:"; \
		echo "  curl -LsSf https://astral.sh/uv/install.sh | sh"; \
		exit 1; \
	fi
	@echo "Updating Claude settings..."
	@UV_CACHE_DIR=$${TMPDIR:-/tmp}/uv-cache ./scripts/update_claude_settings.py
	@echo "Building website data..."
	@UV_CACHE_DIR=$${TMPDIR:-/tmp}/uv-cache ./scripts/build-website.py
	@echo "Formatting Python code with ruff..."
	@if command -v ruff >/dev/null 2>&1; then \
		ruff format .; \
	else \
		echo "ruff not found, skipping Python formatting. Install with: pip install ruff"; \
	fi

.PHONY: test
test: ## Run tests
	@echo "Running tests..."
	@if command -v pytest >/dev/null 2>&1; then \
		pytest images/claude/tests/ -v; \
	else \
		echo "pytest not found. Install with: pip install pytest"; \
		exit 1; \
	fi

.PHONY: build
build: build-claude build-cursor ## Build all container images

.PHONY: build-claude
build-claude: ## Build Claude CLI container image
	@echo "Building Claude container image $(CLAUDE_IMAGE_NAME):$(IMAGE_TAG) with $(CONTAINER_RUNTIME)..."
	$(CONTAINER_RUNTIME) build -f images/claude/Containerfile -t $(CLAUDE_IMAGE_NAME):$(IMAGE_TAG) .

.PHONY: build-cursor
build-cursor: ## Build Cursor CLI container image
	@echo "Building Cursor container image $(CURSOR_IMAGE_NAME):$(IMAGE_TAG) with $(CONTAINER_RUNTIME)..."
	$(CONTAINER_RUNTIME) build -f images/cursor/Containerfile -t $(CURSOR_IMAGE_NAME):$(IMAGE_TAG) .


.PHONY: container-build
container-build: build ## Alias for build target

.PHONY: docs
docs: ## Run docs locally at http://localhost:8000
	@echo "Starting local documentation server..."
	@python3 -m http.server 8000 --directory docs

.DEFAULT_GOAL := help
