# DBT Runner - Makefile
# Builds and packages the extension for distribution

SHELL := bash
.SHELLFLAGS := -euo pipefail -c

NVM_DIR := $(HOME)/.nvm
NVM_SH  := $(NVM_DIR)/nvm.sh

# Detect the latest .vsix after packaging
VSIX_FILE = $(shell find . -maxdepth 1 -name "*.vsix" -type f -printf '%T@ %p\n' 2>/dev/null | sort -rn | head -1 | cut -d' ' -f2-)

.PHONY: all build install compile package clean check-node

# Default target: full build
all: check-node install compile package
	@echo ""
	@echo "=========================================="
	@echo "✅ Build complete!"
	@echo "=========================================="
	@if [ -n "$(VSIX_FILE)" ]; then \
		FILE_SIZE=$$(du -h "$(VSIX_FILE)" | cut -f1); \
		echo ""; \
		echo "📦 Extension package: $(VSIX_FILE) ($$FILE_SIZE)"; \
		echo ""; \
		echo "🚀 Next steps:"; \
		echo "   1. Share the .vsix file with your team"; \
		echo "   2. Team members install with:"; \
		echo "      code --install-extension $(VSIX_FILE)"; \
		echo ""; \
		echo "   Or via VS Code UI:"; \
		echo "   - Extensions → ⋯ → Install from VSIX..."; \
	else \
		echo "❌ Could not find .vsix file"; \
		exit 1; \
	fi

# Alias: same as all
build: all

##
## Step 0 – Ensure Node.js / npm are available
##
check-node:
	@echo "=========================================="
	@echo "DBT Runner - Build"
	@echo "=========================================="
	@echo ""
	@# --- WSL guard: reject Windows Node/npm --------------------------------
	@NODE_PATH=$$(command -v node 2>/dev/null || true); \
	 NPM_PATH=$$(command -v npm  2>/dev/null || true); \
	 if [[ "$$NPM_PATH" == /mnt/c/* ]] || [[ "$$NODE_PATH" == /mnt/c/* ]]; then \
	   echo "❌ Windows Node/npm detected in WSL."; \
	   echo ""; \
	   echo "   npm path : $$NPM_PATH"; \
	   echo "   node path: $$NODE_PATH"; \
	   echo ""; \
	   echo "This causes compile errors (UNC paths not supported)."; \
	   echo ""; \
	   echo "✅ Fix: install Linux Node with nvm"; \
	   echo "   1) curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash"; \
	   echo "   2) source ~/.bashrc"; \
	   echo "   3) nvm install --lts && nvm use --lts"; \
	   echo "   4) hash -r"; \
	   exit 1; \
	 fi
	@# --- Bootstrap Node via nvm if not present -----------------------------
	@if ! command -v node >/dev/null 2>&1 || ! command -v npm >/dev/null 2>&1; then \
	   echo "⚙️  Node.js/npm not found. Bootstrapping with nvm..."; \
	   if ! command -v curl >/dev/null 2>&1; then \
	     echo "❌ curl is required to install nvm automatically. Install curl and rerun."; \
	     exit 1; \
	   fi; \
	   if [ ! -s "$(NVM_SH)" ]; then \
	     echo "⬇️  Installing nvm..."; \
	     curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash; \
	   fi; \
	   if [ -s "$(NVM_SH)" ]; then . "$(NVM_SH)"; fi; \
	   if ! command -v nvm >/dev/null 2>&1; then \
	     echo "❌ nvm could not be loaded. Run: source ~/.bashrc  then rerun: make"; \
	     exit 1; \
	   fi; \
	   echo "⬇️  Installing Node.js LTS..."; \
	   nvm install --lts; \
	   nvm use --lts; \
	   hash -r; \
	   if ! command -v node >/dev/null 2>&1 || ! command -v npm >/dev/null 2>&1; then \
	     echo "❌ Node.js/npm installation did not complete. Run: source ~/.bashrc  then rerun: make"; \
	     exit 1; \
	   fi; \
	   echo "✅ Node.js ready: $$(node --version), npm: $$(npm --version)"; \
	 fi

##
## Step 1 – Install npm dependencies
##
install: check-node
	@echo "📦 Step 1: Installing dependencies..."
	npm install

##
## Step 2 – Compile TypeScript
##
compile: install
	@echo ""
	@echo "🔨 Step 2: Compiling TypeScript..."
	@if [ -f "./node_modules/typescript/bin/tsc" ]; then \
	   node ./node_modules/typescript/bin/tsc -p ./; \
	 else \
	   npm run compile; \
	 fi

##
## Step 3 – Package into .vsix
##
package: compile
	@echo ""
	@echo "📋 Step 3: Packaging extension..."
	npm run package

##
## Utility targets
##
clean:
	@echo "🧹 Cleaning build artefacts..."
	rm -rf out/
	rm -f *.vsix
	@echo "✅ Clean complete."
