/* Wa by Elivate — shared helpers: rule metadata + summary builder */

const TRIGGERS = {
  message:    { label: "Incoming message", short: "message", icon: "message", tone: "brand" },
  group_join: { label: "Member joins group", short: "member joins", icon: "userPlus", tone: "green" },
  schedule:   { label: "Schedule", short: "on schedule", icon: "clock", tone: "violet" },
};

const MATCH_MODES = {
  contains:   { label: "Contains", verb: "contains" },
  equals:     { label: "Equals", verb: "is exactly" },
  startsWith: { label: "Starts with", verb: "starts with" },
  any:        { label: "Any message", verb: "any message" },
};

const SCOPES = {
  any:   { label: "Any chat", short: "any chat", icon: "globe" },
  dm:    { label: "DMs only", short: "DMs", icon: "message" },
  group: { label: "A specific group", short: "a group", icon: "users" },
};

const ACTIONS = {
  reply:         { label: "Reply with text", short: "reply", icon: "message", tone: "brand", needsMsg: true },
  send_group:    { label: "Send to a group", short: "send to group", icon: "send", tone: "blue", needsMsg: true },
  remove_sender: { label: "Remove the sender", short: "remove sender", icon: "userMinus", tone: "red", needsMsg: false },
  lock_group:    { label: "Lock group", short: "lock group", icon: "lock", tone: "amber", needsMsg: false },
  unlock_group:  { label: "Unlock group", short: "unlock group", icon: "unlock", tone: "green", needsMsg: false },
  openai_reply:  { label: "Ask ChatGPT", short: "ask ChatGPT", icon: "message", tone: "violet", needsMsg: false },
};

// Build the live plain-English sentence (SPEC §7.4)
function summarize(rule) {
  const parts = [];
  const t = rule.trigger.type;
  if (t === "message") {
    const m = rule.match;
    if (m.mode === "any") parts.push(["When", null], ["any message", "match"]);
    else parts.push(["When a message", null], [MATCH_MODES[m.mode].verb + " “" + (m.keyword || "…") + "”", "match"]);
  } else if (t === "group_join") {
    parts.push(["When", null], ["a member joins", "trigger"]);
  } else {
    parts.push(["On schedule", "trigger"], [formatSchedule(rule.trigger), "mono"]);
  }
  // scope
  const s = rule.scope;
  if (s.type === "group") parts.push(["in", null], [s.groupName || "a group", "scope"]);
  else if (s.type === "dm") parts.push(["in", null], ["DMs", "scope"]);
  else parts.push(["in", null], ["any chat", "scope"]);
  // action
  parts.push(["→", "arrow"]);
  const a = rule.action;
  const ai = ACTIONS[a.type];
  if (a.type === "reply" || a.type === "send_group") {
    parts.push([ai.short, "action"], ["“" + (a.message ? a.message.slice(0, 42) + (a.message.length > 42 ? "…" : "") : "…") + "”", "quote"]);
  } else if (a.type === "openai_reply") {
    parts.push([ai.short, "action"], [a.instructions ? "with rule context" : "with Settings context", "quote"]);
  } else {
    parts.push([ai.short, "action"]);
  }
  return parts;
}

function formatSchedule(trigger = {}) {
  const schedule = trigger.schedule || {};
  if (schedule.mode === "once") return `${schedule.date || "date"} ${schedule.time || ""}`.trim();
  if (schedule.mode === "daily") return `daily at ${schedule.time || "09:00"}`;
  if (schedule.mode === "weekdays") return `weekdays at ${schedule.time || "09:00"}`;
  if (schedule.mode === "weekly") return `weekly at ${schedule.time || "09:00"}`;
  if (schedule.mode === "monthly") return `monthly day ${schedule.dayOfMonth || 1} at ${schedule.time || "09:00"}`;
  if (schedule.mode === "hourly_window") return `hourly ${schedule.startTime || "06:00"}-${schedule.endTime || "01:00"}`;
  return "(" + (trigger.cron || "cron") + ")";
}

window.RuleMeta = { TRIGGERS, MATCH_MODES, SCOPES, ACTIONS, summarize };
