import * as fs from "fs";
import * as path from "path";
import * as os from "os";
export { getBundledSkillsDir } from "../../utils/package-assets";

const SKILL_DIR_NAME = "skill";
const SKILLS_DIR_NAME = "skills";

export type SkillTargetAgent = "opencode" | "codex" | "claudecode" | "ampcli" | "agents";

export interface SkillTarget {
  agents: SkillTargetAgent[];
  dir: string;
}

export function getGlobalSkillDir(): string {
  const configDir = process.env.OPENCODE_CONFIG_DIR
    || path.join(os.homedir(), ".config", "opencode");
  return path.join(configDir, SKILL_DIR_NAME);
}

export function getLocalSkillDir(): string {
  return path.join(process.cwd(), ".opencode", SKILL_DIR_NAME);
}

function getCodexHomeDir(): string {
  return process.env.CODEX_HOME
    || path.join(os.homedir(), ".codex");
}

function getClaudeCodeHomeDir(): string {
  return process.env.CLAUDECODE_HOME
    || path.join(os.homedir(), ".claude");
}

function getAmpHomeDir(): string {
  return process.env.AMP_CLI_HOME
    || path.join(os.homedir(), ".amp");
}

function getAgentsSkillDir(): string {
  return path.join(os.homedir(), ".agents", SKILLS_DIR_NAME);
}

function dedupeTargets(targets: Array<{ agent: SkillTargetAgent; dir: string }>): SkillTarget[] {
  const deduped = new Map<string, SkillTarget>();

  for (const target of targets) {
    const key = path.resolve(target.dir);
    const existing = deduped.get(key);
    if (existing) {
      if (!existing.agents.includes(target.agent)) {
        existing.agents.push(target.agent);
      }
      continue;
    }
    deduped.set(key, { agents: [target.agent], dir: target.dir });
  }

  return Array.from(deduped.values());
}

export function getGlobalSkillTargets(): SkillTarget[] {
  const claudeSkillsDir = path.join(getClaudeCodeHomeDir(), SKILLS_DIR_NAME);
  const ampSkillsDir = path.join(getAmpHomeDir(), SKILLS_DIR_NAME);
  const agentsSkillsDir = getAgentsSkillDir();

  return dedupeTargets([
    { agent: "opencode", dir: getGlobalSkillDir() },
    { agent: "codex", dir: agentsSkillsDir },
    { agent: "claudecode", dir: claudeSkillsDir },
    { agent: "ampcli", dir: ampSkillsDir },
    { agent: "agents", dir: agentsSkillsDir }
  ]);
}

export function getLocalSkillTargets(): SkillTarget[] {
  const localClaudeSkillsDir = path.join(process.cwd(), ".claude", SKILLS_DIR_NAME);
  const localAmpSkillsDir = path.join(process.cwd(), ".amp", SKILLS_DIR_NAME);
  const localAgentsSkillsDir = path.join(process.cwd(), ".agents", SKILLS_DIR_NAME);

  return dedupeTargets([
    { agent: "opencode", dir: getLocalSkillDir() },
    { agent: "codex", dir: localAgentsSkillsDir },
    { agent: "claudecode", dir: localClaudeSkillsDir },
    { agent: "ampcli", dir: localAmpSkillsDir },
    { agent: "agents", dir: localAgentsSkillsDir }
  ]);
}

export function getLegacyCodexSkillTargets(mode: "global" | "local"): SkillTarget[] {
  return mode === "global"
    ? [{ agents: ["codex"], dir: path.join(getCodexHomeDir(), SKILLS_DIR_NAME) }]
    : [{ agents: ["codex"], dir: path.join(process.cwd(), ".codex", SKILLS_DIR_NAME) }];
}
