/**
 * Group registration and settings via platform IPC.
 */
import path from "path";
import { getContext } from "../client.js";
import { writeIpcFile } from "../ipc.js";

function groupFolder(): string {
  return process.env.NANOCLAW_GROUP_FOLDER ?? "";
}

function messagesDir(ipcDir: string): string {
  return path.join(ipcDir, "messages");
}

function tasksDir(ipcDir: string): string {
  return path.join(ipcDir, "tasks");
}

export async function registerGroup(
  params: { jid: string; name: string; folder: string; trigger: string },
  ipcDir?: string
): Promise<void> {
  const dir = ipcDir ?? getContext().ipcDir;
  writeIpcFile(messagesDir(dir), {
    type: "register_group",
    jid: params.jid,
    name: params.name,
    folder: params.folder,
    trigger: params.trigger,
    groupFolder: groupFolder(),
  });
}

export async function updateGroupSettings(
  params: { requiresTrigger?: boolean },
  ipcDir?: string
): Promise<void> {
  const dir = ipcDir ?? getContext().ipcDir;
  writeIpcFile(tasksDir(dir), {
    type: "update_group_settings",
    jid: process.env.NANOCLAW_CHAT_JID,
    requiresTrigger: params.requiresTrigger,
    groupFolder: groupFolder(),
  });
}
