#!/usr/bin/env bash
# ai-dept-admin workspace：创建知识库、人员、会议、待办、复盘目录与最小占位文件。
#
# 用法：安装器 post_install 调用；或：
#   WORKSPACE=/path/to/workspace-dept ./scripts/init_workspace.sh
#   ./scripts/init_workspace.sh /path/to/workspace-dept

set -euo pipefail

WS="${WORKSPACE:-${OPENCLAW_WORKSPACE:-${1:-}}}"
if [[ -z "$WS" ]]; then
  echo "init_workspace: set WORKSPACE or OPENCLAW_WORKSPACE, or pass workspace root as first arg." >&2
  exit 1
fi

WS="$(cd "$WS" && pwd)"

mkdir -p \
  "$WS/knowledge/images/"{media,emf_raw} \
  "$WS/knowledge/attachments" \
  "$WS/knowledge/archive" \
  "$WS/people/_examples" \
  "$WS/meetings/single" \
  "$WS/meetings/recurring" \
  "$WS/meetings/instances" \
  "$WS/todos/archive" \
  "$WS/retrospectives/templates" \
  "$WS/retrospectives/records" \
  "$WS/memory"

create_if_missing() {
  local f="$1"
  shift
  if [[ ! -f "$f" ]]; then
    printf '%b' "$*" >"$f"
  fi
}

create_if_missing "$WS/knowledge/INDEX.md" \
  "## 部门知识库\n\n（暂无条目，请通过 AGENTS.md 第一节维护索引。）\n"

create_if_missing "$WS/knowledge/FAQ.md" \
  "## 部门常见问题\n\n（暂无，可从高频问答中整理。）\n"

create_if_missing "$WS/people/INDEX.md" \
  "## 部门名册\n\n| 姓名/标识 | 角色 | 人员目录 |\n| --- | --- | --- |\n"

create_if_missing "$WS/meetings/INDEX.md" \
  "## 会议总览\n\n### 近期\n- （暂无）\n\n### 周期会议系列\n- 定义见 meetings/recurring/\n"

create_if_missing "$WS/todos/open.md" \
  "## 进行中待办\n\n| ID | 标题 | 负责人 | 状态 | 截止 | 创建日 | 备注 |\n| --- | --- | --- | --- | --- | --- | --- |\n"

create_if_missing "$WS/retrospectives/README.md" \
  "## 复盘\n\n- **records/**：已完成的复盘记录。\n- **templates/**：标准复盘模板将后续接入；未接入前按 AGENTS.md 默认小节撰写。\n"

create_if_missing "$WS/retrospectives/templates/README.md" \
  "## 复盘模板（预留）\n\n后续在此目录添加团队统一的 Markdown 模版文件。\n"

# MEMORY.md 由 Agent 模板随包安装；若工作区空且无文件则写最小占位
create_if_missing "$WS/MEMORY.md" \
  "# MEMORY.md - 长期记忆\n\n> 由助手从日常记录中提炼；过时条目删除。\n\n---\n\n## 部门协作惯例\n\n（暂无）\n"

echo "init_workspace: ai-dept-admin tree ready under ${WS}"
