/**
 * Built-in chat channel registry: stable ordering and UI-facing metadata.
 * Extension-provided channels register separately; keep protocol IDs aligned.
 */

import type { ChannelCapabilities } from './plugin-types.js';

export const CHAT_CHANNEL_ORDER = ['telegram', 'weixin', 'feishu'] as const;

export type ChatChannelId = (typeof CHAT_CHANNEL_ORDER)[number];

export interface ChatChannelMeta {
  id: ChatChannelId;
  label: string;
  description: string;
  capabilities: ChannelCapabilities;
  /** Optional UI/dock tuning (queue debounce, outbound chunk size). */
  dock?: {
    outbound?: { textChunkLimit?: number };
    queue?: { debounceMs?: number };
  };
}

const DEFAULT_CAPABILITIES: ChannelCapabilities = {
  chatTypes: ['direct', 'group', 'channel', 'thread'],
  reactions: true,
  threads: true,
  media: true,
  polls: false,
  nativeCommands: true,
  blockStreaming: true,
};

const WEIXIN_CAPABILITIES: ChannelCapabilities = {
  chatTypes: ['direct'],
  reactions: false,
  threads: false,
  media: true,
  polls: false,
  nativeCommands: false,
  blockStreaming: true,
};

const FEISHU_CAPABILITIES: ChannelCapabilities = {
  chatTypes: ['direct', 'channel'],
  reactions: true,
  threads: true,
  media: true,
  polls: false,
  nativeCommands: false,
  blockStreaming: false,
  edit: true,
  reply: true,
};

const CHAT_CHANNEL_META: Record<ChatChannelId, ChatChannelMeta> = {
  telegram: {
    id: 'telegram',
    label: 'Telegram',
    description: 'Telegram Bot API (GrammY)',
    capabilities: DEFAULT_CAPABILITIES,
    dock: {
      outbound: { textChunkLimit: 4000 },
      queue: { debounceMs: 300 },
    },
  },
  weixin: {
    id: 'weixin',
    label: 'Weixin',
    description: 'WeChat via Tencent ilink (QR login)',
    capabilities: WEIXIN_CAPABILITIES,
    dock: {
      outbound: { textChunkLimit: 4000 },
      queue: { debounceMs: 0 },
    },
  },
  feishu: {
    id: 'feishu',
    label: 'Feishu',
    description: 'Feishu/Lark enterprise messaging (Socket Mode)',
    capabilities: FEISHU_CAPABILITIES,
    dock: {
      outbound: { textChunkLimit: 4000 },
      queue: { debounceMs: 350 },
    },
  },
};

export function getChatChannelMeta(id: ChatChannelId): ChatChannelMeta {
  return CHAT_CHANNEL_META[id];
}

export function isChatChannelId(id: string): id is ChatChannelId {
  return (CHAT_CHANNEL_ORDER as readonly string[]).includes(id);
}

export function listChatChannelMeta(): ChatChannelMeta[] {
  return CHAT_CHANNEL_ORDER.map((id) => CHAT_CHANNEL_META[id]);
}
