#!/usr/bin/env bash
# =============================================================================
# adapters/codex-cli/adapter.sh — OpenAI Codex CLI platform adapter
# =============================================================================
# Codex CLI reads AGENTS.md at the project (or vault) root for its operating
# manual. There is no native slash-command system, so we emit a dispatcher
# table in AGENTS.md that maps natural-language triggers to command files
# under .codex/commands/.
# =============================================================================

CODEX_PLATFORM="codex-cli"
CODEX_DIR="codex"
CODEX_DISPATCHER="AGENTS.md"

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

  _codex_emit_dispatcher "$src" "$dst"
  _codex_translate_commands "$src/commands" "$dst/.${CODEX_DIR}/commands"
  _codex_copy_references "$src/references" "$dst/.${CODEX_DIR}/references"
  _codex_copy_scripts "$src/scripts" "$dst/.${CODEX_DIR}/scripts"
  _codex_emit_install_hint "$dst"
}

# Emit AGENTS.md at the dist root. Header + auto-generated routing table
# (one row per command, parsed from frontmatter description:).
_codex_emit_dispatcher() {
  local src="$1" dst="$2"
  local out="$dst/$CODEX_DISPATCHER"

  {
    cat <<'EOF'
# Obsidian Second Brain — Codex 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 Codex
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 (folder map, daily note format, naming).
2. When the user's request matches a trigger in the tables below, read the
   matching file under `.codex/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.
4. Do not invent commands. If no command matches, ask the user what they
   want or fall back to plain natural-language help.

## Command routing tables (grouped by category)
EOF

    emit_routing_table_grouped "$src/commands" "$CODEX_PLATFORM" ".codex/commands"
    emit_trigger_reference     "$src/commands" "$CODEX_PLATFORM"

    cat <<'EOF'

## Trigger conventions

Each command file has a leading paragraph that documents its trigger phrases
in plain English. Treat those triggers as primary; the slash-form `/name` is
also accepted if the user types it.

## Scripts

Python helpers live under `.codex/scripts/`. They run via `uv run -m
scripts.research.<name>` from the vault root (or wherever the skill is
installed). The commands that need them reference the exact invocation
inside the command body.

---

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

# Copy commands into .codex/commands/, neutralizing Claude-specific tool
# names so the instructions still make sense for Codex CLI agents.
_codex_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" "$CODEX_PLATFORM" || continue
    out="$dst/$(basename "$f")"
    cp "$f" "$out"
    rewrite_tool_neutral "$out"
    rewrite_platform_paths "$out" "$CODEX_DIR"
  done
}

_codex_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" "$CODEX_DIR"
  done
}

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

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

```bash
# From the repo root, after running `bash scripts/build.sh --platform codex-cli`:
# Copy (or symlink) the built tree into your vault root:
cp -R dist/codex-cli/. /path/to/your/vault/
```

Then in your vault:

- `AGENTS.md` is the operating manual Codex reads at session start.
- `.codex/commands/*.md` are the command bodies the AI follows when a
  matching trigger fires.
- `.codex/scripts/` holds the Python helpers invoked by the research
  toolkit commands. Run them via `uv run -m scripts.research.<name>`.

Start Codex CLI from the vault root.
EOF
}
