# ================================================
# Urban Lifeline 微服务管理 Makefile
# ================================================

# 项目根目录
PROJECT_ROOT := $(shell pwd)

# Maven 命令
MAVEN := mvn
MAVEN_OPTS := -Dmaven.test.skip=true

# 服务列表
SERVICES := gateway auth system log file message bidding platform workcase crontab agent

# 服务端口映射
PORT_gateway := 8080
PORT_auth := 8081
PORT_system := 8082
PORT_log := 8083
PORT_file := 8084
PORT_message := 8085
PORT_bidding := 8086
PORT_platform := 8087
PORT_workcase := 8088
PORT_crontab := 8089
PORT_agent := 8090

# 服务主类映射
MAINCLASS_gateway := org.xyzh.gateway.GatewayApplication
MAINCLASS_auth := org.xyzh.auth.AuthApp
MAINCLASS_system := org.xyzh.system.SystemApp
MAINCLASS_log := org.xyzh.log.LogApp
MAINCLASS_file := org.xyzh.file.FileApp
MAINCLASS_message := org.xyzh.message.MessageApp
MAINCLASS_bidding := org.xyzh.bidding.BiddingApp
MAINCLASS_platform := org.xyzh.platform.PlatformApp
MAINCLASS_workcase := org.xyzh.workcase.WorkcaseApp
MAINCLASS_crontab := org.xyzh.crontab.CrontabApp
MAINCLASS_agent := org.xyzh.agent.AgentApp

# PID 文件目录
PID_DIR := $(PROJECT_ROOT)/.pids
LOG_DIR := $(PROJECT_ROOT)/.logs

# 颜色定义
COLOR_RESET := \033[0m
COLOR_GREEN := \033[0;32m
COLOR_YELLOW := \033[0;33m
COLOR_BLUE := \033[0;34m
COLOR_RED := \033[0;31m

# ================================================
# 帮助信息
# ================================================
.PHONY: help
help:
	@echo "$(COLOR_BLUE)=============================================$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)  Urban Lifeline 微服务管理工具$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)=============================================$(COLOR_RESET)"
	@echo ""
	@echo "$(COLOR_GREEN)构建命令:$(COLOR_RESET)"
	@echo "  make build              - 构建所有服务"
	@echo "  make build-<service>    - 构建指定服务 (如: make build-system)"
	@echo "  make clean              - 清理所有服务"
	@echo ""
	@echo "$(COLOR_GREEN)启动命令:$(COLOR_RESET)"
	@echo "  make start              - 启动所有服务"
	@echo "  make start-<service>    - 启动指定服务 (如: make start-system)"
	@echo "  make start-core         - 启动核心服务 (gateway, auth, system)"
	@echo ""
	@echo "$(COLOR_GREEN)停止命令:$(COLOR_RESET)"
	@echo "  make stop               - 停止所有服务"
	@echo "  make stop-<service>     - 停止指定服务 (如: make stop-system)"
	@echo ""
	@echo "$(COLOR_GREEN)重启命令:$(COLOR_RESET)"
	@echo "  make restart            - 重启所有服务"
	@echo "  make restart-<service>  - 重启指定服务 (如: make restart-system)"
	@echo ""
	@echo "$(COLOR_GREEN)状态查看:$(COLOR_RESET)"
	@echo "  make status             - 查看所有服务状态"
	@echo "  make status-<service>   - 查看指定服务状态"
	@echo "  make logs-<service>     - 查看指定服务日志"
	@echo ""
	@echo "$(COLOR_GREEN)可用服务:$(COLOR_RESET)"
	@echo "  $(SERVICES)"
	@echo ""

# ================================================
# 初始化目录
# ================================================
.PHONY: init-dirs
init-dirs:
	@mkdir -p $(PID_DIR)
	@mkdir -p $(LOG_DIR)

# ================================================
# 构建相关
# ================================================
.PHONY: build
build: init-dirs
	@echo "$(COLOR_YELLOW)开始构建所有服务...$(COLOR_RESET)"
	@$(MAVEN) clean install $(MAVEN_OPTS)
	@echo "$(COLOR_GREEN)✓ 所有服务构建完成$(COLOR_RESET)"

.PHONY: $(addprefix build-,$(SERVICES))
$(addprefix build-,$(SERVICES)): build-%:
	@echo "$(COLOR_YELLOW)开始构建 $* 服务...$(COLOR_RESET)"
	@cd $(PROJECT_ROOT)/$* && $(MAVEN) clean install $(MAVEN_OPTS)
	@echo "$(COLOR_GREEN)✓ $* 服务构建完成$(COLOR_RESET)"

.PHONY: clean
clean:
	@echo "$(COLOR_YELLOW)开始清理所有服务...$(COLOR_RESET)"
	@$(MAVEN) clean
	@rm -rf $(PID_DIR)
	@rm -rf $(LOG_DIR)
	@echo "$(COLOR_GREEN)✓ 清理完成$(COLOR_RESET)"

# ================================================
# 启动服务
# ================================================
.PHONY: start
start: init-dirs $(addprefix start-,$(SERVICES))
	@echo "$(COLOR_GREEN)✓ 所有服务启动完成$(COLOR_RESET)"

.PHONY: start-core
start-core: init-dirs start-gateway start-auth start-system
	@echo "$(COLOR_GREEN)✓ 核心服务启动完成$(COLOR_RESET)"

.PHONY: $(addprefix start-,$(SERVICES))
$(addprefix start-,$(SERVICES)): start-%:
	@if [ -f "$(PID_DIR)/$*.pid" ] && kill -0 $$(cat $(PID_DIR)/$*.pid) 2>/dev/null; then \
		echo "$(COLOR_YELLOW)⚠ $* 服务已在运行中 (PID: $$(cat $(PID_DIR)/$*.pid))$(COLOR_RESET)"; \
	else \
		echo "$(COLOR_BLUE)启动 $* 服务...$(COLOR_RESET)"; \
		nohup $(MAVEN) -pl $* spring-boot:run > $(LOG_DIR)/$*.log 2>&1 & \
		echo $$! > $(PID_DIR)/$*.pid; \
		sleep 2; \
		if kill -0 $$(cat $(PID_DIR)/$*.pid) 2>/dev/null; then \
			echo "$(COLOR_GREEN)✓ $* 服务启动成功 (PID: $$(cat $(PID_DIR)/$*.pid), Port: $(PORT_$*))$(COLOR_RESET)"; \
		else \
			echo "$(COLOR_RED)✗ $* 服务启动失败$(COLOR_RESET)"; \
			rm -f $(PID_DIR)/$*.pid; \
		fi; \
	fi

# ================================================
# 停止服务
# ================================================
.PHONY: stop
stop: $(addprefix stop-,$(SERVICES))
	@echo "$(COLOR_GREEN)✓ 所有服务停止完成$(COLOR_RESET)"

.PHONY: $(addprefix stop-,$(SERVICES))
$(addprefix stop-,$(SERVICES)): stop-%:
	@if [ -f "$(PID_DIR)/$*.pid" ]; then \
		PID=$$(cat $(PID_DIR)/$*.pid); \
		if kill -0 $$PID 2>/dev/null; then \
			echo "$(COLOR_YELLOW)停止 $* 服务 (PID: $$PID)...$(COLOR_RESET)"; \
			kill $$PID; \
			sleep 2; \
			if kill -0 $$PID 2>/dev/null; then \
				echo "$(COLOR_RED)强制停止 $* 服务...$(COLOR_RESET)"; \
				kill -9 $$PID; \
			fi; \
			rm -f $(PID_DIR)/$*.pid; \
			echo "$(COLOR_GREEN)✓ $* 服务已停止$(COLOR_RESET)"; \
		else \
			echo "$(COLOR_YELLOW)⚠ $* 服务未运行$(COLOR_RESET)"; \
			rm -f $(PID_DIR)/$*.pid; \
		fi; \
	else \
		echo "$(COLOR_YELLOW)⚠ $* 服务未运行$(COLOR_RESET)"; \
	fi

# ================================================
# 重启服务
# ================================================
.PHONY: restart
restart: stop start
	@echo "$(COLOR_GREEN)✓ 所有服务重启完成$(COLOR_RESET)"

.PHONY: $(addprefix restart-,$(SERVICES))
$(addprefix restart-,$(SERVICES)): restart-%: stop-% start-%
	@echo "$(COLOR_GREEN)✓ $* 服务重启完成$(COLOR_RESET)"

# ================================================
# 查看状态
# ================================================
.PHONY: status
status:
	@echo "$(COLOR_BLUE)=============================================$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)  服务运行状态$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)=============================================$(COLOR_RESET)"
	@for service in $(SERVICES); do \
		printf "%-15s" "$$service:"; \
		if [ -f "$(PID_DIR)/$$service.pid" ]; then \
			PID=$$(cat $(PID_DIR)/$$service.pid); \
			if kill -0 $$PID 2>/dev/null; then \
				echo "$(COLOR_GREEN)✓ Running (PID: $$PID)$(COLOR_RESET)"; \
			else \
				echo "$(COLOR_RED)✗ Stopped$(COLOR_RESET)"; \
				rm -f $(PID_DIR)/$$service.pid; \
			fi; \
		else \
			echo "$(COLOR_YELLOW)○ Not Started$(COLOR_RESET)"; \
		fi; \
	done
	@echo ""

.PHONY: $(addprefix status-,$(SERVICES))
$(addprefix status-,$(SERVICES)): status-%:
	@if [ -f "$(PID_DIR)/$*.pid" ]; then \
		PID=$$(cat $(PID_DIR)/$*.pid); \
		if kill -0 $$PID 2>/dev/null; then \
			echo "$(COLOR_GREEN)✓ $* 服务运行中 (PID: $$PID, Port: $(PORT_$*))$(COLOR_RESET)"; \
		else \
			echo "$(COLOR_RED)✗ $* 服务已停止$(COLOR_RESET)"; \
		fi; \
	else \
		echo "$(COLOR_YELLOW)○ $* 服务未启动$(COLOR_RESET)"; \
	fi

# ================================================
# 查看日志
# ================================================
.PHONY: $(addprefix logs-,$(SERVICES))
$(addprefix logs-,$(SERVICES)): logs-%:
	@if [ -f "$(LOG_DIR)/$*.log" ]; then \
		tail -f $(LOG_DIR)/$*.log; \
	else \
		echo "$(COLOR_YELLOW)⚠ $* 服务日志文件不存在$(COLOR_RESET)"; \
	fi

# ================================================
# 快捷命令
# ================================================
.PHONY: dev
dev: start-core
	@echo "$(COLOR_GREEN)✓ 开发环境启动完成 (gateway, auth, system)$(COLOR_RESET)"

.PHONY: prod
prod: start
	@echo "$(COLOR_GREEN)✓ 生产环境启动完成$(COLOR_RESET)"

# 默认目标
.DEFAULT_GOAL := help
