# HotPlex Worker Gateway - Development Makefile
# Not for production service management.
#
#   make help        Show commands
#   make quickstart  First-time setup
#   make dev         Start dev environment
#   make check       Quality check (CI)

# ─────────────────────────────────────────────────────────────────────────────
# Configuration
# ─────────────────────────────────────────────────────────────────────────────

BINARY_NAME  := hotplex
BUILD_DIR    := bin
MAIN_PATH    := ./cmd/hotplex
CONFIG_DIR   := configs
LOG_DIR      := logs

GO_VERSION   := $(shell go version | cut -d' ' -f3)
GOOS         := $(shell go env GOOS)
GOARCH       := $(shell go env GOARCH)
GIT_SHA      := $(shell git rev-parse --short=8 HEAD 2>/dev/null || echo "unknown")
BUILD_TIME   := $(shell date '+%Y-%m-%dT%H:%M:%S%z')
LDFLAGS      := -s -w -X main.version=v1.16.0 -X main.buildTime=$(BUILD_TIME)
BUILD_OPTS   := -trimpath

GATEWAY_PID   := $(HOME)/.hotplex/.pids/gateway.pid
GATEWAY_LOG   := $(LOG_DIR)/hotplex.log
WEB_CHAT_PID  := $(HOME)/.hotplex/.pids/hotplex-webchat.pid
WEB_CHAT_PORT := 3000
WEB_CHAT_LOG  := $(CURDIR)/$(LOG_DIR)/webchat.log
WEB_CHAT_DIR  := webchat
WEB_CHAT_OUT  := internal/webchat/out
GRACE_PERIOD  := 7

# ─────────────────────────────────────────────────────────────────────────────
# Color
# ─────────────────────────────────────────────────────────────────────────────

RESET  := \033[0m
BOLD   := \033[1m
DIM    := \033[2m
RED    := \033[31m
GREEN  := \033[32m
YELLOW := \033[33m
CYAN   := \033[36m

# ─────────────────────────────────────────────────────────────────────────────
# PHONY
# ─────────────────────────────────────────────────────────────────────────────

.PHONY: all help quickstart hooks check-tools build build-windows build-one run
.PHONY: dev dev-start dev-stop dev-status dev-logs dev-reset
.PHONY: gateway-start gateway-stop gateway-status gateway-logs
.PHONY: webchat-dev webchat-stop webchat-embed webchat-rebuild
.PHONY: docs-build docs-clean docs-lint
.PHONY: test test-short lint fmt quality check clean

# ─────────────────────────────────────────────────────────────────────────────
# Default
# ─────────────────────────────────────────────────────────────────────────────

all: help

# ─────────────────────────────────────────────────────────────────────────────
# Setup
# ─────────────────────────────────────────────────────────────────────────────

quickstart: hooks
	@if command -v go > /dev/null 2>&1; then \
		$(MAKE) check-tools build test-short; \
		echo ""; \
		echo "  $(GREEN)✓ Developer setup complete$(RESET)"; \
		echo ""; \
		echo "    make dev      Start dev environment"; \
		echo "    make run      Run gateway"; \
		echo "    make help     Show all commands"; \
		echo ""; \
	else \
		echo ""; \
		echo "  $(GREEN)✓ Quickstart complete$(RESET)"; \
		echo ""; \
		echo "    $(DIM)Go not detected — skipping build & tests.$(RESET)"; \
		echo ""; \
		echo "    $(BOLD)Next steps:$(RESET)"; \
		echo "      1. Download binary from releases"; \
		echo "      2. Run $(CYAN)hotplex onboard$(RESET) to configure"; \
		echo "      3. Run $(CYAN)hotplex gateway start$(RESET) to launch"; \
		echo ""; \
	fi

check-tools:
	@$(call check-tool, go, "Go")
	@$(call check-tool, golangci-lint, "golangci-lint")
	@$(call check-tool, goimports, "goimports")

hooks:
	@echo "$(CYAN)Installing git hooks...$(RESET)"
	@for hook in scripts/git-hooks/*; do \
		name=$$(basename "$$hook"); \
		target=".git/hooks/$$name"; \
		if [ -L "$$target" ]; then \
			echo "  $(GREEN)✓$(RESET) $$name (symlink exists)"; \
		elif [ -f "$$target" ]; then \
			echo "  $(YELLOW)⚠$(RESET) $$name (regular file, skipping — remove manually and re-run)"; \
		else \
			ln -s "$(PWD)/$$hook" "$$target" && \
			echo "  $(GREEN)✓$(RESET) $$name → $$hook"; \
		fi; \
	done
	@echo "  $(DIM)Pre-push runs: fmt → lint → vet → mod verify → build → test$(RESET)"

define check-tool
	@if command -v $(1) > /dev/null 2>&1; then \
		echo "  $(GREEN)✓$(RESET) $(2)"; \
	else \
		echo "  $(YELLOW)⚠$(RESET) $(2) $(DIM)(missing)$(RESET)"; \
	fi
endef

# ─────────────────────────────────────────────────────────────────────────────
# Build
# ─────────────────────────────────────────────────────────────────────────────

build: docs-build webchat-embed
	@echo "$(CYAN)Building...$(RESET)"
	@mkdir -p $(BUILD_DIR) $(LOG_DIR)
	@CGO_ENABLED=0 go build $(BUILD_OPTS) -ldflags="$(LDFLAGS)" \
		-o $(BUILD_DIR)/$(BINARY_NAME)-$(GOOS)-$(GOARCH) $(MAIN_PATH)
	@echo "  $(GREEN)✓$(RESET) $(BUILD_DIR)/$(BINARY_NAME)-$(GOOS)-$(GOARCH)"

build-windows:
	@echo "$(CYAN)Cross-compiling for Windows...$(RESET)"
	@mkdir -p $(BUILD_DIR)
	@$(MAKE) build-one GOOS=windows GOARCH=amd64 SUFFIX=.exe --no-print-directory
	@$(MAKE) build-one GOOS=windows GOARCH=arm64 SUFFIX=.exe --no-print-directory

build-one:
	@CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(BUILD_OPTS) -ldflags="$(LDFLAGS)" \
		-o $(BUILD_DIR)/$(BINARY_NAME)-$(GOOS)-$(GOARCH)$(SUFFIX) $(MAIN_PATH)
	@echo "  $(GREEN)✓$(RESET) $(BUILD_DIR)/$(BINARY_NAME)-$(GOOS)-$(GOARCH)$(SUFFIX)"

run: build
	@./$(BUILD_DIR)/$(BINARY_NAME)-$(GOOS)-$(GOARCH) \
		gateway start -c $(CONFIG_DIR)/config-dev.yaml

# ─────────────────────────────────────────────────────────────────────────────
# Test
# ─────────────────────────────────────────────────────────────────────────────

test:
	@echo "$(CYAN)Testing...$(RESET)"
	@GORACE="history_size=5" go test -race -timeout 15m ./...
	@echo "  $(GREEN)✓ Tests passed$(RESET)"

test-short:
	@echo "$(CYAN)Testing...$(RESET)"
	@GORACE="history_size=5" go test -short -race -timeout 5m ./...
	@echo "  $(GREEN)✓ Tests passed$(RESET)"

coverage:
	@echo "$(CYAN)Generating coverage report...$(RESET)"
	@go test -timeout=15m -coverprofile=coverage.out -covermode=atomic \
		$$(go list ./... | grep -v -e 'internal/worker/proc' -e 'internal/worker/pi' -e 'cmd/hotplex')
	@echo ""
	@echo "$(BOLD)Per-package coverage:$(RESET)"
	@go tool cover -func=coverage.out | grep -v "^total:" | sort -t: -k3 -n
	@echo ""
	@TOTAL=$$(go tool cover -func=coverage.out | tail -1 | grep -oP '\d+\.\d+') ; \
		echo "  $(BOLD)Total: $${TOTAL}%$(RESET)"

test-slack-e2e:
	@echo "$(CYAN)Running Slack semi-automated E2E tests...$(RESET)"
	@test -n "$$SLACK_BOT_TOKEN" || (echo "  $(RED)SLACK_BOT_TOKEN required$(RESET)"; exit 1)
	@test -n "$$SLACK_APP_TOKEN" || (echo "  $(RED)SLACK_APP_TOKEN required$(RESET)"; exit 1)
	@go test -v -tags=slack_e2e -timeout 30m ./internal/messaging/slack/...

# ─────────────────────────────────────────────────────────────────────────────
# Quality
# ─────────────────────────────────────────────────────────────────────────────

lint:
	@echo "$(CYAN)Linting...$(RESET)"
	@golangci-lint run ./...

fmt:
	@echo "$(CYAN)Formatting...$(RESET)"
	@go fmt ./...
	@if command -v goimports > /dev/null 2>&1; then goimports -w .; fi

quality: fmt lint test
	@echo ""
	@echo "  $(GREEN)✓ All checks passed$(RESET)"
	@echo ""

check: quality build
	@echo "  $(GREEN)✓ CI check passed$(RESET)"

# ─────────────────────────────────────────────────────────────────────────────
# Dev Environment
# ─────────────────────────────────────────────────────────────────────────────

dev: dev-start
	@echo ""
	@echo "  $(DIM)─────────────────────────────────────$(RESET)"
	@echo "  $(GREEN)✓ Dev environment ready$(RESET)"
	@echo ""
	@printf "    make %-12s %s\n" "dev-logs" "View logs"
	@printf "    make %-12s %s\n" "dev-status" "Check status"
	@printf "    make %-12s %s\n" "dev-stop" "Stop all"
	@echo ""

dev-start: gateway-start
	@$(MAKE) webchat-dev || echo "  $(YELLOW)⚠$(RESET) Webchat skipped (run 'cd webchat && pnpm install' to fix)"

dev-stop: webchat-stop gateway-stop
	@echo "  $(GREEN)✓ Dev environment stopped$(RESET)"

dev-status:
	@./scripts/dev.sh status all

dev-logs:
	@./scripts/dev.sh logs all

dev-reset: dev-stop dev-start

# ─────────────────────────────────────────────────────────────────────────────
# Gateway
# ─────────────────────────────────────────────────────────────────────────────

gateway-start: build
	@./scripts/dev.sh start gateway

gateway-stop:
	@./scripts/dev.sh stop gateway

gateway-status:
	@./scripts/dev.sh status gateway

gateway-logs:
	@./scripts/dev.sh logs gateway

# ─────────────────────────────────────────────────────────────────────────────
# Webchat
# ─────────────────────────────────────────────────────────────────────────────

webchat-dev:
	@./scripts/dev.sh start webchat

webchat-stop:
	@./scripts/dev.sh stop webchat

webchat-embed:
	@if [ ! -d $(WEB_CHAT_OUT)/_next ]; then \
		echo "$(CYAN)Building webchat for embedding...$(RESET)"; \
		cd $(WEB_CHAT_DIR) && pnpm install --frozen-lockfile && pnpm build && \
		rm -rf ../$(WEB_CHAT_OUT).tmp && cp -r out ../$(WEB_CHAT_OUT).tmp && \
		rm -rf ../$(WEB_CHAT_OUT) && mv ../$(WEB_CHAT_OUT).tmp ../$(WEB_CHAT_OUT); \
	fi

webchat-rebuild:
	@echo "$(CYAN)Rebuilding webchat...$(RESET)"
	@cd $(WEB_CHAT_DIR) && pnpm build && \
	rm -rf ../$(WEB_CHAT_OUT).tmp && cp -r out ../$(WEB_CHAT_OUT).tmp && \
	rm -rf ../$(WEB_CHAT_OUT) && mv ../$(WEB_CHAT_OUT).tmp ../$(WEB_CHAT_OUT)
	@echo "  $(GREEN)✓$(RESET) Webchat rebuilt"

# ─────────────────────────────────────────────────────────────────────────────
# Documentation
# ─────────────────────────────────────────────────────────────────────────────

docs-build:
	@echo "$(CYAN)Building documentation...$(RESET)"
	@go run cmd/build-docs/main.go
	@echo "  $(GREEN)✓$(RESET) Documentation built"

docs-clean:
	@rm -rf internal/docs/out
	@echo "  $(GREEN)✓$(RESET) Documentation cleaned"

docs-lint: docs-build
	@echo "$(CYAN)Docs link validation passed$(RESET)"

# ─────────────────────────────────────────────────────────────────────────────
# Clean
# ─────────────────────────────────────────────────────────────────────────────

clean:
	@go clean
	@rm -rf $(BUILD_DIR)
	@rm -f coverage.out
	@echo "  $(GREEN)✓$(RESET) Cleaned"

# ─────────────────────────────────────────────────────────────────────────────
# Help
# ─────────────────────────────────────────────────────────────────────────────

help:
	@echo ""
	@echo "  $(CYAN)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━$(RESET)"
	@echo "  $(CYAN)  ⚡ HotPlex Worker$(RESET)  $(GIT_SHA)  $(GOOS)/$(GOARCH)"
	@echo "  $(CYAN)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━$(RESET)"
	@echo ""
	@echo "  $(BOLD)⚡ Start"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "dev"         "Start all services (gateway + webchat)"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "dev-start"   "Start individually"
	@echo ""
	@echo "  $(BOLD)⏹  Stop"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "dev-stop"      "Stop all services"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "gateway-stop"   "Stop gateway"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "webchat-stop"  "Stop webchat"
	@echo ""
	@echo "  $(BOLD)🔧 Build"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "build"          "Build binary"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "build-windows"  "Cross-compile for Windows"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "run"     "Build and run (foreground)"
	@echo ""
	@echo "  $(BOLD)🧪 Test & Quality"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "test"         "All tests (race, 15m)"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "test-short"   "Short tests (5m)"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "lint"         "Run linter"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "fmt"          "Format code"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "quality"       "fmt + lint + test"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "check"         "quality + build (CI)"
	@echo ""
	@echo "  $(BOLD)📊 Status & Logs"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "dev-status"     "All services"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "gateway-status"  "Gateway"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "dev-logs"      "View all logs"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "gateway-logs"   "Gateway logs"
	@echo ""
	@echo "  $(BOLD)📖 Documentation"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "docs-build"    "Build static HTML docs"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "docs-clean"    "Remove generated docs"
	@echo ""
	@echo "  $(BOLD)🔄 Workflow"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "dev-reset"   "Restart all services"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "quickstart"  "First-time setup"
	@echo ""
	@echo "  $(BOLD)🧹 Other"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "clean"        "Clean artifacts"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "check-tools"  "Check dev tools"
	@printf "    $(CYAN)make %-15s$(RESET)  %s\n" "hooks"        "Install git hooks"
	@echo ""
	@echo "  $(DIM)Try:  make dev | make test | make check"
	@echo ""

# Catch-all
%:
	@echo ""
	@echo "  $(RED)Unknown: make $@$(RESET)"
	@echo "    make help  Show commands"
	@echo ""
	@exit 1
