const fs = require('fs');
const path = require('path');
const os = require('os');

/**
 * OpenAI Codex CLI tool adapter.
 *
 * Codex skill format is structurally identical to Claude's: a directory
 * with `SKILL.md` (YAML frontmatter: name + description), plus optional
 * `scripts/`, `references/`, `assets/`. The ONLY differences from Claude:
 *   - Lives at `.agents/skills/<skill>/` (repo-level) or
 *     `~/.agents/skills/<skill>/` (user-level).
 *   - May carry an optional `agents/openai.yaml` for Codex-specific
 *     metadata (display name, icon, brand color, allow_implicit_invocation,
 *     dependencies). Not required for the skill to load.
 *
 * Because the format is the same, the install model is "symlink the same
 * source dir into two places" — Claude reads from `.claude/skills/<x>`,
 * Codex reads from `.agents/skills/<x>`, and both follow the symlink to
 * the single source under `.framework/framework/.claude/skills/<x>/`.
 *
 * Codex does NOT have an equivalent of Claude subagents (`.claude/agents/`)
 * or slash commands (custom prompts are deprecated in favor of skills).
 * AGENTS.md at the repo root IS read by Codex — same convention as Claude.
 */
class CodexAdapter {
  constructor(cwd = process.cwd()) {
    this.cwd = cwd;
  }

  get name() { return 'codex'; }
  get label() { return 'OpenAI Codex CLI'; }

  skillsDir() { return '.agents/skills'; }

  // Codex has no equivalent of Claude subagents or slash commands (custom
  // prompts are deprecated in favor of skills). Returning null disables the
  // per-item merge for those kinds — adapters that DO support them return
  // the target directory string.
  agentsDir() { return null; }
  commandsDir() { return null; }

  // Codex has no equivalent of Claude Code dynamic workflows (the JS
  // orchestration runtime is Claude-only). Returning null disables the
  // per-item workflow merge for Codex — the same source workflow scripts are
  // simply never linked into a Codex tree.
  workflowsDir() { return null; }

  supportsSubagents() { return false; }
  supportsSlashCommands() { return false; }
  supportsHooks() { return false; }

  /**
   * Heuristic: Codex is present on the user's machine if `~/.codex/` exists
   * (created automatically by `codex` CLI on first run). Pure best-effort
   * for the `configure` autodetect — the user can always toggle the flag.
   */
  static detect() {
    try { return fs.existsSync(path.join(os.homedir(), '.codex')); }
    catch { return false; }
  }
}

module.exports = CodexAdapter;
