/**
 * ZCLAW Chinese Models Plugin
 * 
 * Registers Chinese AI model providers for OpenClaw Gateway:
 * - Zhipu GLM (智谱)
 * - Qwen (通义千问)
 * - Kimi (月之暗面)
 * - MiniMax
 * 
 * All providers use OpenAI-compatible API format.
 */

interface PluginAPI {
  config: Record<string, any>;
  registerProvider(provider: ProviderDefinition): void;
  registerHook(event: string, handler: (...args: any[]) => any, meta?: Record<string, any>): void;
}

interface ProviderDefinition {
  id: string;
  name: string;
  baseUrl: string;
  apiKeyEnvVar?: string;
  models: ProviderModel[];
  headers?: (apiKey: string) => Record<string, string>;
}

interface ProviderModel {
  id: string;
  alias?: string;
  contextWindow?: number;
  maxOutputTokens?: number;
  supportsVision?: boolean;
  supportsStreaming?: boolean;
}

// 智谱 GLM Provider
const zhipuProvider: ProviderDefinition = {
  id: 'zhipu',
  name: 'Zhipu AI (智谱)',
  baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
  apiKeyEnvVar: 'ZHIPU_API_KEY',
  models: [
    {
      id: 'glm-5',
      alias: 'GLM-5',
      contextWindow: 128000,
      maxOutputTokens: 4096,
      supportsVision: true,
      supportsStreaming: true,
    },
    {
      id: 'glm-4.7',
      alias: 'GLM-4.7',
      contextWindow: 128000,
      maxOutputTokens: 4096,
      supportsVision: true,
      supportsStreaming: true,
    },
    {
      id: 'glm-4-plus',
      alias: 'GLM-4-Plus',
      contextWindow: 128000,
      maxOutputTokens: 4096,
      supportsVision: false,
      supportsStreaming: true,
    },
    {
      id: 'glm-4-flash',
      alias: 'GLM-4-Flash',
      contextWindow: 128000,
      maxOutputTokens: 4096,
      supportsVision: false,
      supportsStreaming: true,
    },
  ],
  headers: (apiKey: string) => ({
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  }),
};

// 通义千问 Provider (OpenAI-compatible via DashScope)
const qwenProvider: ProviderDefinition = {
  id: 'qwen',
  name: 'Qwen (通义千问)',
  baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
  apiKeyEnvVar: 'QWEN_API_KEY',
  models: [
    {
      id: 'qwen3.5-plus',
      alias: 'Qwen3.5+',
      contextWindow: 131072,
      maxOutputTokens: 8192,
      supportsVision: false,
      supportsStreaming: true,
    },
    {
      id: 'qwen-max',
      alias: 'Qwen-Max',
      contextWindow: 32768,
      maxOutputTokens: 8192,
      supportsVision: false,
      supportsStreaming: true,
    },
    {
      id: 'qwen-vl-max',
      alias: 'Qwen-VL-Max',
      contextWindow: 32768,
      maxOutputTokens: 4096,
      supportsVision: true,
      supportsStreaming: true,
    },
  ],
  headers: (apiKey: string) => ({
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  }),
};

// Kimi (月之暗面) Provider
const kimiProvider: ProviderDefinition = {
  id: 'kimi',
  name: 'Kimi (月之暗面)',
  baseUrl: 'https://api.moonshot.cn/v1',
  apiKeyEnvVar: 'KIMI_API_KEY',
  models: [
    {
      id: 'kimi-k2.5',
      alias: 'Kimi-K2.5',
      contextWindow: 131072,
      maxOutputTokens: 8192,
      supportsVision: false,
      supportsStreaming: true,
    },
    {
      id: 'moonshot-v1-128k',
      alias: 'Moonshot-128K',
      contextWindow: 128000,
      maxOutputTokens: 4096,
      supportsVision: false,
      supportsStreaming: true,
    },
  ],
  headers: (apiKey: string) => ({
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  }),
};

// MiniMax Provider
const minimaxProvider: ProviderDefinition = {
  id: 'minimax',
  name: 'MiniMax',
  baseUrl: 'https://api.minimax.chat/v1',
  apiKeyEnvVar: 'MINIMAX_API_KEY',
  models: [
    {
      id: 'minimax-m2.5',
      alias: 'MiniMax-M2.5',
      contextWindow: 245760,
      maxOutputTokens: 16384,
      supportsVision: false,
      supportsStreaming: true,
    },
    {
      id: 'abab6.5s-chat',
      alias: 'ABAB-6.5s',
      contextWindow: 245760,
      maxOutputTokens: 8192,
      supportsVision: false,
      supportsStreaming: true,
    },
  ],
  headers: (apiKey: string) => ({
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  }),
};

/**
 * Plugin entry point - registers all Chinese model providers
 */
export default function register(api: PluginAPI) {
  const pluginConfig = api.config?.plugins?.entries?.['zclaw-chinese-models']?.config ?? {};

  // Register Zhipu GLM
  const zhipu = { ...zhipuProvider };
  if (pluginConfig.zhipuBaseUrl) zhipu.baseUrl = pluginConfig.zhipuBaseUrl;
  api.registerProvider(zhipu);

  // Register Qwen
  const qwen = { ...qwenProvider };
  if (pluginConfig.qwenBaseUrl) qwen.baseUrl = pluginConfig.qwenBaseUrl;
  api.registerProvider(qwen);

  // Register Kimi
  const kimi = { ...kimiProvider };
  if (pluginConfig.kimiBaseUrl) kimi.baseUrl = pluginConfig.kimiBaseUrl;
  api.registerProvider(kimi);

  // Register MiniMax
  const minimax = { ...minimaxProvider };
  if (pluginConfig.minimaxBaseUrl) minimax.baseUrl = pluginConfig.minimaxBaseUrl;
  api.registerProvider(minimax);

  // Log registration
  api.registerHook('gateway:startup', async () => {
    console.log('[ZCLAW] Chinese model providers registered: zhipu, qwen, kimi, minimax');
  }, { name: 'zclaw-chinese-models.startup', description: 'Log provider registration on startup' });
}
