.PHONY: install test lint format build up down clean all

# ── Variables ────────────────────────────────────────────────────────────────
PYTHON      := python
PIP         := pip
PYTEST      := pytest
RUFF        := ruff
COMPOSE     := docker compose

# ── Development ──────────────────────────────────────────────────────────────

## Install project with dev dependencies
install:
	$(PIP) install -e '.[dev]'

## Run test suite
test:
	$(PYTEST) tests/ -v --tb=short

## Run linter (ruff check + format check)
lint:
	$(RUFF) check flowagent/
	$(RUFF) format --check flowagent/

## Auto-format source code with ruff
format:
	$(RUFF) format flowagent/
	$(RUFF) check --fix flowagent/

# ── Docker ───────────────────────────────────────────────────────────────────

## Build all Docker images
build:
	$(COMPOSE) build

## Start all services in detached mode
up:
	$(COMPOSE) up -d

## Stop and remove all services
down:
	$(COMPOSE) down

## Show logs for all services (Ctrl+C to stop)
logs:
	$(COMPOSE) logs -f

## Rebuild images and restart services
restart: down build up

# ── Cleanup ──────────────────────────────────────────────────────────────────

## Remove containers, images, volumes, and caches
clean:
	$(COMPOSE) down --volumes --remove-orphans
	docker system prune -f
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true
	rm -rf .pytest_cache .ruff_cache dist build *.egg-info

# ── Meta ─────────────────────────────────────────────────────────────────────

## Run lint, test, then build (full CI-like pass)
all: lint test build

## Show available targets
help:
	@echo ""
	@echo "FlowAgent Makefile targets:"
	@echo ""
	@grep -E '^## ' $(MAKEFILE_LIST) | sed 's/^## /  /' | \
	    awk 'BEGIN{p=""} /^  [a-z]/{p=$$0; next} {print p; print "    "$$0; p=""}' || \
	    grep -E '^[a-z_-]+:' $(MAKEFILE_LIST) | cut -d: -f1 | sort | sed 's/^/  make /'
	@echo ""

.DEFAULT_GOAL := help
