#!/usr/bin/env bash
# workspace 初始化脚本
# 用途：新环境部署后一键还原工作状态
# 用法：bash scripts/init-workspace.sh

set -euo pipefail

echo "🔧 初始化工作空间..."

# 检查依赖
for cmd in git node npm curl; do
  if ! command -v "$cmd" &>/dev/null; then
    echo "❌ 缺少依赖: $cmd"
    exit 1
  fi
done
echo "✅ 所有依赖已就绪"

# 检查 Git 远程
cd "$(dirname "$0")/.."
if ! git remote -v &>/dev/null; then
  echo "⚠️  没有配置远程仓库，跳过"
else
  echo "✅ Git 远程已配置"
fi

# 检查浏览器
if command -v openclaw &>/dev/null; then
  if openclaw browser doctor 2>&1 | grep -q "not running"; then
    echo "🦞 浏览器未启动，尝试启动..."
    openclaw browser start 2>/dev/null || echo "⚠️  浏览器启动失败，可手动运行 openclaw browser start"
  else
    echo "✅ 浏览器工具已就绪"
  fi
fi

echo "✅ 任务流程脚本: scripts/task-flow.sh"
echo "✅ Skill 提取脚本: scripts/extract-skill.sh"
echo ""
echo "✨ 工作空间就绪"
echo "  目录: $(pwd)"
echo "  日期: $(date '+%Y-%m-%d %H:%M')"
