#!/usr/bin/env bash
# =============================================================================
# adapters/gemini-cli/adapter.sh — Gemini CLI platform adapter
# =============================================================================
# Gemini CLI reads GEMINI.md at the project root as its operating manual.
# Same overall shape as the Codex adapter but with Gemini's dispatcher name
# and directory layout (.gemini/).
# =============================================================================

GEMINI_PLATFORM="gemini-cli"
GEMINI_DIR="gemini"
GEMINI_DISPATCHER="GEMINI.md"

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

  _gemini_emit_dispatcher "$src" "$dst"
  _gemini_translate_commands "$src/commands" "$dst/.${GEMINI_DIR}/commands"
  _gemini_copy_references "$src/references" "$dst/.${GEMINI_DIR}/references"
  _gemini_copy_scripts "$src/scripts" "$dst/.${GEMINI_DIR}/scripts"
  _gemini_emit_install_hint "$dst"
}

_gemini_emit_dispatcher() {
  local src="$1" dst="$2"
  local out="$dst/$GEMINI_DISPATCHER"

  {
    cat <<'EOF'
# Obsidian Second Brain — Gemini CLI 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 Gemini
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 `.gemini/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" "$GEMINI_PLATFORM" ".gemini/commands"
    emit_trigger_reference     "$src/commands" "$GEMINI_PLATFORM"

    cat <<'EOF'

---

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

_gemini_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" "$GEMINI_PLATFORM" || continue
    out="$dst/$(basename "$f")"
    cp "$f" "$out"
    rewrite_tool_neutral "$out"
    rewrite_platform_paths "$out" "$GEMINI_DIR"
  done
}

_gemini_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" "$GEMINI_DIR"
  done
}

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

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

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

Then in your vault:

- `GEMINI.md` is the operating manual Gemini reads at session start.
- `.gemini/commands/*.md` are the command bodies the AI follows.
- `.gemini/scripts/` holds the Python helpers (`uv run -m scripts.research.<name>`).
EOF
}
