import { createHash } from "node:crypto";
import { COMMUNICATION_CHANNELS_QUEUES } from "./lib/queue.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("communication_channels");
const POLL_TICK_INTERVAL_SECONDS = Math.max(
  10,
  Number.parseInt(process.env.OM_HUB_POLL_SCHEDULER_TICK_SECONDS ?? "60", 10) || 60
);
function stableScheduleUuid(stableKey) {
  const hex = createHash("sha256").update(stableKey).digest("hex");
  return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
}
function stablePollTickScheduleId(organizationId) {
  return stableScheduleUuid(`communication_channels:poll-tick:${organizationId}`);
}
const setup = {
  defaultRoleFeatures: {
    superadmin: [
      "communication_channels.view",
      "communication_channels.manage",
      "communication_channels.react",
      "communication_channels.assign",
      "communication_channels.connect_user_channel",
      "communication_channels.admin",
      "communication_channels.channel.import_history",
      "communication_channels.channel.push.manage"
    ],
    admin: [
      "communication_channels.view",
      "communication_channels.manage",
      "communication_channels.react",
      "communication_channels.assign",
      "communication_channels.connect_user_channel",
      "communication_channels.admin",
      "communication_channels.channel.import_history",
      "communication_channels.channel.push.manage"
    ],
    manager: [
      "communication_channels.view",
      "communication_channels.manage",
      "communication_channels.react",
      "communication_channels.assign",
      "communication_channels.connect_user_channel"
    ],
    employee: [
      "communication_channels.view",
      "communication_channels.react",
      "communication_channels.connect_user_channel"
    ]
  },
  async seedDefaults({ container, organizationId, tenantId }) {
    const cradle = container;
    if (typeof cradle.hasRegistration !== "function" || !cradle.hasRegistration("schedulerService")) {
      return;
    }
    const schedulerService = container.resolve("schedulerService");
    try {
      await schedulerService.register({
        id: stablePollTickScheduleId(organizationId),
        name: "Communication channels poll tick",
        description: `Enumerates active polling channels every ${POLL_TICK_INTERVAL_SECONDS}s and enqueues per-channel poll jobs.`,
        scopeType: "organization",
        organizationId,
        tenantId,
        scheduleType: "interval",
        scheduleValue: `${POLL_TICK_INTERVAL_SECONDS}s`,
        timezone: "UTC",
        targetType: "queue",
        targetQueue: COMMUNICATION_CHANNELS_QUEUES.pollTick,
        targetPayload: {
          scope: { tenantId, organizationId }
        },
        sourceType: "module",
        sourceModule: "communication_channels",
        isEnabled: true
      });
      await schedulerService.register({
        id: stableScheduleUuid(`communication_channels:gmail-renew-watch:${organizationId}`),
        name: "Gmail watch renewal",
        description: "Daily 04:00 UTC. Re-issues gmail.users.watch for channels within OM_PUSH_RENEWAL_GMAIL_LEAD_HOURS of expiry.",
        scopeType: "organization",
        organizationId,
        tenantId,
        scheduleType: "cron",
        scheduleValue: "0 4 * * *",
        timezone: "UTC",
        targetType: "queue",
        targetQueue: COMMUNICATION_CHANNELS_QUEUES.gmailRenewWatch,
        targetPayload: { scope: { tenantId, organizationId } },
        sourceType: "module",
        sourceModule: "communication_channels",
        isEnabled: true
      });
    } catch (error) {
      logger.warn("Failed to register module schedules", { err: error });
    }
  }
};
var setup_default = setup;
export {
  setup_default as default,
  setup
};
//# sourceMappingURL=setup.js.map
