#!/bin/bash
# 3588AdminBackend 开发运行脚本
# 用于本地开发调试

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
CONFIG_FILE="$PROJECT_DIR/safesightd.json"

echo "========== 3588AdminBackend 开发运行 =========="
echo "项目目录: $PROJECT_DIR"
echo ""

# 检查 Go 环境
if ! command -v go >/dev/null 2>&1; then
    echo "错误: 未找到 Go 命令"
    exit 1
fi

echo "Go 版本: $(go version)"
echo ""

# 检查配置文件
if [ ! -f "$CONFIG_FILE" ]; then
    echo "创建默认配置文件..."
    cat > "$CONFIG_FILE" << 'EOF'
{
  "listen": "127.0.0.1:18080",
  "discovery_port": 35688,
  "discovery_timeout_ms": 1200,
  "offline_after_ms": 10000,
  "agent_token": "dev-token-12345",
  "concurrency": 5
}
EOF
    echo "配置文件已创建: $CONFIG_FILE"
    echo "请根据需要修改配置"
    echo ""
fi

# 下载依赖
echo "[1/2] 检查依赖..."
cd "$PROJECT_DIR"
go mod download

# 运行
echo "[2/2] 启动服务..."
echo "配置文件: $CONFIG_FILE"
echo "API 地址: http://127.0.0.1:18080"
echo ""
echo "按 Ctrl+C 停止服务"
echo ""

# 使用 go run 直接运行
go run ./cmd/safesightd -config "$CONFIG_FILE"
