# ================================================
# Urban Lifeline - Docker 构建与部署 (All-in-One)
# ================================================

.PHONY: help build build-pg build-base build-serv build-web up down logs ps status restart
.PHONY: save save-pg save-base save-serv save-web

# 项目根目录 (相对于此 Makefile)
ROOT_DIR := ../..

# 镜像导出目录
EXPORT_DIR := ./images

# 时间戳标签
TIMESTAMP := $(shell date +%Y%m%d_%H%M%S)

# 默认目标
help:
	@echo "Urban Lifeline Docker 命令 (All-in-One 模式)"
	@echo ""
	@echo "构建命令:"
	@echo "  make build           - 构建所有镜像 (pg + base + serv + web)"
	@echo "  make build-pg        - 构建 PostgreSQL 镜像"
	@echo "  make build-base      - 构建后端基础镜像"
	@echo "  make build-serv      - 构建后端服务镜像 (依赖 base)"
	@echo "  make build-web       - 构建前端镜像"
	@echo ""
	@echo "构建并导出 (SAVE=1):"
	@echo "  make build-pg SAVE=1   - 构建并导出 PostgreSQL 镜像"
	@echo "  make build-serv SAVE=1 - 构建并导出后端镜像"
	@echo "  make build-web SAVE=1  - 构建并导出前端镜像"
	@echo "  make build SAVE=1      - 构建并导出所有镜像"
	@echo ""
	@echo "导出命令:"
	@echo "  make save            - 导出所有镜像"
	@echo "  make save-pg         - 导出 PostgreSQL 镜像"
	@echo "  make save-base       - 导出后端基础镜像"
	@echo "  make save-serv       - 导出后端服务镜像"
	@echo "  make save-web        - 导出前端镜像"
	@echo ""
	@echo "启动/停止:"
	@echo "  make up              - 启动所有服务"
	@echo "  make down            - 停止所有服务"
	@echo ""
	@echo "管理命令:"
	@echo "  make ps              - 查看容器状态"
	@echo "  make status          - 查看服务状态"
	@echo "  make logs            - 查看日志"
	@echo "  make restart-serv SERVICE=gateway  - 重启后端服务"
	@echo "  make restart-web SITE=platform     - 重启前端站点"
	@echo ""
	@echo "镜像导出目录: $(EXPORT_DIR)"

# ============================================
# 构建镜像
# ============================================

# 构建所有镜像
build: build-pg build-base build-serv build-web
ifdef SAVE
	@$(MAKE) save
endif

# 构建 PostgreSQL 镜像
build-pg:
	@echo "=========================================="
	@echo "  构建 PostgreSQL 镜像"
	@echo "  标签: $(TIMESTAMP) / latest"
	@echo "=========================================="
	docker build -t urban-lifeline-pg:$(TIMESTAMP) -f postgres/Dockerfile.pg postgres/
	docker tag urban-lifeline-pg:$(TIMESTAMP) urban-lifeline-pg:latest
ifdef SAVE
	@$(MAKE) save-pg
endif

# 构建后端基础镜像
build-base:
	@echo "=========================================="
	@echo "  构建后端基础镜像"
	@echo "  标签: $(TIMESTAMP) / latest"
	@echo "=========================================="
	cd $(ROOT_DIR) && docker build -t urban-lifeline-base-serv:$(TIMESTAMP) -f docker/urbanLifeline/serv/Dockerfile.base .
	docker tag urban-lifeline-base-serv:$(TIMESTAMP) urban-lifeline-base-serv:latest
ifdef SAVE
	@$(MAKE) save-base
endif

# 构建后端服务镜像 (依赖 base 镜像)
build-serv: build-base
	@echo "=========================================="
	@echo "  构建后端服务镜像"
	@echo "  标签: $(TIMESTAMP) / latest"
	@echo "=========================================="
	cd $(ROOT_DIR) && docker build -t urban-lifeline-serv:$(TIMESTAMP) -f docker/urbanLifeline/serv/Dockerfile.serv .
	docker tag urban-lifeline-serv:$(TIMESTAMP) urban-lifeline-serv:latest
ifdef SAVE
	@$(MAKE) save-serv
endif

# 构建前端镜像
build-web:
	@echo "=========================================="
	@echo "  构建前端镜像"
	@echo "  标签: $(TIMESTAMP) / latest"
	@echo "=========================================="
	cd $(ROOT_DIR) && docker build -t urban-lifeline-web:$(TIMESTAMP) -f docker/urbanLifeline/web/Dockerfile.web .
	docker tag urban-lifeline-web:$(TIMESTAMP) urban-lifeline-web:latest
ifdef SAVE
	@$(MAKE) save-web
endif

# ============================================
# 导出镜像
# ============================================

# 创建导出目录
$(EXPORT_DIR):
	mkdir -p $(EXPORT_DIR)

# 导出所有镜像
save: save-pg save-base save-serv save-web

# 导出 PostgreSQL 镜像
save-pg: $(EXPORT_DIR)
	@echo "导出 PostgreSQL 镜像..."
	docker save urban-lifeline-pg:latest | gzip > $(EXPORT_DIR)/urban-lifeline-pg.tar.gz
	@echo "已导出: $(EXPORT_DIR)/urban-lifeline-pg.tar.gz"

# 导出后端基础镜像
save-base: $(EXPORT_DIR)
	@echo "导出后端基础镜像..."
	docker save urban-lifeline-base-serv:latest | gzip > $(EXPORT_DIR)/urban-lifeline-base-serv.tar.gz
	@echo "已导出: $(EXPORT_DIR)/urban-lifeline-base-serv.tar.gz"

# 导出后端服务镜像
save-serv: $(EXPORT_DIR)
	@echo "导出后端服务镜像..."
	docker save urban-lifeline-serv:latest | gzip > $(EXPORT_DIR)/urban-lifeline-serv.tar.gz
	@echo "已导出: $(EXPORT_DIR)/urban-lifeline-serv.tar.gz"

# 导出前端镜像
save-web: $(EXPORT_DIR)
	@echo "导出前端镜像..."
	docker save urban-lifeline-web:latest | gzip > $(EXPORT_DIR)/urban-lifeline-web.tar.gz
	@echo "已导出: $(EXPORT_DIR)/urban-lifeline-web.tar.gz"

# ============================================
# 启动/停止
# ============================================

up:
	cd .. && docker compose --profile all up -d

down:
	cd .. && docker compose --profile all down

# ============================================
# 状态查看
# ============================================

ps:
	docker ps --filter "name=urban-lifeline"

status:
	@echo "=== 后端服务状态 ==="
	@docker exec urban-lifeline-serv /app/service-manager.sh status 2>/dev/null || echo "后端容器未运行"
	@echo ""
	@echo "=== 前端服务状态 ==="
	@docker exec urban-lifeline-web /app/web-manager.sh status 2>/dev/null || echo "前端容器未运行"

# ============================================
# 日志
# ============================================

logs:
	cd .. && docker compose logs -f --tail=100

logs-serv:
	docker logs -f --tail=100 urban-lifeline-serv

logs-web:
	docker logs -f --tail=100 urban-lifeline-web

# ============================================
# 重启单个服务
# ============================================

restart-serv:
	@if [ -z "$(SERVICE)" ]; then \
		echo "用法: make restart-serv SERVICE=gateway"; \
		echo "可用服务: gateway system auth file log message crontab bidding workcase platform ai"; \
	else \
		docker exec urban-lifeline-serv /app/service-manager.sh restart $(SERVICE); \
	fi

restart-web:
	@if [ -z "$(SITE)" ]; then \
		echo "用法: make restart-web SITE=platform"; \
		echo "可用站点: shared platform workcase bidding workcase_wechat"; \
	else \
		docker exec urban-lifeline-web /app/web-manager.sh restart $(SITE); \
	fi
