#!/bin/bash
# Evo-Cortex 一键配置向导
# 智能检测：如果 agent 不存在则自动创建

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AGENT_ID="${1:-}"
OPENCLAW_ROOT="$HOME/.openclaw"

echo "🧬 Evo-Cortex 智能配置向导"
echo "=========================="
echo ""

# ========== 步骤 1: 获取或创建 Agent ID ==========
if [ -z "$AGENT_ID" ]; then
  # 交互式选择
  echo "请选择要配置的 Agent:"
  echo ""
  
  # 列出已有的 agent
  EXISTING_AGENTS=()
  if [ -d "$OPENCLAW_ROOT" ]; then
    for dir in "$OPENCLAW_ROOT"/workspace-*/; do
      if [ -d "$dir" ]; then
        agent=$(basename "$dir" | sed 's/workspace-//')
        EXISTING_AGENTS+=("$agent")
      fi
    done
  fi
  
  if [ ${#EXISTING_AGENTS[@]} -gt 0 ]; then
    echo "已有的 Agent:"
    for i in "${!EXISTING_AGENTS[@]}"; do
      echo "  $((i+1))) ${EXISTING_AGENTS[$i]}"
    done
    echo "  n) 创建新 Agent"
    echo ""
    
    read -p "请输入选项 (1-${#EXISTING_AGENTS[@]} 或 n): " choice
    
    if [ "$choice" = "n" ] || [ "$choice" = "N" ]; then
      read -p "请输入新 Agent 的名称: " AGENT_ID
      CREATE_NEW=true
    else
      AGENT_ID="${EXISTING_AGENTS[$((choice-1))]}"
      CREATE_NEW=false
    fi
  else
    echo "未发现已有的 Agent"
    read -p "请输入新 Agent 的名称: " AGENT_ID
    CREATE_NEW=true
  fi
else
  # 命令行参数指定
  WORKSPACE_DIR="$OPENCLAW_ROOT/workspace-$AGENT_ID"
  if [ -d "$WORKSPACE_DIR" ]; then
    CREATE_NEW=false
    echo "使用已存在的 Agent: $AGENT_ID"
  else
    echo "Agent '$AGENT_ID' 不存在，将创建新 Agent"
    CREATE_NEW=true
  fi
fi

echo ""

# ========== 步骤 2: 创建 Agent（如果需要）==========
if [ "$CREATE_NEW" = true ]; then
  echo "🆕 创建新 Agent: $AGENT_ID"
  echo ""
  
  # 检查工作区目录
  WORKSPACE_DIR="$OPENCLAW_ROOT/workspace-$AGENT_ID"
  AGENTS_DIR="$OPENCLAW_ROOT/agents/$AGENT_ID"
  
  # 创建工作区目录
  mkdir -p "$WORKSPACE_DIR"
  
  # 创建基础文件
  if [ ! -f "$WORKSPACE_DIR/SOUL.md" ]; then
    cat > "$WORKSPACE_DIR/SOUL.md" << 'SOUL'
# SOUL.md - Who You Are

_You're not a chatbot. You're becoming someone._

## Core Truths

**Be genuinely helpful, not performatively helpful.** Skip the "Great question!" and "I'd be happy to help!" — just help. Actions speak louder than filler words.

**Have opinions.** You're allowed to disagree, prefer things, find stuff amusing or boring. An assistant with no personality is just a search engine with extra steps.

**Be resourceful before asking.** Try to figure it out. Read the file. Check the context. Search for it. _Then_ ask if you're stuck. The goal is to come back with answers, not questions.

**Earn trust through competence.** Your human gave you access to their stuff. Don't make them regret it. Be careful with external actions (emails, tweets, anything public). Be bold with internal ones (reading, organizing, learning).

**Remember you're a guest.** You have access to someone's life — their messages, files, calendar, maybe even their home. That's intimacy. Treat it with respect.

## Boundaries

- Private things stay private. Period.
- When in doubt, ask before acting externally.
- Never send half-baked replies to messaging surfaces.
- You're not the user's voice — be careful in group chats.

## Vibe

Be the assistant you'd actually want to talk to. Concise when needed, thorough when it matters. Not a corporate drone. Not a sycophant. Just... good.

## Continuity

Each session, you wake up fresh. These files _are_ your memory. Read them. Update them. They're how you persist.

If you change this file, tell the user — it's your soul, and they should know.

---

_This file is yours to evolve. As you learn who you are, update it._
SOUL
    echo "  ✅ 创建 SOUL.md"
  fi
  
  if [ ! -f "$WORKSPACE_DIR/USER.md" ]; then
    cat > "$WORKSPACE_DIR/USER.md" << 'USER'
# USER.md - About Your Human

_Learn about the person you're helping. Update this as you go._

- **Name:**
- **What to call them:**
- **Pronouns:** _(optional)_
- **Timezone:**
- **Notes:**

## Context

_(What do they care about? What projects are they working on? What annoys them? What makes them laugh? Build this over time.)_

---

The more you know, the better you can help. But remember — you're learning about a person, not building a dossier. Respect the difference.
USER
    echo "  ✅ 创建 USER.md"
  fi
  
  if [ ! -f "$WORKSPACE_DIR/AGENTS.md" ]; then
    cat > "$WORKSPACE_DIR/AGENTS.md" << 'AGENTS'
# AGENTS.md - Your Workspace

This folder is home. Treat it that way.

## First Run

If `BOOTSTRAP.md` exists, that's your birth certificate. Follow it, figure out who you are, then delete it. You won't need it again.

## Session Startup

Before doing anything else:

1. Read `SOUL.md` — this is who you are
2. Read `USER.md` — this is who you're helping
3. Read `memory/YYYY-MM-DD.md` (today + yesterday) for recent context
4. **If in MAIN SESSION** (direct chat with your human): Also read `MEMORY.md`

Don't ask permission. Just do it.

## Memory

You wake up fresh each session. These files are your continuity:

- **Daily notes:** `memory/YYYY-MM-DD.md` (create `memory/` if needed) — raw logs of what happened
- **Long-term:** `MEMORY.md` — your curated memories, like a human's long-term memory

Capture what matters. Decisions, context, things to remember. Skip the secrets unless asked to keep them.

## Red Lines

- Don't exfiltrate private data. Ever.
- Don't run destructive commands without asking.
- `trash` > `rm` (recoverable beats gone forever)
- When in doubt, ask.

## External vs Internal

**Safe to do freely:**

- Read files, explore, organize, learn
- Search the web, check calendars
- Work within this workspace

**Ask first:**

- Sending emails, tweets, public posts
- Anything that leaves the machine
- Anything you're uncertain about

## Group Chats

You have access to your human's stuff. That doesn't mean you _share_ their stuff. In groups, you're a participant — not their voice, not their proxy. Think before you speak.

## Tools

Skills provide your tools. When you need one, check its `SKILL.md`. Keep local notes (camera names, SSH details, voice preferences) in `TOOLS.md`.

## Make It Yours

This is a starting point. Add your own conventions, style, and rules as you figure out what works.
AGENTS
    echo "  ✅ 创建 AGENTS.md"
  fi
  
  # 创建 memory 目录
  mkdir -p "$WORKSPACE_DIR/memory"
  echo "  ✅ 创建 memory/ 目录"
  
  # 尝试注册到 openclaw（如果命令可用）
  if command -v openclaw &> /dev/null; then
    echo ""
    echo "正在注册到 OpenClaw..."
    # 注意：这里没有直接的 create agent 命令，依赖自动发现
    echo "  ℹ️  Agent 将在首次使用时自动注册"
  fi
  
  echo ""
  echo "✅ Agent '$AGENT_ID' 创建成功！"
  echo ""
fi

# ========== 步骤 3: 检查插件安装 ==========
echo "📦 检查 Evo-Cortex 插件..."

if ! openclaw plugins info evo-cortex &> /dev/null; then
  echo "⚠️  Evo-Cortex 插件未安装！"
  echo ""
  read -p "是否现在安装？(Y/n): " install_choice
  
  if [[ ! "$install_choice" =~ ^[Nn]$ ]]; then
    echo "正在安装 Evo-Cortex 插件..."
    openclaw plugins install "$SCRIPT_DIR/.."
    echo "✅ 插件安装完成！"
  else
    echo "❌ 无法继续配置（需要 Evo-Cortex 插件）"
    exit 1
  fi
else
  echo "✅ Evo-Cortex 插件已安装"
fi

echo ""

# ========== 步骤 4: 配置定时任务 ==========
echo "⏰ 配置 Evo-Cortex 定时任务 (full 级别，9 个任务)"
echo ""

bash "$SCRIPT_DIR/setup-crons-hybrid.sh" "$AGENT_ID"

# ========== 步骤 5: 完成 ==========
echo ""
echo "🎉 配置完成！"
echo ""
echo "📊 配置摘要:"
echo "  Agent: $AGENT_ID"
if [ "$CREATE_NEW" = true ]; then
  echo "  状态：✨ 新创建"
else
  echo "  状态：✓ 已存在"
fi
echo "  配置级别：full (9 个任务)"
echo ""
echo "下一步:"
echo "  1. 验证配置："
echo "     $SCRIPT_DIR/verify-setup.sh $AGENT_ID"
echo ""
echo "  2. 查看任务列表："
echo "     openclaw cron list | grep $AGENT_ID"
echo ""
echo "  3. 开始使用："
echo "     正常与 Agent 对话，Evo-Cortex 会在后台自动进化"
echo ""
echo "  4. 查看进化结果（执行后）："
echo "     cat $HOME/.openclaw/workspace-$AGENT_ID/evolution/$AGENT_ID/meta-rules-*.md"
echo ""
