/**
 * notifiers/index.ts — Registry
 * Unified management of all notification channels
 */
import { Notifier, NotifyParams, NotifyResult } from './notifier.types';
import { barkNotifier } from './bark.notifier';
import { serverchanNotifier } from './serverchan.notifier';
import { serverchanturboNotifier } from './serverchanturbo.notifier';
import { telegramNotifier } from './telegram.notifier';
import { larkNotifier } from './lark.notifier';
import { webhookNotifier } from './webhook.notifier';
import { wechatWorkBotNotifier } from './wechat_work_bot.notifier';
import { pushplusNotifier } from './pushplus.notifier';
import { dingtalkNotifier } from './dingtalk.notifier';
import { gotifyNotifier } from './gotify.notifier';
import { ntfyNotifier } from './ntfy.notifier';
import { discordNotifier } from './discord.notifier';
import { smtpNotifier } from './smtp.notifier';
import { qmsgNotifier } from './qmsg.notifier';

const registry = new Map<string, Notifier>();

function register(n: Notifier): void {
  registry.set(n.name, n);
}

// Register all built-in notifiers
register(barkNotifier);
register(serverchanNotifier);
register(serverchanturboNotifier);
register(telegramNotifier);
register(larkNotifier);
register(webhookNotifier);
register(wechatWorkBotNotifier);
register(pushplusNotifier);
register(dingtalkNotifier);
register(gotifyNotifier);
register(ntfyNotifier);
register(discordNotifier);
register(smtpNotifier);
register(qmsgNotifier);

export function getNotifier(name: string): Notifier | undefined {
  return registry.get(name);
}

export function getAllNotifiers(): Notifier[] {
  return Array.from(registry.values());
}

export function getAllNotifierParams(): Record<string, { name: string; label: string; params: any[] }> {
  const result: Record<string, any> = {};
  for (const [name, n] of registry) {
    result[name] = { name: n.name, label: n.label, params: n.params };
  }
  return result;
}

export async function notifyWith(name: string, params: NotifyParams): Promise<NotifyResult> {
  const n = getNotifier(name);
  if (!n) return { success: false, message: `Unknown channel: ${name}` };
  return n.notify(params);
}
