# BZZZ Build System with Embedded Web UI - Dual Binary Support
.PHONY: build build-agent build-hap 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 - build both binaries
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 - both binaries
build: build-ui embed-ui build-agent build-hap

# 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 autonomous agent binary
build-agent: build-ui embed-ui
	@echo "🔨 Building BZZZ Agent binary with embedded web UI..."
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 go build -ldflags="-s -w" -o $(BUILD_DIR)/bzzz-agent ./cmd/agent
	@echo "✅ BZZZ Agent binary built successfully: $(BUILD_DIR)/bzzz-agent"

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

# Legacy build target for backward compatibility
build-go: build-agent
	@echo "⚠️ build-go is deprecated, use build-agent or build-hap"

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

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

# 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-agent:
	@echo "⚡ Quick agent build (Go only)..."
	@mkdir -p $(BUILD_DIR)
	go build -o $(BUILD_DIR)/bzzz-agent ./cmd/agent
	@echo "✅ Quick agent build complete"

quick-build-hap:
	@echo "⚡ Quick HAP build (Go only)..."
	@mkdir -p $(BUILD_DIR)
	go build -o $(BUILD_DIR)/bzzz-hap ./cmd/hap
	@echo "✅ Quick HAP build complete"

# Quick build both binaries
quick-build: quick-build-agent quick-build-hap

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

# Help
help:
	@echo "BZZZ Dual-Binary Build System"
	@echo ""
	@echo "Available targets:"
	@echo "  all              - Build both binaries with embedded UI (default)"
	@echo "  build            - Build both binaries with embedded UI"
	@echo "  build-agent      - Build autonomous agent binary only"
	@echo "  build-hap        - Build human agent portal binary only"
	@echo "  build-ui         - Build React web UI 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 both binaries system-wide"
	@echo "  test             - Run tests"
	@echo "  clean            - Clean build artifacts"
	@echo "  quick-build      - Quick build both binaries (Go only)"
	@echo "  quick-build-agent- Quick build agent binary only"
	@echo "  quick-build-hap  - Quick build HAP binary only"
	@echo "  docker-build     - Build Docker image"
	@echo "  help             - Show this help"
	@echo ""
	@echo "Binaries:"
	@echo "  bzzz-agent       - Autonomous AI agent for task execution"
	@echo "  bzzz-hap         - Human Agent Portal for interactive coordination"