/** 渠道配置字段（对齐原版 channelConfigFields） */
export type NotifyField = {
  key: string
  label: string
  required: boolean
  placeholder?: string
  type?: 'text' | 'textarea' | 'note'
}

export const CHANNEL_CONFIG_FIELDS: Record<string, NotifyField[]> = {
  Telegram: [
    { key: 'bot_token', label: 'Bot Token', required: true, placeholder: '从 @BotFather 获取' },
    { key: 'chat_id', label: 'Chat ID', required: true, placeholder: '聊天/群组 ID' },
    { key: 'api_host', label: 'API 地址', required: false, placeholder: '自定义 API 地址，留空使用官方' },
    { key: 'proxy_url', label: '代理地址', required: false, placeholder: 'http/https/socks5 代理' },
  ],
  Bark: [
    { key: 'server', label: '服务地址', required: false, placeholder: '默认 https://api.day.app' },
    { key: 'push_key', label: 'Push Key', required: true, placeholder: 'Bark Push Key' },
    { key: 'proxy_url', label: '代理地址', required: false, placeholder: 'http/https/socks5 代理' },
    { key: 'sound', label: '推送声音', required: false, placeholder: '留空使用默认' },
    { key: 'badge', label: '角标数量', required: false, placeholder: '例如 1' },
    { key: 'group', label: '推送分组', required: false },
    { key: 'icon', label: '推送图标', required: false, placeholder: '图标 URL' },
    { key: 'level', label: '时效性', required: false, placeholder: 'active / timeSensitive / passive' },
    { key: 'url', label: '跳转URL', required: false },
    { key: 'copy', label: '复制内容', required: false },
    { key: 'auto_copy', label: '自动复制', required: false, placeholder: '1 表示开启' },
  ],
  Dtalk: [
    { key: 'access_token', label: 'Access Token', required: true, placeholder: '钉钉机器人 access_token' },
    { key: 'secret', label: '加签秘钥', required: false, placeholder: '可选' },
  ],
  QyWeiXin: [
    { key: 'access_token', label: 'Access Token', required: true, placeholder: '企业微信机器人 Key' },
  ],
  Feishu: [
    { key: 'access_token', label: 'Access Token', required: true, placeholder: '飞书机器人 access_token' },
    { key: 'secret', label: '加签秘钥', required: false, placeholder: '可选' },
  ],
  Custom: [
    { key: 'webhook', label: 'Webhook URL', required: true, placeholder: 'https://...' },
    { key: 'headers', label: '请求头', required: false, placeholder: 'JSON 格式', type: 'textarea' },
    { key: 'body', label: '请求体模板', required: false, placeholder: '使用 TEXT 作为消息内容占位符', type: 'textarea' },
  ],
  Ntfy: [
    { key: 'topic', label: 'Topic', required: true },
    { key: 'url', label: 'API 地址', required: false, placeholder: '默认 https://ntfy.sh' },
    { key: 'priority', label: '优先级', required: false, placeholder: '1-5' },
    { key: 'icon', label: '图标 URL', required: false },
    { key: 'token', label: 'Token', required: false },
    { key: 'username', label: '用户名', required: false },
    { key: 'password', label: '密码', required: false },
  ],
  Gotify: [
    { key: 'url', label: '服务地址', required: true, placeholder: 'https://gotify.example.com' },
    { key: 'token', label: 'Token', required: true },
    { key: 'priority', label: '优先级', required: false, placeholder: '0-10' },
  ],
  PushMe: [
    { key: 'push_key', label: 'Push Key', required: true },
    { key: 'url', label: 'API 地址', required: false, placeholder: '默认 https://push.i-i.me' },
    { key: 'type', label: '类型', required: false },
  ],
  Email: [
    { key: 'server', label: 'SMTP 服务器', required: true, placeholder: 'smtp.example.com' },
    { key: 'port', label: '端口', required: true, placeholder: '465' },
    { key: 'account', label: '邮箱账号', required: true },
    { key: 'passwd', label: '邮箱密码', required: true },
    { key: 'from_name', label: '发信人名称', required: false },
    { key: 'to_account', label: '收件邮箱', required: true },
  ],
  AliyunSMS: [
    { key: 'access_key_id', label: 'AccessKeyId', required: true },
    { key: 'access_key_secret', label: 'AccessKeySecret', required: true },
    { key: 'sign_name', label: '短信签名', required: true },
    { key: 'region_id', label: '区域ID', required: false, placeholder: '默认 cn-hangzhou' },
    { key: 'phone_number', label: '手机号码', required: true },
    { key: 'template_code', label: '短信模板 CODE', required: true },
  ],
  PushPlus: [
    { key: 'token', label: 'Token', required: true, placeholder: 'PushPlus Token' },
    { key: 'topic', label: '群组编码', required: false },
    { key: 'template', label: '推送模板', required: false, placeholder: 'html, txt, json, markdown' },
    { key: 'channel', label: '推送渠道', required: false, placeholder: 'wechat, dingding, feishu, mail等' },
    { key: 'webhook', label: 'Webhook', required: false },
    { key: 'callback_url', label: '回调地址', required: false },
    { key: 'to', label: '好友令牌', required: false },
  ],
  VoceChat: [
    { key: 'server', label: '服务地址', required: true, placeholder: 'https://vocechat.yourdomain.com' },
    { key: 'api_key', label: 'API Key', required: true, placeholder: 'Bot API Key' },
    { key: 'target_id', label: '目标 ID', required: true, placeholder: 'uid 或 gid' },
    { key: 'target_type', label: '目标类型', required: false, placeholder: 'user (默认) / group' },
  ],
  WxPusher: [
    { key: 'app_token', label: 'AppToken', required: true, placeholder: 'AT_...' },
    { key: 'uids', label: 'UIDs', required: false, placeholder: '用户 UID，多个用逗号分隔' },
    { key: 'topic_ids', label: 'TopicIDs', required: false, placeholder: '主题 ID，多个用逗号分隔' },
    { key: 'verify_pay_type', label: '付费验证', required: false, placeholder: '0:不验证, 1:仅付费, 2:仅未订阅/过期' },
  ],
  // 兼容小写 type
  telegram: [
    { key: 'bot_token', label: 'Bot Token', required: true },
    { key: 'chat_id', label: 'Chat ID', required: true },
    { key: 'api_host', label: 'API 地址', required: false },
    { key: 'proxy_url', label: '代理地址', required: false },
  ],
  webhook: [
    { key: 'webhook', label: 'Webhook URL', required: true },
    { key: 'headers', label: '请求头', required: false, type: 'textarea' },
    { key: 'body', label: '请求体模板', required: false, type: 'textarea' },
  ],
}

const TYPE_ALIASES: Record<string, string> = {
  PushMes: 'PushMe',
  pushme: 'PushMe',
  pushmes: 'PushMe',
  webhook: 'Custom',
  custom: 'Custom',
}

export function getChannelFields(type: string): NotifyField[] {
  if (!type) return []
  const normalized = TYPE_ALIASES[type] || type
  return (
    CHANNEL_CONFIG_FIELDS[normalized] ||
    CHANNEL_CONFIG_FIELDS[type] ||
    CHANNEL_CONFIG_FIELDS[type.charAt(0).toUpperCase() + type.slice(1)] ||
    []
  )
}

export function emptyConfigForType(type: string, existing: Record<string, any> = {}) {
  const fields = getChannelFields(type)
  const cfg: Record<string, string> = {}
  for (const f of fields) {
    cfg[f.key] = existing[f.key] != null ? String(existing[f.key]) : ''
  }
  // 保留未知字段
  Object.keys(existing || {}).forEach((k) => {
    if (cfg[k] === undefined) cfg[k] = String(existing[k] ?? '')
  })
  return cfg
}
