# Makefile for StemeDB

.PHONY: all build dev quality install clean test test-fast test-lib install-nextest help go-fmt go-lint go-test validate

# Default target
all: build

## --- Development ---

# Build the project (dev profile)
build:
	cargo build

# Run the project
dev:
	cargo run -p stemedb-core

# Run tests - choose based on need
test:
	@cargo nextest run --workspace 2>/dev/null || cargo test --workspace

# Fast: single crate during development (usage: make test-fast CRATE=stemedb-core)
test-fast:
ifndef CRATE
	$(error CRATE is required. Usage: make test-fast CRATE=stemedb-core)
endif
	cargo test -p $(CRATE) --lib

# Medium: all lib tests (skip integration tests)
test-lib:
	cargo test --workspace --lib

# Install nextest for faster parallel test execution
install-nextest:
	cargo install cargo-nextest --locked

# Validate end-to-end (build, start server, assert, query, shutdown)
validate:
	@./scripts/validate.sh

# Fast validation (skip build, use existing binaries)
validate-fast:
	@./scripts/validate.sh --no-build

## --- Quality & Verification ---

# Run all quality checks (formatting, linting, duplication, tests)
quality: fmt-check lint duplication test
	@echo "✅ Quality checks passed!"

# Check for code duplication
duplication:
	@echo "Checking for code duplication..."
	@jscpd ./crates --format rust --min-lines 5 --min-tokens 50 || true

# Check formatting without changing files
fmt-check:
	cargo fmt --all -- --check

# Run clippy (linter)
lint:
	cargo clippy --workspace --all-targets --all-features -- -D warnings

# Fix formatting automatically
fmt:
	cargo fmt --all

## --- Go SDK ---

# Format Go code
go-fmt:
	@echo "Formatting Go SDK..."
	@for mod in sdk/go/steme sdk/go/adk sdk/go/examples/basic sdk/go/examples/skeptic; do \
		if [ -d "$$mod" ]; then \
			gofmt -w "$$mod"; \
			command -v goimports >/dev/null && goimports -w "$$mod" || true; \
		fi \
	done

# Lint Go code
go-lint:
	@echo "Linting Go SDK..."
	@for mod in sdk/go/steme sdk/go/adk; do \
		if [ -f "$$mod/go.mod" ]; then \
			echo "  → $$mod"; \
			(cd "$$mod" && go vet ./...); \
			command -v golangci-lint >/dev/null && (cd "$$mod" && golangci-lint run ./...) || true; \
		fi \
	done

# Test Go code
go-test:
	@echo "Testing Go SDK..."
	@for mod in sdk/go/steme sdk/go/adk; do \
		if [ -f "$$mod/go.mod" ]; then \
			echo "  → $$mod"; \
			(cd "$$mod" && go test ./... -v); \
		fi \
	done

## --- Installation ---

# Install dependencies (Rust toolchain, etc - assumed cargo exists)
install:
	@echo "Ensuring rust toolchain is up to date..."
	rustup update stable
	@echo "Installing helpful tools..."
	# cargo install cargo-watch # Optional but recommended

## --- Utilities ---

clean:
	cargo clean

help:
	@echo "Available commands:"
	@echo "  make build       - Build the workspace"
	@echo "  make dev         - Run the core binary"
	@echo "  make test        - Run all tests (uses nextest if available)"
	@echo "  make test-fast CRATE=name - Fast: single crate lib tests (~30s)"
	@echo "  make test-lib    - Medium: all unit tests (~3min)"
	@echo "  make install-nextest - Install cargo-nextest for parallel tests"
	@echo "  make quality     - Run format check, clippy, duplication, tests"
	@echo "  make fmt         - Auto-format code"
	@echo "  make lint        - Run clippy linter"
	@echo "  make duplication - Check for code duplication (jscpd)"
	@echo "  make go-fmt      - Format Go SDK code"
	@echo "  make go-lint     - Lint Go SDK code"
	@echo "  make go-test     - Test Go SDK"
	@echo "  make install     - Setup environment"
	@echo "  make validate    - End-to-end validation (build, server, assert, query)"
	@echo "  make validate-fast - Fast validation (skip build)"
