# ============================================
# Social App Monorepo - 统一命令入口
# ============================================
#
# 使用方式：
#   make <command>
#
# 常用命令：
#   make up          启动本地开发依赖
#   make down        停止并移除容器
#   make logs        查看依赖服务日志
#   make ps          查看运行状态
#   make clean       清理本地数据卷（警告：会丢失数据）
#   make env         提示如何配置环境变量
#   make api-dev     启动 FastAPI 后端服务
#   make flutter-dev 启动 Flutter 移动应用
#   make milvus-init 初始化 Milvus 向量数据库

.PHONY: help up down logs ps clean env api-dev flutter-dev milvus-init

# 默认目标：显示帮助
help:
	@echo "Social App Monorepo - 可用命令："
	@echo ""
	@echo "  依赖服务管理："
	@echo "    make up           启动本地开发依赖（Redis、Milvus、Postgres）"
	@echo "    make down         停止并移除所有容器"
	@echo "    make logs         查看依赖服务日志（可用 SERVICE=redis 等指定）"
	@echo "    make ps           查看容器运行状态"
	@echo "    make clean        清理本地数据卷（警告：会丢失数据）"
	@echo ""
	@echo "  应用启动："
	@echo "    make api-dev      启动 FastAPI 后端服务"
	@echo "    make flutter-dev  启动 Flutter 移动应用"
	@echo ""
	@echo "  配置与初始化："
	@echo "    make env          显示如何配置环境变量"
	@echo "    make milvus-init  初始化 Milvus 向量数据库"
	@echo ""
	@echo "  示例："
	@echo "    make logs SERVICE=redis"
	@echo "    make logs SERVICE=milvus"

# ============================================
# 依赖服务管理
# ============================================

# 启动本地开发依赖
up:
	@echo "🚀 启动本地开发依赖..."
	docker compose -f infra/local/docker-compose.yml --env-file infra/local/env/.env up -d
	@echo ""
	@echo "✅ 依赖服务已启动"
	@echo ""
	@echo "服务端口："
	@echo "  - Redis:     6379"
	@echo "  - Milvus:    19530 (gRPC) / 19111 (HTTP)"
	@echo "  - Postgres:  54322"
	@echo ""
	@echo "检查状态：make ps"
	@echo "查看日志：make logs"

# 停止并移除容器
down:
	@echo "🛑 停止并移除容器..."
	docker compose -f infra/local/docker-compose.yml --env-file infra/local/env/.env down
	@echo "✅ 容器已停止并移除"

# 查看日志
logs:
ifndef SERVICE
	@echo "📋 查看所有服务日志（Ctrl+C 退出）..."
	docker compose -f infra/local/docker-compose.yml --env-file infra/local/env/.env logs -f
else
	@echo "📋 查看 $(SERVICE) 服务日志（Ctrl+C 退出）..."
	docker compose -f infra/local/docker-compose.yml --env-file infra/local/env/.env logs -f $(SERVICE)
endif

# 查看运行状态
ps:
	@echo "📊 容器运行状态："
	docker compose -f infra/local/docker-compose.yml --env-file infra/local/env/.env ps

# 清理数据卷（警告：会丢失数据）
clean:
	@echo "⚠️  警告：此操作将删除所有本地数据（Redis、Milvus、Postgres）"
	@read -p "确认继续？[y/N] " confirm; \
	if [ "$$confirm" = "y" ] || [ "$$confirm" = "Y" ]; then \
		docker compose -f infra/local/docker-compose.yml --env-file infra/local/env/.env down -v; \
		echo "✅ 数据卷已清理"; \
	else \
		echo "❌ 操作已取消"; \
	fi

# ============================================
# 配置管理
# ============================================

# 显示环境变量配置说明
env:
	@echo "📝 环境变量配置说明："
	@echo ""
	@echo "1. 查看配置规范："
	@echo "   cat configs/env/.env.example"
	@echo ""
	@echo "2. 本地开发配置："
	@echo "   mkdir -p infra/local/env"
	@echo "   cp configs/env/.env.example configs/env/.env"
	@echo "   cp infra/local/env/.env.example infra/local/env/.env"
	@echo "   # 编辑 configs/env/.env 与 infra/local/env/.env"
	@echo ""
	@echo "3. 确保以下变量已正确配置："
	@echo "   - DATABASE_URL (连接到 localhost:54322)"
	@echo "   - REDIS_URL (连接到 localhost:6379)"
	@echo "   - MILVUS_URI (连接到 localhost:19530)"
	@echo ""
	@echo "4. 严禁将包含真实密钥的配置文件提交到 Git"

# ============================================
# 应用启动
# ============================================

# 启动 FastAPI 后端服务
api-dev:
	@echo "🔧 启动 FastAPI 后端服务..."
	@echo ""
	@echo "前置条件："
	@echo "  1. 确保依赖服务已启动：make up"
	@echo "  2. 确保环境变量已配置：make env"
	@echo ""
	@echo "启动命令（示例）："
	@echo "  cd backend"
	@echo "  uv run uvicorn src.app:app --host 0.0.0.0 --port 8000 --reload"
	@echo ""
	@echo "或使用 Python 虚拟环境："
	@echo "  python -m venv .venv"
	@echo "  source .venv/bin/activate"
	@echo "  pip install -r requirements.txt"
	@echo "  uvicorn src.main:app --host 0.0.0.0 --port 8000 --reload"

# 启动 Flutter 移动应用
flutter-dev:
	@echo "📱 启动 Flutter 移动应用..."
	@echo ""
	@echo "前置条件："
	@echo "  1. 确保后端服务已启动：make api-dev"
	@echo "  2. 确保 Flutter SDK 已安装"
	@echo ""
	@echo "启动命令（示例）："
	@echo "  cd apps/mobile"
	@echo "  flutter run --dart-define=PUBLIC_API_BASE_URL=http://localhost:8000"
	@echo ""
	@echo "或使用调试模式："
	@echo "  flutter run --dart-define=PUBLIC_API_BASE_URL=http://localhost:8000 --debug"
	@echo ""
	@echo "构建版本："
	@echo "  flutter build apk --dart-define=PUBLIC_API_BASE_URL=http://localhost:8000"

# ============================================
# 初始化脚本
# ============================================

# 初始化 Milvus 向量数据库
milvus-init:
	@echo "🔧 初始化 Milvus 向量数据库..."
	@echo ""
	@echo "前置条件："
	@echo "  1. 确保依赖服务已启动：make up"
	@echo "  2. 确保 Milvus 服务已健康（检查：make ps）"
	@echo ""
	@echo "初始化脚本："
	@echo "  bash tools/scripts/init_milvus.sh"
	@echo ""
	@echo "如脚本不存在，请创建："
	@echo "  mkdir -p tools/scripts"
	@echo "  cat > tools/scripts/init_milvus.sh << 'EOF'"
	@echo "#!/bin/bash"
	@echo "# Milvus 初始化脚本"
	@echo "# TODO: 创建集合、索引等"
	@echo "EOF"
