# ============================================================
# Bamboo Inspect — Makefile
# Run `make` or `make help` to see all available targets
# ============================================================

.DEFAULT_GOAL := help

COMPOSE := docker compose -f docker/docker-compose.yml --env-file .env
VENV    := .venv
PIP     := $(VENV)/bin/pip

.PHONY: help dev start stop logs \
        client-install client-build client-publish client-publish-test \
        lint format clean

help: ## Show this help message
	@echo "Bamboo Inspect — Available targets:"
	@echo ""
	@echo "  Server (Core Manager):"
	@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z_-]+:.*## Server/ {printf "    \033[36m%-24s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@echo ""
	@echo "  Client (Test Agent):"
	@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z_-]+:.*## Client/ {printf "    \033[36m%-24s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@echo ""
	@echo "  General:"
	@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z_-]+:.*## General/ {printf "    \033[36m%-24s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

# ------------------------------------------------------------------
# Server (Core Manager — Docker)
# ------------------------------------------------------------------

dev: ## Server: Start dev server (hot reload, DB from .env)
	$(COMPOSE) --profile dev up --build

start: ## Server: Start production server (DB config from .env, detached)
	$(COMPOSE) --profile prod up -d --build

stop: ## Server: Stop all containers
	$(COMPOSE) --profile dev --profile prod down

logs: ## Server: Tail production server logs
	$(COMPOSE) --profile prod logs -f

# ------------------------------------------------------------------
# Client (Test Agent — PyPI package)
# ------------------------------------------------------------------

client-install: ## Client: Create venv and install agent in editable mode
	python3 -m venv $(VENV)
	$(PIP) install --upgrade pip
	$(PIP) install -e ".[dev]"
	@echo ""
	@echo "Done! Activate with: source $(VENV)/bin/activate"

client-build: ## Client: Build Python package (sdist + wheel)
	$(VENV)/bin/python -m build

client-publish: client-build ## Client: Publish package to PyPI
	$(VENV)/bin/twine check dist/*
	$(VENV)/bin/twine upload dist/*

client-publish-test: client-build ## Client: Publish package to TestPyPI
	$(VENV)/bin/twine check dist/*
	$(VENV)/bin/twine upload --repository testpypi dist/*

# ------------------------------------------------------------------
# General
# ------------------------------------------------------------------

lint: ## General: Run linter (ruff check)
	$(VENV)/bin/ruff check src/ server/

format: ## General: Auto-format code (ruff format)
	$(VENV)/bin/ruff format src/ server/

clean: ## General: Remove build artifacts and caches
	rm -rf dist/ build/
	find . -type d -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
	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
	@echo "Cleaned."
