#!/bin/bash
# 只讀操作，不修改任何文件
# 用於驗證 Momentry Core 環境狀態

set -e

echo "=== Momentry Core Environment Validation ==="
echo "執行時間: $(date)"
echo ""

echo "1. 📡 檢查端口佔用狀態:"
echo "   Port 3002 (生產):"
if PORT_3002_PID=$(lsof -ti:3002 2>/dev/null); then
	echo "   ✅ 正在使用 (PID: $PORT_3002_PID)"
	ps -p $PORT_3002_PID -o pid,command 2>/dev/null | tail -n +2 || true
else
	echo "   ❌ 未使用"
fi

echo "   Port 3003 (開發):"
if PORT_3003_PID=$(lsof -ti:3003 2>/dev/null); then
	echo "   ✅ 正在使用 (PID: $PORT_3003_PID)"
	ps -p $PORT_3003_PID -o pid,command 2>/dev/null | tail -n +2 || true
else
	echo "   ✅ 可用"
fi

echo ""
echo "2. ⚙️  檢查二進制文件狀態:"
echo "   生產二進制 (momentry):"
if [ -f "/Users/accusys/momentry_core_0.1/target/release/momentry" ]; then
	LS_OUTPUT=$(ls -la "/Users/accusys/momentry_core_0.1/target/release/momentry")
	echo "   ✅ 存在: $LS_OUTPUT"
else
	echo "   ❌ 不存在"
fi

echo "   開發二進制 (momentry_playground):"
if [ -f "/Users/accusys/momentry_core_0.1/target/release/momentry_playground" ]; then
	LS_OUTPUT=$(ls -la "/Users/accusys/momentry_core_0.1/target/release/momentry_playground")
	echo "   ✅ 存在: $LS_OUTPUT"
else
	echo "   ⚠️  不存在 (可能需要構建)"
fi

echo ""
echo "3. 📄 檢查環境配置文件:"
echo "   生產配置 (.env):"
if [ -f "/Users/accusys/momentry_core_0.1/.env" ]; then
	echo "   ✅ 存在"
	grep -E "MOMENTRY_SERVER_PORT|MOMENTRY_REDIS_PREFIX" "/Users/accusys/momentry_core_0.1/.env" 2>/dev/null || echo "   ⚠️  未找到關鍵配置"
else
	echo "   ❌ 不存在"
fi

echo "   開發配置 (.env.development):"
if [ -f "/Users/accusys/momentry_core_0.1/.env.development" ]; then
	echo "   ✅ 存在"
	grep -E "MOMENTRY_SERVER_PORT|MOMENTRY_REDIS_PREFIX" "/Users/accusys/momentry_core_0.1/.env.development" 2>/dev/null || echo "   ⚠️  未找到關鍵配置"
else
	echo "   ❌ 不存在"
fi

echo ""
echo "4. 🗄️  檢查資料庫連接狀態:"
echo "   Redis 前綴配置:"
if [ -f "/Users/accusys/momentry_core_0.1/.env" ]; then
	REDIS_PREFIX=$(grep "MOMENTRY_REDIS_PREFIX" "/Users/accusys/momentry_core_0.1/.env" 2>/dev/null | cut -d= -f2 || echo "momentry:")
	echo "   生產: $REDIS_PREFIX"
fi
if [ -f "/Users/accusys/momentry_core_0.1/.env.development" ]; then
	DEV_REDIS_PREFIX=$(grep "MOMENTRY_REDIS_PREFIX" "/Users/accusys/momentry_core_0.1/.env.development" 2>/dev/null | cut -d= -f2 || echo "momentry_dev:")
	echo "   開發: $DEV_REDIS_PREFIX"
fi

echo ""
echo "5. 🏥 生產服務健康檢查:"
if [ -n "$PORT_3002_PID" ]; then
	echo "   嘗試連接生產服務 (port 3002)..."
	if curl -f -s -o /dev/null -w "HTTP狀態碼: %{http_code}\n" --max-time 5 "http://localhost:3002/api/v1/health"; then
		echo "   ✅ 生產服務健康"
	else
		echo "   ❌ 生產服務無法連接"
	fi
else
	echo "   ⚠️  無生產服務運行"
fi

echo ""
echo "6. 📊 系統資源檢查:"
echo "   記憶體使用:"
ps aux | grep -E "momentry|momentry_playground" | grep -v grep | awk '{print "   " $11 " (PID:" $2 ") MEM:" $4 "% CPU:" $3 "%"}' || echo "   無相關進程"

echo ""
echo "=== 驗證總結 ==="
echo "✅ 所有只讀檢查完成"
echo "📋 未修改任何系統文件"
echo "🔒 生產服務保持原狀"
echo ""
echo "建議下一步:"
if [ -n "$PORT_3002_PID" ]; then
	echo "   1. 生產服務正在運行 (PID: $PORT_3002_PID)"
	echo "   2. 如需開發測試，使用 port 3003"
	echo "   3. 執行: ./scripts/deployment/safe/deploy_dry_run.sh"
else
	echo "   1. 無生產服務運行"
	echo "   2. 可啟動開發測試"
	echo "   3. 執行: ./scripts/deployment/safe/agent_commands.sh test-development"
fi
