# ============================================
# 海外预约系统 - Makefile
# ============================================

.PHONY: help deploy build start stop restart logs status clean

# 默认目标
.DEFAULT_GOAL := help

# 颜色定义
GREEN  := \033[0;32m
YELLOW := \033[1;33m
BLUE   := \033[0;34m
NC     := \033[0m

# 检测 docker compose 命令
DOCKER_COMPOSE := $(shell if docker compose version > /dev/null 2>&1; then echo "docker compose"; else echo "docker-compose"; fi)

# 帮助信息
help:
	@echo ""
	@echo "$(BLUE)============================================$(NC)"
	@echo "$(BLUE)   海外预约系统 - 部署命令$(NC)"
	@echo "$(BLUE)============================================$(NC)"
	@echo ""
	@echo "$(GREEN)部署命令:$(NC)"
	@echo "  make deploy      - 完整部署（构建 Admin + 启动服务）"
	@echo "  make build       - 仅构建 Admin 前端"
	@echo "  make start       - 启动服务（跳过构建）"
	@echo ""
	@echo "$(GREEN)管理命令:$(NC)"
	@echo "  make stop        - 停止服务"
	@echo "  make restart     - 重启服务"
	@echo "  make status      - 查看服务状态"
	@echo "  make logs        - 查看实时日志"
	@echo "  make logs-api    - 查看 API 日志"
	@echo "  make logs-nginx  - 查看 Nginx 日志"
	@echo ""
	@echo "$(GREEN)其他命令:$(NC)"
	@echo "  make clean       - 清理构建产物"
	@echo "  make shell-api   - 进入 API 容器"
	@echo "  make shell-nginx - 进入 Nginx 容器"
	@echo "  make init        - 初始化环境配置"
	@echo ""

# 完整部署
deploy:
	@./deploy.sh

# 仅构建 Admin
build:
	@echo "$(YELLOW)构建 Admin 前端...$(NC)"
	@cd ../admin && npm install && npm run build
	@rm -rf ./admin-dist
	@cp -r ../admin/dist ./admin-dist
	@echo "$(GREEN)✓ Admin 构建完成$(NC)"

# 启动服务（跳过构建）
start:
	@./deploy.sh --skip-build

# 停止服务
stop:
	@./deploy.sh --stop

# 重启服务
restart:
	@./deploy.sh --restart

# 查看服务状态
status:
	@$(DOCKER_COMPOSE) ps

# 查看所有日志
logs:
	@$(DOCKER_COMPOSE) logs -f

# 查看 API 日志
logs-api:
	@$(DOCKER_COMPOSE) logs -f api

# 查看 Nginx 日志
logs-nginx:
	@$(DOCKER_COMPOSE) logs -f nginx

# 进入 API 容器
shell-api:
	@docker exec -it overseas-appointment-api sh

# 进入 Nginx 容器
shell-nginx:
	@docker exec -it overseas-appointment-nginx sh

# 初始化环境配置
init:
	@if [ ! -f .env ]; then \
		cp .env.example .env; \
		echo "$(GREEN)✓ 已创建 .env 文件，请编辑配置$(NC)"; \
	else \
		echo "$(YELLOW).env 文件已存在$(NC)"; \
	fi

# 清理构建产物
clean:
	@echo "$(YELLOW)清理构建产物...$(NC)"
	@rm -rf ./admin-dist
	@$(DOCKER_COMPOSE) down --rmi local 2>/dev/null || true
	@echo "$(GREEN)✓ 清理完成$(NC)"

# 查看健康状态
health:
	@curl -s http://localhost:2701/health | python3 -m json.tool 2>/dev/null || curl -s http://localhost:2701/health
