.DEFAULT_GOAL := help
PYTHON := python3
VENV := .venv
BIN := $(VENV)/bin
IMAGE := weaveflow:latest

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

$(VENV): ## Create the virtualenv (falls back to virtualenv when python3-venv is absent)
	$(PYTHON) -m venv $(VENV) 2>/dev/null || virtualenv -p $(PYTHON) $(VENV)

.PHONY: install
install: $(VENV) ## Install the framework with dev + all provider extras
	$(BIN)/pip install --upgrade pip
	$(BIN)/pip install -e ".[dev,all]"

.PHONY: lint
lint: ## Run ruff lint checks
	$(BIN)/ruff check weaveflow tests

.PHONY: format
format: ## Auto-format with ruff
	$(BIN)/ruff format weaveflow tests
	$(BIN)/ruff check --fix weaveflow tests

.PHONY: typecheck
typecheck: ## Run mypy in strict mode
	$(BIN)/mypy weaveflow

.PHONY: test
test: ## Run the test suite with coverage (fails under 80%)
	$(BIN)/pytest --cov=weaveflow --cov-report=term-missing

.PHONY: e2e
e2e: ## Run the end-to-end suite (CLI, pipeline, examples — offline, no slow tests)
	$(BIN)/pytest tests/e2e

.PHONY: e2e-full
e2e-full: ## Run e2e incl. the build+install-in-clean-venv deploy test
	WEAVEFLOW_E2E_FULL=1 $(BIN)/pytest -m slow tests/e2e

.PHONY: check
check: lint typecheck test ## Run lint, typecheck, and tests

.PHONY: example
example: ## Run the offline example pipeline
	$(BIN)/python examples/quickstart.py

.PHONY: demo-framework
demo-framework: ## Run the hands-on test-bed (series/parallel/nested/memory/CLI)
	$(BIN)/python test-framwork/run.py

.PHONY: docs
docs: ## Serve the documentation site locally (http://127.0.0.1:8000)
	$(BIN)/pip install -e ".[docs]" && $(BIN)/mkdocs serve

.PHONY: docs-build
docs-build: ## Build the static documentation site into ./site
	$(BIN)/pip install -e ".[docs]" && $(BIN)/mkdocs build

.PHONY: docs-html
docs-html: ## Build the zero-dependency HTML docs (docs/index.html + docs/pages/)
	python3 docs/build_site.py

.PHONY: build
build: ## Build the wheel + sdist
	$(BIN)/pip install build && $(BIN)/python -m build

.PHONY: docker-build
docker-build: ## Build the production Docker image
	docker build -t $(IMAGE) .

.PHONY: docker-run
docker-run: ## Run the CLI inside the Docker image
	docker run --rm $(IMAGE) --version

.PHONY: clean
clean: ## Remove build/test artifacts
	rm -rf $(VENV) dist build site *.egg-info .pytest_cache .mypy_cache .ruff_cache .coverage
	find . -type d -name __pycache__ -exec rm -rf {} +
