// Collect iMessage allowlist from the user during install.
// The actual wiring (writing access.json, registering the MCP in the agent's
// .mcp.json) happens in install-hook.js after the module is copied into place.

export async function setup(systemConfig, readline) {
  const ask = (prompt) => new Promise((resolve) => readline.question(prompt, resolve));

  console.log("\niMessage Module Setup");
  console.log("─────────────────────────────────────────────");
  console.log("This module lets your agent read and send iMessages via Messages.app.");
  console.log("It reads ~/Library/Messages/chat.db and sends via osascript.\n");
  console.log("Two macOS permissions are required AFTER install:");
  console.log("  • Full Disk Access for /usr/libexec/sshd-keygen-wrapper");
  console.log("    (or the terminal/process running the agent)");
  console.log("  • Automation → Messages — auto-prompts on first send\n");
  console.log("You must also be signed into Messages.app with iMessage enabled.\n");

  console.log("Allowlist: only these contacts can message the agent. List phone");
  console.log("numbers (e.g. +15551234567) and/or email addresses, comma-separated.");
  console.log("Anyone not on this list is silently dropped.\n");

  const raw = (await ask("Allowlist (comma-separated): ")).trim();
  const allowFrom = raw.split(",").map((s) => s.trim()).filter(Boolean);

  if (allowFrom.length === 0) {
    console.warn(
      "  Warning: empty allowlist — the agent will not respond to any iMessage."
    );
    console.warn(
      "  Add contacts later by editing <installPath>/services/imessage/data/access.json."
    );
  } else {
    console.log(`  Configured allowlist (${allowFrom.length}):`);
    for (const c of allowFrom) console.log(`    • ${c}`);
  }

  return { allowFrom };
}
