#!/usr/bin/env bash
# =============================================================================
# adapters/opencode/adapter.sh — OpenCode platform adapter
# =============================================================================
# OpenCode reads AGENTS.md (the cross-IDE convention) at the project root.
# Same overall shape as the Codex adapter; the layout uses .opencode/.
# =============================================================================

OPENCODE_PLATFORM="opencode"
OPENCODE_DIR="opencode"
OPENCODE_DISPATCHER="AGENTS.md"

adapter_build() {
  local src="$1" dst="$2"

  _opencode_emit_dispatcher "$src" "$dst"
  _opencode_translate_commands "$src/commands" "$dst/.${OPENCODE_DIR}/commands"
  _opencode_copy_references "$src/references" "$dst/.${OPENCODE_DIR}/references"
  _opencode_copy_scripts "$src/scripts" "$dst/.${OPENCODE_DIR}/scripts"
  _opencode_emit_install_hint "$dst"
}

_opencode_emit_dispatcher() {
  local src="$1" dst="$2"
  local out="$dst/$OPENCODE_DISPATCHER"

  {
    cat <<'EOF'
# Obsidian Second Brain — OpenCode Operating Manual

This vault runs the **obsidian-second-brain** skill. The skill ships a set of
*commands*: each one is a multi-step instruction file that you (the OpenCode
agent) should follow when the user's request matches its trigger phrase.

## How to operate

1. Read `_CLAUDE.md` in the vault root, if it exists, to learn the user's
   vault conventions.
2. When the user's request matches a trigger in the tables below, read the
   matching file under `.opencode/commands/<name>.md` and follow its
   instructions step by step.
3. Treat the AI-first vault rule (`references/ai-first-rules.md`) as
   non-negotiable for every note you write: `## For future Claude` preamble,
   rich frontmatter (`type`, `date`, `tags`, `ai-first: true`), `[[wikilinks]]`
   for every person/project/concept, recency markers per external claim,
   sources verbatim, confidence levels where applicable.

## Command routing tables (grouped by category)
EOF

    emit_routing_table_grouped "$src/commands" "$OPENCODE_PLATFORM" ".opencode/commands"
    emit_trigger_reference     "$src/commands" "$OPENCODE_PLATFORM"

    cat <<'EOF'

---

*Generated by adapters/opencode/adapter.sh — do not edit manually.*
EOF
  } > "$out"
}

_opencode_translate_commands() {
  local src="$1" dst="$2"
  [[ -d "$src" ]] || return 0
  mkdir -p "$dst"
  local f out
  for f in "$src"/*.md; do
    [[ -f "$f" ]] || continue
    should_include "$f" "$OPENCODE_PLATFORM" || continue
    out="$dst/$(basename "$f")"
    cp "$f" "$out"
    rewrite_tool_neutral "$out"
    rewrite_platform_paths "$out" "$OPENCODE_DIR"
  done
}

_opencode_copy_references() {
  local src="$1" dst="$2"
  [[ -d "$src" ]] || return 0
  mkdir -p "$dst"
  cp -R "$src/." "$dst/"
  find "$dst" -type f -name '*.md' -print0 | while IFS= read -r -d '' f; do
    rewrite_platform_paths "$f" "$OPENCODE_DIR"
  done
}

_opencode_copy_scripts() {
  local src="$1" dst="$2"
  [[ -d "$src" ]] || return 0
  mkdir -p "$dst"
  cp -R "$src/." "$dst/"
}

_opencode_emit_install_hint() {
  local dst="$1"
  cat > "$dst/INSTALL.md" <<'EOF'
# Install on OpenCode

```bash
# After running `bash scripts/build.sh --platform opencode`:
cp -R dist/opencode/. /path/to/your/vault/
```

Then in your vault:

- `AGENTS.md` is the operating manual OpenCode reads at session start.
- `.opencode/commands/*.md` are the command bodies the AI follows.
- `.opencode/scripts/` holds the Python helpers.
EOF
}
