# Augint-Library: Python Package Template
# Development commands for creating reusable Python libraries

# Project configuration
PROJECT_NAME ?= augint-library
PYTHON_VERSION ?= 3.12
MIN_COVERAGE ?= 90

# Default target
.DEFAULT_GOAL := help

.PHONY: help test test-all test-unit test-integration test-slow coverage quick-test watch profile lint lint-fix format type-check security docs clean install build dev-install pre-commit claude docker-build docker-stop docker-clean mutmut-run mutmut-results mutmut-report mutmut-show mutmut-diff

# Core Commands
help: ## Show this help message
	@echo "$(PROJECT_NAME) Development Commands"
	@echo ""
	@echo "Core Commands:"
	@awk 'BEGIN {FS = ":.*##"}; /^[a-zA-Z_-]+:.*##/ { printf "  %-20s %s\n", $$1, $$2 }' $(MAKEFILE_LIST) | sort
	@echo ""
	@echo "Testing Commands:"
	@echo "  test                 Run fast tests (default)"
	@echo "  test-unit            Run unit tests with coverage"
	@echo "  test-integration     Run integration tests"
	@echo "  test-all             Run all tests"
	@echo "  test-slow            Run slow tests only"
	@echo "  coverage             Generate coverage report"
	@echo ""
	@echo "Mutation Testing:"
	@echo "  mutmut-run           Run mutation tests (slow!)"
	@echo "  mutmut-results       Show mutation test results"
	@echo "  mutmut-report        Generate HTML mutation report"
	@echo "  mutmut-show          Show specific mutation (use ID=n)"
	@echo "  mutmut-diff          Test only changed files"
	@echo ""
	@echo "Library Commands:"
	@echo "  type-check           Run type checking"
	@echo "  security             Run security scans"
	@echo "  docs                 Generate documentation"
	@echo "  build                Build distribution packages"
	@echo "  pre-commit           Run pre-commit hooks"

# Installation targets
install: ## Install project dependencies
	@echo "Installing dependencies..."
	@uv sync
	@echo "Dependencies installed successfully"

dev-install: ## Install with development dependencies
	@echo "Installing development dependencies..."
	@uv sync --group security --group compliance
	@echo "Development dependencies installed successfully"

# Testing targets - respects pytest.ini configuration
test: ## Run fast tests (default)
	@echo "Running fast tests..."
	@pytest -m "fast or (not slow and not ci_only and not integration)" --cov=src --cov-report=html --cov-report=xml --cov-fail-under=$(MIN_COVERAGE)

test-unit:
	pytest -m "fast or (not slow and not ci_only and not integration)" --cov=src --cov-report=html --cov-report=xml --cov-fail-under=90

test-integration:
	pytest -m "integration or ci_only or slow" --cov=src --cov-report=html --cov-report=xml

test-all:
	pytest -m "" --cov=src --cov-report=html --cov-report=xml --cov-fail-under=90

test-slow:
	pytest -m slow

coverage:
	pytest tests/unit/ --cov=src --cov-report=term-missing --cov-report=html --cov-fail-under=90

quick-test: ## Run only fast unit tests
	@echo "Running quick unit tests..."
	@uv run pytest tests/unit/ -m "not slow"

watch: ## Run tests in watch mode
	@echo "Running tests in watch mode (press Ctrl+C to stop)..."
	@uv run pytest-watch

profile: ## Run with profiling
	@echo "Running benchmark tests with profiling..."
	@uv run pytest tests/unit/test_benchmarks.py --benchmark-only --benchmark-cprofile=tottime

# Code quality targets
lint: ## Run linting with fixes
	@echo "Running linter..."
	@ruff check --fix

format: ## Format code
	@echo "Formatting code..."
	@ruff format .

lint-fix: ## Auto-fix linting issues
	@echo "Auto-fixing linting issues..."
	@uv run ruff check --fix
	@uv run ruff format

type-check:
	mypy src/

# Security scanning
security:
	bandit -r src/
	safety check
	pip-audit

# Documentation
docs:
	pdoc --output-dir docs src/

# Pre-commit hooks
pre-commit: ## Run all pre-commit hooks
	@echo "Running pre-commit hooks..."
	@pre-commit run --all-files

# Cleanup
clean: ## Clean build artifacts and caches
	@echo "Cleaning build artifacts..."
	@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 ".ruff_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 "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
	@rm -rf dist/ build/ htmlcov/ .coverage coverage.xml coverage.json
	@rm -f pytest.log test-report.html
	@echo "Clean complete"

# Build distribution
build: ## Build distribution packages
	@echo "Building distribution packages..."
	@uv build
	@echo "Build complete"

# Convenience target to continue a Claude session
claude:
	@echo "Launching Claude Code..."
	@docker-compose up -d dev
	@echo "Waiting for container to be ready..."
	@powershell -Command "Start-Sleep -Seconds 2"
	@docker-compose exec -it dev claude $(ARGS)


claude-continue:
	@$(MAKE) claude ARGS=-c

join-claude:
	@echo "Joining Claude container with bash shell..."
	@docker-compose exec -it dev /bin/bash

claude-x:
	@$(MAKE) claude ARGS='--dangerously-skip-permissions -c'

# Show ports for running containers
ports:
	@docker-compose ps

docker-build:
	@echo "Building Docker images..."
	docker-compose build

docker-stop:
	@echo "Stopping Docker containers..."
	@docker-compose down

docker-clean:
	@echo "Cleaning up Docker resources..."
	@docker-compose down -v --remove-orphans
	docker system prune -f
	rm -f pytest.log test-report.html

# Mutation Testing targets
mutmut-run:
	@echo "Running mutation tests (this will take a while)..."
	@echo "Press Ctrl+C to stop and save progress"
	mutmut run

mutmut-results:
	@echo "Mutation test results:"
	mutmut results

mutmut-report:
	@echo "Generating HTML mutation report..."
	mutmut html
	@echo "Report saved to html/index.html"

mutmut-show:
	@if [ -z "$(ID)" ]; then \
		echo "Usage: make mutmut-show ID=n"; \
		echo "Where n is the mutation ID from 'make mutmut-results'"; \
	else \
		mutmut show $(ID); \
	fi

mutmut-diff:
	@echo "Running mutation tests on changed files only..."
	@git_diff=$$(git diff --name-only origin/main... | grep -E "\.py$$" | grep -v test_ | xargs); \
	if [ -n "$$git_diff" ]; then \
		mutmut run --paths-to-mutate="$$git_diff"; \
	else \
		echo "No Python files changed"; \
	fi
