# ================================================
# Urban Lifeline - Docker 构建管理 Makefile
# ================================================
# 使用方法:
#   make help              - 显示帮助信息
#   make build-all         - 构建所有镜像
#   make build-serv        - 构建所有后端服务镜像
#   make build-gateway     - 构建单个服务镜像
#   make save-all          - 导出所有镜像
#   make up                - 启动所有服务
#   make down              - 停止所有服务
# ================================================

# 项目根目录（docker 目录的上级）
PROJECT_ROOT := $(shell cd .. && pwd)
DOCKER_DIR := $(shell pwd)

# 镜像版本
IMAGE_VERSION ?= $(shell date +%Y%m%d_%H%M%S)
IMAGE_REGISTRY ?= 

# 服务列表
SERV_SERVICES := gateway system auth file ai workcase
WEB_SERVICES := platform workcase-web

# 服务端口映射
PORT_gateway := 8080
PORT_system := 8082
PORT_auth := 8081
PORT_file := 8084
PORT_ai := 8090
PORT_workcase := 8088

# 输出目录
BUILD_OUTPUT := $(DOCKER_DIR)/output

# 颜色定义
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 Docker 构建管理$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)=============================================$(COLOR_RESET)"
	@echo ""
	@echo "$(COLOR_GREEN)构建命令:$(COLOR_RESET)"
	@echo "  make build-base        - 构建基础镜像"
	@echo "  make build-all         - 构建所有镜像"
	@echo "  make build-serv        - 构建所有后端服务镜像"
	@echo "  make build-web         - 构建前端镜像"
	@echo "  make build-<service>   - 构建单个服务 (gateway/system/auth/file/ai/workcase)"
	@echo ""
	@echo "$(COLOR_GREEN)编译命令:$(COLOR_RESET)"
	@echo "  make compile-serv      - 编译所有后端服务"
	@echo "  make compile-<service> - 编译单个后端服务"
	@echo "  make compile-web       - 编译所有前端项目"
	@echo ""
	@echo "$(COLOR_GREEN)导出命令:$(COLOR_RESET)"
	@echo "  make save-all          - 导出所有镜像到 tar 文件"
	@echo "  make save-serv         - 导出所有后端镜像"
	@echo "  make save-<service>    - 导出单个服务镜像"
	@echo ""
	@echo "$(COLOR_GREEN)运行命令:$(COLOR_RESET)"
	@echo "  make up                - 启动所有服务"
	@echo "  make up-infra          - 启动基础设施 (nacos/minio/jitsi)"
	@echo "  make up-serv           - 启动后端服务"
	@echo "  make up-web            - 启动前端服务"
	@echo "  make down              - 停止所有服务"
	@echo "  make logs              - 查看所有日志"
	@echo "  make status            - 查看服务状态"
	@echo ""
	@echo "$(COLOR_GREEN)清理命令:$(COLOR_RESET)"
	@echo "  make clean             - 清理构建产物"
	@echo "  make clean-images      - 清理 Docker 镜像"
	@echo ""
	@echo "$(COLOR_YELLOW)当前版本: $(IMAGE_VERSION)$(COLOR_RESET)"
	@echo ""

# ================================================
# 初始化
# ================================================
.PHONY: init
init:
	@mkdir -p $(BUILD_OUTPUT)
	@mkdir -p $(DOCKER_DIR)/volumes/logs

# ================================================
# 编译后端服务
# ================================================
.PHONY: compile-serv
compile-serv:
	@echo "$(COLOR_YELLOW)编译所有后端服务...$(COLOR_RESET)"
	@cd $(PROJECT_ROOT)/urbanLifelineServ && mvn clean package -DskipTests
	@echo "$(COLOR_GREEN)✓ 后端服务编译完成$(COLOR_RESET)"

.PHONY: $(addprefix compile-,$(SERV_SERVICES))
$(addprefix compile-,$(SERV_SERVICES)): compile-%:
	@echo "$(COLOR_YELLOW)编译 $* 服务...$(COLOR_RESET)"
	@cd $(PROJECT_ROOT)/urbanLifelineServ && mvn clean package -DskipTests -pl $* -am
	@echo "$(COLOR_GREEN)✓ $* 服务编译完成$(COLOR_RESET)"

# ================================================
# 编译前端项目
# ================================================
.PHONY: compile-web
compile-web:
	@echo "$(COLOR_YELLOW)编译前端项目...$(COLOR_RESET)"
	@cd $(PROJECT_ROOT)/urbanLifelineWeb && pnpm install && pnpm run build:all
	@echo "$(COLOR_GREEN)✓ 前端项目编译完成$(COLOR_RESET)"

.PHONY: compile-platform
compile-platform:
	@echo "$(COLOR_YELLOW)编译 platform 项目...$(COLOR_RESET)"
	@cd $(PROJECT_ROOT)/urbanLifelineWeb/packages/platform && pnpm run build
	@echo "$(COLOR_GREEN)✓ platform 编译完成$(COLOR_RESET)"

.PHONY: compile-workcase
compile-workcase-web:
	@echo "$(COLOR_YELLOW)编译 workcase 项目...$(COLOR_RESET)"
	@cd $(PROJECT_ROOT)/urbanLifelineWeb/packages/workcase && pnpm run build
	@echo "$(COLOR_GREEN)✓ workcase 编译完成$(COLOR_RESET)"

# ================================================
# 构建 Docker 镜像
# ================================================
.PHONY: build-base
build-base:
	@echo "$(COLOR_YELLOW)构建基础镜像...$(COLOR_RESET)"
	@cd $(PROJECT_ROOT) && docker build \
		-t urban-lifeline-base-serv:$(IMAGE_VERSION) \
		-t urban-lifeline-base-serv:latest \
		-f docker/urbanLifeline/serv/Dockerfile.base .
	@echo "$(COLOR_GREEN)✓ 基础镜像构建完成$(COLOR_RESET)"

.PHONY: build-all
build-all: build-base build-serv build-web
	@echo "$(COLOR_GREEN)✓ 所有镜像构建完成$(COLOR_RESET)"

.PHONY: build-serv
build-serv: $(addprefix build-,$(SERV_SERVICES))
	@echo "$(COLOR_GREEN)✓ 所有后端服务镜像构建完成$(COLOR_RESET)"

# 构建单个后端服务镜像
.PHONY: $(addprefix build-,$(SERV_SERVICES))
$(addprefix build-,$(SERV_SERVICES)): build-%:
	@echo "$(COLOR_YELLOW)构建 $* 服务镜像...$(COLOR_RESET)"
	@if [ ! -f "$(PROJECT_ROOT)/urbanLifelineServ/$*/target/$*-*.jar" ] && \
	   [ ! -f "$(PROJECT_ROOT)/urbanLifelineServ/$*/target/*.jar" ]; then \
		echo "$(COLOR_RED)✗ JAR 包不存在，请先执行 make compile-$*$(COLOR_RESET)"; \
		exit 1; \
	fi
	@cd $(PROJECT_ROOT) && docker build \
		--build-arg SERVICE_NAME=$* \
		--build-arg SERVICE_PORT=$(PORT_$*) \
		-t urban-lifeline-$*:$(IMAGE_VERSION) \
		-t urban-lifeline-$*:latest \
		-f docker/urbanLifeline/serv/Dockerfile.template .
	@echo "$(COLOR_GREEN)✓ $* 镜像构建完成: urban-lifeline-$*:$(IMAGE_VERSION)$(COLOR_RESET)"

.PHONY: build-web
build-web: build-platform build-workcase-web
	@echo "$(COLOR_GREEN)✓ 所有前端镜像构建完成$(COLOR_RESET)"

.PHONY: build-platform
build-platform:
	@echo "$(COLOR_YELLOW)构建 platform 镜像...$(COLOR_RESET)"
	@if [ ! -d "$(PROJECT_ROOT)/urbanLifelineWeb/packages/platform/dist" ]; then \
		echo "$(COLOR_RED)✗ platform dist 不存在，请先执行 make compile-web$(COLOR_RESET)"; \
		exit 1; \
	fi
	@cd $(PROJECT_ROOT) && docker build \
		--build-arg WEB_NAME=platform \
		-t urban-lifeline-platform:$(IMAGE_VERSION) \
		-t urban-lifeline-platform:latest \
		-f docker/urbanLifeline/web/Dockerfile .
	@echo "$(COLOR_GREEN)✓ platform 镜像构建完成$(COLOR_RESET)"

.PHONY: build-workcase-web
build-workcase-web:
	@echo "$(COLOR_YELLOW)构建 workcase-web 镜像...$(COLOR_RESET)"
	@if [ ! -d "$(PROJECT_ROOT)/urbanLifelineWeb/packages/workcase/dist" ]; then \
		echo "$(COLOR_RED)✗ workcase dist 不存在，请先执行 make compile-web$(COLOR_RESET)"; \
		exit 1; \
	fi
	@cd $(PROJECT_ROOT) && docker build \
		--build-arg WEB_NAME=workcase \
		-t urban-lifeline-workcase-web:$(IMAGE_VERSION) \
		-t urban-lifeline-workcase-web:latest \
		-f docker/urbanLifeline/web/Dockerfile .
	@echo "$(COLOR_GREEN)✓ workcase-web 镜像构建完成$(COLOR_RESET)"

# ================================================
# 导出镜像
# ================================================
.PHONY: save-all
save-all: init save-serv save-web
	@echo "$(COLOR_GREEN)✓ 所有镜像导出完成$(COLOR_RESET)"
	@echo "输出目录: $(BUILD_OUTPUT)"
	@ls -lh $(BUILD_OUTPUT)/*.tar 2>/dev/null || true

.PHONY: save-serv
save-serv: $(addprefix save-,$(SERV_SERVICES))
	@echo "$(COLOR_GREEN)✓ 所有后端镜像导出完成$(COLOR_RESET)"

.PHONY: $(addprefix save-,$(SERV_SERVICES))
$(addprefix save-,$(SERV_SERVICES)): save-%: init
	@echo "$(COLOR_YELLOW)导出 $* 镜像...$(COLOR_RESET)"
	@docker save -o $(BUILD_OUTPUT)/urban-lifeline-$*_$(IMAGE_VERSION).tar \
		urban-lifeline-$*:$(IMAGE_VERSION)
	@echo "$(COLOR_GREEN)✓ $* 镜像已导出: $(BUILD_OUTPUT)/urban-lifeline-$*_$(IMAGE_VERSION).tar$(COLOR_RESET)"

.PHONY: save-web
save-web: save-platform save-workcase-web
	@echo "$(COLOR_GREEN)✓ 所有前端镜像导出完成$(COLOR_RESET)"

.PHONY: save-platform
save-platform: init
	@echo "$(COLOR_YELLOW)导出 platform 镜像...$(COLOR_RESET)"
	@docker save -o $(BUILD_OUTPUT)/urban-lifeline-platform_$(IMAGE_VERSION).tar \
		urban-lifeline-platform:$(IMAGE_VERSION)
	@echo "$(COLOR_GREEN)✓ platform 镜像已导出$(COLOR_RESET)"

.PHONY: save-workcase-web
save-workcase-web: init
	@echo "$(COLOR_YELLOW)导出 workcase-web 镜像...$(COLOR_RESET)"
	@docker save -o $(BUILD_OUTPUT)/urban-lifeline-workcase-web_$(IMAGE_VERSION).tar \
		urban-lifeline-workcase-web:$(IMAGE_VERSION)
	@echo "$(COLOR_GREEN)✓ workcase-web 镜像已导出$(COLOR_RESET)"

.PHONY: save-base
save-base: init
	@echo "$(COLOR_YELLOW)导出基础镜像...$(COLOR_RESET)"
	@docker save -o $(BUILD_OUTPUT)/urban-lifeline-base-serv_$(IMAGE_VERSION).tar \
		urban-lifeline-base-serv:$(IMAGE_VERSION)
	@echo "$(COLOR_GREEN)✓ 基础镜像已导出$(COLOR_RESET)"

# ================================================
# Docker Compose 操作
# ================================================
.PHONY: up
up:
	@echo "$(COLOR_YELLOW)启动所有服务...$(COLOR_RESET)"
	@docker compose --profile all up -d
	@echo "$(COLOR_GREEN)✓ 所有服务已启动$(COLOR_RESET)"

.PHONY: up-infra
up-infra:
	@echo "$(COLOR_YELLOW)启动基础设施...$(COLOR_RESET)"
	@docker compose --profile infra up -d
	@echo "$(COLOR_GREEN)✓ 基础设施已启动$(COLOR_RESET)"

.PHONY: up-serv
up-serv:
	@echo "$(COLOR_YELLOW)启动后端服务...$(COLOR_RESET)"
	@docker compose --profile serv up -d
	@echo "$(COLOR_GREEN)✓ 后端服务已启动$(COLOR_RESET)"

.PHONY: up-web
up-web:
	@echo "$(COLOR_YELLOW)前端已集成到 Nginx (infra)，请使用 make up-infra$(COLOR_RESET)"

.PHONY: down
down:
	@echo "$(COLOR_YELLOW)停止所有服务...$(COLOR_RESET)"
	@docker compose --profile all down
	@echo "$(COLOR_GREEN)✓ 所有服务已停止$(COLOR_RESET)"

.PHONY: restart
restart: down up
	@echo "$(COLOR_GREEN)✓ 所有服务已重启$(COLOR_RESET)"

.PHONY: logs
logs:
	@docker compose logs -f

.PHONY: status
status:
	@echo "$(COLOR_BLUE)=============================================$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)  服务运行状态$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)=============================================$(COLOR_RESET)"
	@docker compose ps

# ================================================
# 清理
# ================================================
.PHONY: clean
clean:
	@echo "$(COLOR_YELLOW)清理构建产物...$(COLOR_RESET)"
	@rm -rf $(BUILD_OUTPUT)
	@echo "$(COLOR_GREEN)✓ 清理完成$(COLOR_RESET)"

.PHONY: clean-images
clean-images:
	@echo "$(COLOR_YELLOW)清理 Docker 镜像...$(COLOR_RESET)"
	@docker images | grep "urban-lifeline" | awk '{print $$3}' | xargs -r docker rmi -f
	@echo "$(COLOR_GREEN)✓ 镜像清理完成$(COLOR_RESET)"

# ================================================
# 快捷命令
# ================================================
.PHONY: full-build
full-build: compile-serv compile-web build-all
	@echo "$(COLOR_GREEN)✓ 完整构建完成$(COLOR_RESET)"

.PHONY: full-deploy
full-deploy: full-build save-all
	@echo "$(COLOR_GREEN)✓ 完整部署包准备完成$(COLOR_RESET)"
	@echo "输出目录: $(BUILD_OUTPUT)"

# 默认目标
.DEFAULT_GOAL := help
