"use strict";

const path = require("path");
const os = require("os");

const PACKAGE_NAME = "@evolvconsulting/evolv-coder-kit";
const PACKAGE_ROOT = path.resolve(__dirname, "..", "..");
const GLOBAL_CLAUDE_DIR = path.join(os.homedir(), ".claude");

const MIGRATIONS_STATE_FILE = path.join(
  GLOBAL_CLAUDE_DIR,
  "evolv-coder-kit",
  ".migrations",
);

const EXIT_SUCCESS = 0;
const EXIT_ERROR = 1;
const EXIT_INTERRUPTED = 130;
const EXIT_DRIFT = 2;

const LOCK_FILE = path.join(
  GLOBAL_CLAUDE_DIR,
  "evolv-coder-kit",
  ".install-lock",
);

const DISABLED_STATE_FILE = path.join(
  GLOBAL_CLAUDE_DIR,
  "evolv-coder-kit",
  ".disabled",
);
const LOCK_STALE_MS = 10 * 60 * 1000; // 10 minutes
const CHECKSUM_DISPLAY_LENGTH = 12; // truncated SHA-256 for display

const LEGACY_PACKAGE_NAME = "@evolvconsulting/claude-code-kit";

const MARKETPLACE_REPO = "evolvconsulting/evolv-marketplace";
const MARKETPLACE_KEY = "evolv-marketplace";

const STANDARDS_REPO = "evolvconsulting/evolv-coder-standards";
const STANDARDS_CACHE_DIR = path.join(
  GLOBAL_CLAUDE_DIR,
  "evolv-coder-standards",
);

// Archived migration IDs — removed from MIGRATIONS but IDs must never be reused.
// See docs/contributing/migration-authoring.md for archival policy.
const ARCHIVED_IDS = [
  "001",
  "002",
  "003",
  "004",
  "005",
  "006",
  "007",
  "008",
  "009",
  "010",
  "011",
  "012",
  "013",
  "014",
  "015",
  "016",
  "017",
  "018",
];

const SAFETY_HOOK_DEFINITIONS = [
  {
    name: "protect-files",
    filename: "protect-files.sh",
    event: "PreToolUse",
    matcher: "Edit|Write",
    commandTemplate: '"$CLAUDE_PROJECT_DIR"/.claude/hooks/protect-files.sh',
  },
  {
    name: "detect-secrets",
    filename: "detect-secrets.sh",
    event: "PreToolUse",
    matcher: "Bash",
    commandTemplate: '"$CLAUDE_PROJECT_DIR"/.claude/hooks/detect-secrets.sh',
  },
  {
    name: "hitl-approval",
    filename: "hitl-approval.sh",
    event: "PreToolUse",
    matcher: "Bash",
    commandTemplate: '"$CLAUDE_PROJECT_DIR"/.claude/hooks/hitl-approval.sh',
  },
  {
    name: "security-gate",
    filename: "security-gate.sh",
    event: "PreToolUse",
    matcher: "Bash",
    commandTemplate: '"$CLAUDE_PROJECT_DIR"/.claude/hooks/security-gate.sh',
  },
];
const SAFETY_HOOK_FILENAMES = SAFETY_HOOK_DEFINITIONS.map((d) => d.filename);

const INSTALL_TARGETS = {
  global: {
    skills: {
      src: path.join(PACKAGE_ROOT, "scaffold", "global", "skills"),
      dest: path.join(GLOBAL_CLAUDE_DIR, "skills"),
      subDirPrefix: "eck-",
      label:
        "Global skills (eck:spec, eck:design, eck:develop, eck:validate, eck:deploy, eck:quick-task, eck:pause, eck:resume, eck:new-project, eck:migrate-project, eck:audit-unmanaged-project, eck:audit-current-project, eck:audit-security, eck:archive-project, eck:restore-project, eck:pull-from-global-catalog, eck:pull-from-user-catalog, eck:push-to-user-catalog, eck:submit-to-repo, eck:project-status, eck:pull-from-standards, eck:list-global-catalog, eck:list-user-catalog, eck:list-project-catalog, eck:disable-protections, eck:enable-protections, eck:fresh-context, eck:dev-rigor, eck:output-mode, eck:history, eck:help, eck:update-project, eck:update-permissions, eck:ac-coverage, eck:sandbox, eck:sudo)",
    },
    templates: {
      src: path.join(PACKAGE_ROOT, "scaffold", "project"),
      dest: path.join(GLOBAL_CLAUDE_DIR, "evolv-coder-kit"),
      label: "Project-kickstart scaffold (agents, skills, hooks, primitives)",
    },
    hooks: {
      src: path.join(
        PACKAGE_ROOT,
        "scaffold",
        "global",
        "compiled-hooks",
        "dist",
      ),
      dest: path.join(GLOBAL_CLAUDE_DIR, "hooks"),
      label:
        "Hooks (update checker, statusline, context monitor, session guidance)",
    },
    outputModes: {
      src: path.join(PACKAGE_ROOT, "scaffold", "project", "output-modes"),
      dest: path.join(GLOBAL_CLAUDE_DIR, "output-modes"),
      label: "Output modes (guided, standard, concise, pair, review)",
    },
  },
};

module.exports = {
  PACKAGE_NAME,
  PACKAGE_ROOT,
  GLOBAL_CLAUDE_DIR,
  MIGRATIONS_STATE_FILE,
  EXIT_SUCCESS,
  EXIT_ERROR,
  EXIT_INTERRUPTED,
  EXIT_DRIFT,
  LOCK_FILE,
  LOCK_STALE_MS,
  CHECKSUM_DISPLAY_LENGTH,
  LEGACY_PACKAGE_NAME,
  MARKETPLACE_REPO,
  MARKETPLACE_KEY,
  STANDARDS_REPO,
  STANDARDS_CACHE_DIR,
  ARCHIVED_IDS,
  SAFETY_HOOK_DEFINITIONS,
  SAFETY_HOOK_FILENAMES,
  INSTALL_TARGETS,
  DISABLED_STATE_FILE,
};
