import {
  Channel,
  OnInboundMessage,
  OnChatMetadata,
  RegisteredGroup,
} from '../types.js';
import type { DmAutoRegisterInfo, MentionAutoRegisterInfo } from './discord.js';

export interface ChannelOpts {
  onMessage: OnInboundMessage;
  onChatMetadata: OnChatMetadata;
  registeredGroups: () => Record<string, RegisteredGroup>;
  onDmAutoRegister?: (info: DmAutoRegisterInfo) => boolean;
  onMentionAutoRegister?: (info: MentionAutoRegisterInfo) => boolean;
  onSlashCommand?: (chatJid: string, command: string) => void;
  onChannelDelete?: (info: { channelId: string }) => void;
  onCategoryAutoRegister?: (info: { channelId: string; channelName: string; categoryId: string | null }) => boolean;
  onGuildMemberAdd?: (userId: string, username: string, member: any) => void;
  onPairingAttempt?: (userId: string, code: string) => Promise<string | null>;
}

export type ChannelFactory = (opts: ChannelOpts) => Channel | null;

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

export function registerChannel(name: string, factory: ChannelFactory): void {
  registry.set(name, factory);
}

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

export function getRegisteredChannelNames(): string[] {
  return [...registry.keys()];
}
