# BZZZ Build System with Embedded Web UI
.PHONY: build build-ui build-go clean dev setup install deps test

# Configuration
UI_DIR = install/config-ui
BUILD_DIR = build
DIST_DIR = $(UI_DIR)/dist
EMBED_DIR = pkg/web

# Default target
all: build

# Install dependencies
deps:
	@echo "📦 Installing Go dependencies..."
	go mod download
	go mod tidy
	@echo "📦 Installing Node.js dependencies..."
	cd $(UI_DIR) && npm install

# Development mode - run both Go and React in development
dev:
	@echo "🚀 Starting development mode..."
	@echo "   Go API: http://localhost:8080"
	@echo "   React UI: http://localhost:3000"
	cd $(UI_DIR) && npm run dev &
	go run main.go

# Build the complete application
build: build-ui embed-ui build-go

# Build the React web UI
build-ui:
	@echo "🔨 Building React web UI..."
	@mkdir -p $(BUILD_DIR)
	cd $(UI_DIR) && npm ci
	cd $(UI_DIR) && npm run build
	@echo "✅ Web UI built successfully"

# Embed the web UI into Go source
embed-ui: build-ui
	@echo "📦 Embedding web UI into Go binary..."
	@mkdir -p $(EMBED_DIR)
	@cp -r $(UI_DIR)/out/* $(EMBED_DIR)/ 2>/dev/null || cp -r $(UI_DIR)/.next/static $(EMBED_DIR)/ 2>/dev/null || true
	@echo "✅ Web UI embedded successfully"

# Build the Go binary with embedded UI
build-go:
	@echo "🔨 Building Go binary with embedded web UI..."
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 go build -ldflags="-s -w" -o $(BUILD_DIR)/bzzz .
	@echo "✅ BZZZ binary built successfully: $(BUILD_DIR)/bzzz"

# Setup development environment
setup: deps
	@echo "🔧 Setting up development environment..."
	@mkdir -p $(BUILD_DIR)
	@mkdir -p $(EMBED_DIR)
	@echo "✅ Development environment ready"

# Install BZZZ system-wide
install: build
	@echo "📥 Installing BZZZ..."
	sudo cp $(BUILD_DIR)/bzzz /usr/local/bin/
	sudo chmod +x /usr/local/bin/bzzz
	@echo "✅ BZZZ installed to /usr/local/bin/bzzz"

# Run tests
test:
	@echo "🧪 Running tests..."
	go test -v ./...

# Clean build artifacts
clean:
	@echo "🧹 Cleaning build artifacts..."
	rm -rf $(BUILD_DIR)
	rm -rf $(EMBED_DIR)
	rm -rf $(UI_DIR)/node_modules
	rm -rf $(UI_DIR)/.next
	rm -rf $(UI_DIR)/out
	rm -rf $(UI_DIR)/dist
	@echo "✅ Clean complete"

# Quick build for development (skip UI rebuild if not changed)
quick-build:
	@echo "⚡ Quick build (Go only)..."
	@mkdir -p $(BUILD_DIR)
	go build -o $(BUILD_DIR)/bzzz .
	@echo "✅ Quick build complete"

# Docker build
docker-build:
	@echo "🐳 Building Docker image..."
	docker build -t bzzz:latest .
	@echo "✅ Docker image built"

# Help
help:
	@echo "BZZZ Build System"
	@echo ""
	@echo "Available targets:"
	@echo "  all         - Build complete application (default)"
	@echo "  build       - Build complete application with embedded UI"
	@echo "  build-ui    - Build React web UI only"
	@echo "  build-go    - Build Go binary only"
	@echo "  embed-ui    - Embed web UI into Go source"
	@echo "  dev         - Start development mode"
	@echo "  setup       - Setup development environment"
	@echo "  deps        - Install dependencies"
	@echo "  install     - Install BZZZ system-wide"
	@echo "  test        - Run tests"
	@echo "  clean       - Clean build artifacts"
	@echo "  quick-build - Quick Go-only build"
	@echo "  docker-build- Build Docker image"
	@echo "  help        - Show this help"