.PHONY: install install-dev clean lint format check test tests help setup update dev-setup

PYTHON_MIN_VERSION = 3.10

## ==== Environment Setup ====

## Verify the development environment
verify-env:
	@echo ">> Checking Python version..."
	@python3 -c "import sys; assert sys.version_info >= (3, 10), 'Python $(PYTHON_MIN_VERSION) or higher is required'" || (echo "Error: Python $(PYTHON_MIN_VERSION) or higher is required" && exit 1)
	@echo ">> Checking uv installation..."
	@uv --version >/dev/null 2>&1 || (echo "Error: uv is not installed. Please install it first: https://docs.astral.sh/uv/getting-started/installation/" && exit 1)

## Setup development environment with uv
setup: verify-env
	@echo ">> Setting up development environment..."
	uv sync

## Update dependencies to their latest versions
update:
	@echo ">> Updating dependencies..."
	uv lock --upgrade && uv sync

## Install for production
install: verify-env
	@echo ">> Installing production dependencies"
	uv sync --no-dev

## Install for development
install-dev: verify-env
	@echo ">> Installing development dependencies"
	uv sync

## ==== Development Commands ====

## Run code formatting
format:
	@echo ">> Formatting code..."
	uv run ruff check --fix --unsafe-fixes trainwave_cli/ tests/
	uv run black .

## Run linting checks
lint:
	@echo ">> Running linting..."
	uv run ruff check trainwave_cli/ tests/

## Run all checks (linting, formatting, tests)
check: lint
	@echo ">> Checking code format..."
	uv run black --check .
	@echo ">> Running tests..."
	$(MAKE) test

## Run tests with coverage
test:
	uv run pytest -s tests/ \
		--log-level=WARNING \
		--disable-pytest-warnings

## Run tests (alias for test)
tests: test

## Delete all temporary files and artifacts
clean:
	@echo ">> Cleaning temporary files..."
	find . -type d -name "__pycache__" -exec rm -rf {} +
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	find . -type d -name ".eggs" -exec rm -rf {} +
	find . -type d -name ".pytest_cache" -exec rm -rf {} +
	find . -type d -name ".coverage" -exec rm -rf {} +
	find . -type d -name ".ruff_cache" -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	find . -type f -name "*.pyo" -delete
	find . -type f -name "*.pyd" -delete
	find . -type f -name ".coverage" -delete
	find . -type f -name "coverage.xml" -delete
	rm -rf build/ dist/ .ipynb_checkpoints/ **/.ipynb_checkpoints

## ==== Quick Development Setup ====

## Set up complete development environment (recommended for new developers)
dev-setup: verify-env setup
	@echo ">> Development environment is ready!"
	@echo ">> Run 'make help' to see available commands"


#################################################################################
# Self Documenting Commands                                                     #
#################################################################################

.DEFAULT_GOAL := help

# Inspired by <http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html>
# sed script explained:
# /^##/:
# 	* save line in hold space
# 	* purge line
# 	* Loop:
# 		* append newline + line to hold space
# 		* go to next line
# 		* if line starts with doc comment, strip comment character off and loop
# 	* remove target prerequisites
# 	* append hold space (+ newline) to line
# 	* replace newline plus comments by `---`
# 	* print line
# Separate expressions are necessary because labels cannot be delimited by
# semicolon; see <http://stackoverflow.com/a/11799865/1968>
.PHONY: help
help:
	@echo "$$(tput bold)Available commands:$$(tput sgr0)"
	@sed -n -e "/^## / { \
		h; \
		s/.*//; \
		:doc" \
		-e "H; \
		n; \
		s/^## //; \
		t doc" \
		-e "s/:.*//; \
		G; \
		s/\\n## /---/; \
		s/\\n/ /g; \
		p; \
	}" ${MAKEFILE_LIST} \
	| awk -F '---' \
		-v ncol=$$(tput cols) \
		-v indent=19 \
		-v col_on="$$(tput setaf 6)" \
		-v col_off="$$(tput sgr0)" \
	'{ \
		printf "%s%*s%s ", col_on, -indent, $$1, col_off; \
		n = split($$2, words, " "); \
		line_length = ncol - indent; \
		for (i = 1; i <= n; i++) { \
			line_length -= length(words[i]) + 1; \
			if (line_length <= 0) { \
				line_length = ncol - indent - length(words[i]) - 1; \
				printf "\n%*s ", -indent, " "; \
			} \
			printf "%s ", words[i]; \
		} \
		printf "\n"; \
	}' \
	| more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars')
