# Makefile for StemeDB

.PHONY: all build dev quality install clean test 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
test:
	cargo test --workspace

# 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 unit 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)"
