.PHONY: start deps build sdk dev test test-plugin lint fmt clean help

BINARY := enclave.wasm
BUILD_DIR := example

# === Primary Commands ===

start: deps build sdk dev

deps:
	@command -v sqlc >/dev/null || go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
	@command -v golangci-lint >/dev/null || go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	@bun install
	@cd example && bun install

build:
	@echo "Building WASM plugin..."
	@GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared -o $(BUILD_DIR)/$(BINARY) .
	@echo "Built $(BUILD_DIR)/$(BINARY)"

sdk:
	@echo "Building TypeScript SDK..."
	@bun run build
	@echo "Built dist/enclave.js"

dev:
	@echo "Starting dev server at http://localhost:8080"
	@cd example && bun run dev

# === Testing ===

test:
	@go test -v ./...

test-plugin: build
	@echo "Testing generate()..."
	@extism call $(BUILD_DIR)/$(BINARY) generate --input '{"credential":"dGVzdC1jcmVkZW50aWFs"}' --wasi
	@echo "\nTesting query()..."
	@extism call $(BUILD_DIR)/$(BINARY) query --input '{"did":""}' --wasi

test-sdk: sdk
	@cd example && bun run test

# === Code Quality ===

lint:
	@golangci-lint run ./...

fmt:
	@go fmt ./...
	@bun run --filter '*' format 2>/dev/null || true

generate:
	@sqlc generate

# === Utilities ===

clean:
	@rm -f $(BUILD_DIR)/$(BINARY)
	@rm -rf dist

help:
	@echo "Motr Enclave"
	@echo ""
	@echo "  make start       - Full setup + dev server (recommended)"
	@echo "  make build       - Build WASM plugin"
	@echo "  make sdk         - Build TypeScript SDK"
	@echo "  make dev         - Start dev server"
	@echo "  make test        - Run Go tests"
	@echo "  make test-plugin - Test plugin with Extism CLI"
	@echo "  make test-sdk    - Run SDK tests in browser"
	@echo "  make clean       - Remove build artifacts"
