import path from "node:path";
import { exists } from "../fsx.ts";
import type { TargetAdapter, TargetContext } from "../types.ts";
import { anyExists, scopeRoot, skillDirActions } from "./common.ts";

/**
 * Codex CLI — ~/.codex/skills/designpaca/.
 * Codex 도 Claude Code 와 같은 SKILL.md 규약을 쓴다(실측: ~/.codex/skills/frontend-design 등).
 */
export const codex: TargetAdapter = {
  id: "codex",
  label: "Codex CLI",
  hint: "~/.codex/skills/designpaca — SKILL.md 그대로",
  scopes: ["user", "project"],

  async detect(ctx: TargetContext) {
    return anyExists([
      path.join(ctx.home, ".codex"),
      path.join(ctx.home, ".codex", "AGENTS.md"),
      path.join(ctx.cwd, ".codex"),
    ]);
  },

  async plan(ctx: TargetContext) {
    const root = scopeRoot(ctx, [".codex", "skills", "designpaca"], [".codex", "skills", "designpaca"]);
    return {
      target: this.id,
      scope: ctx.scope,
      root,
      actions: skillDirActions(root, ctx.skill),
      alreadyInstalled: await exists(path.join(root, "SKILL.md")),
    };
  },
};
