import type {
  ProviderBackendConfig,
  ProviderDefinition,
  ProviderType,
  ProviderTypeInfo,
} from './types';

export const PROVIDER_DEFINITIONS: ProviderDefinition[] = [
  {
    id: 'anthropic',
    name: 'Anthropic',
    icon: '🤖',
    placeholder: 'sk-ant-api03-...',
    model: 'Claude',
    requiresApiKey: true,
    category: 'official',
    envVar: 'ANTHROPIC_API_KEY',
    defaultModelId: 'claude-opus-4-6',
    showModelId: true,
    modelIdPlaceholder: 'claude-opus-4-6',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
  },
  {
    id: 'openai',
    name: 'OpenAI',
    icon: '💚',
    placeholder: 'sk-proj-...',
    model: 'GPT',
    requiresApiKey: true,
    category: 'official',
    envVar: 'OPENAI_API_KEY',
    defaultModelId: 'gpt-5.5',
    isOAuth: true,
    supportsApiKey: true,
    showModelId: true,
    modelIdPlaceholder: 'gpt-5.5',
    supportedAuthModes: ['api_key', 'oauth_browser'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://api.openai.com/v1',
      api: 'openai-responses',
      apiKeyEnv: 'OPENAI_API_KEY',
    },
  },
  {
    id: 'google',
    name: 'Google',
    icon: '🔷',
    placeholder: 'AIza...',
    model: 'Gemini',
    requiresApiKey: true,
    category: 'official',
    envVar: 'GEMINI_API_KEY',
    defaultModelId: 'gemini-3.1-pro-preview',
    showModelId: true,
    modelIdPlaceholder: 'gemini-3.1-pro-preview',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
  },
  {
    id: 'openrouter',
    name: 'OpenRouter',
    icon: '🌐',
    placeholder: 'sk-or-v1-...',
    model: 'Multi-Model',
    requiresApiKey: true,
    showModelId: true,
    modelIdPlaceholder: 'anthropic/claude-opus-4.6',
    defaultModelId: 'anthropic/claude-opus-4.6',
    category: 'compatible',
    envVar: 'OPENROUTER_API_KEY',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://openrouter.ai/api/v1',
      api: 'openai-completions',
      apiKeyEnv: 'OPENROUTER_API_KEY',
      headers: {
        'HTTP-Referer': 'https://claw-x.com',
        'X-OpenRouter-Title': 'ClawX',
      },
    },
  },
  {
    id: 'ark',
    name: 'ByteDance Ark',
    icon: 'A',
    placeholder: 'your-ark-api-key',
    model: 'Doubao',
    requiresApiKey: true,
    defaultBaseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
    showBaseUrl: true,
    showModelId: true,
    modelIdPlaceholder: 'ep-20260228000000-xxxxx',
    category: 'official',
    envVar: 'ARK_API_KEY',
    codePlanPresetBaseUrl: 'https://ark.cn-beijing.volces.com/api/coding/v3',
    codePlanPresetModelId: 'ark-code-latest',
    codePlanDocsUrl: 'https://www.volcengine.com/docs/82379/1928261?lang=zh',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
      api: 'openai-completions',
      apiKeyEnv: 'ARK_API_KEY',
    },
  },
  {
    id: 'moonshot',
    name: 'Moonshot (CN)',
    icon: '🌙',
    placeholder: 'sk-...',
    model: 'Kimi',
    requiresApiKey: true,
    defaultBaseUrl: 'https://api.moonshot.cn/v1',
    defaultModelId: 'kimi-k2.6',
    showModelId: true,
    modelIdPlaceholder: 'kimi-k2.6',
    category: 'official',
    envVar: 'MOONSHOT_API_KEY',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://api.moonshot.cn/v1',
      api: 'openai-completions',
      apiKeyEnv: 'MOONSHOT_API_KEY',
      models: [
        {
          id: 'kimi-k2.6',
          name: 'Kimi K2.6',
          reasoning: false,
          input: ['text'],
          cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
          contextWindow: 256000,
          maxTokens: 8192,
        },
      ],
    },
  },
  {
    id: 'moonshot-global',
    name: 'Moonshot (Global)',
    icon: '🌙',
    placeholder: 'sk-...',
    model: 'Kimi',
    requiresApiKey: true,
    defaultBaseUrl: 'https://api.moonshot.ai/v1',
    defaultModelId: 'kimi-k2.6',
    showModelId: true,
    modelIdPlaceholder: 'kimi-k2.6',
    category: 'official',
    envVar: 'MOONSHOT_GLOBAL_API_KEY',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://api.moonshot.ai/v1',
      api: 'openai-completions',
      apiKeyEnv: 'MOONSHOT_GLOBAL_API_KEY',
      models: [
        {
          id: 'kimi-k2.6',
          name: 'Kimi K2.6',
          reasoning: false,
          input: ['text'],
          cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
          contextWindow: 256000,
          maxTokens: 8192,
        },
      ],
    },
  },
  {
    id: 'siliconflow',
    name: 'SiliconFlow (CN)',
    icon: '🌊',
    placeholder: 'sk-...',
    model: 'Multi-Model',
    requiresApiKey: true,
    defaultBaseUrl: 'https://api.siliconflow.cn/v1',
    showModelId: true,
    modelIdPlaceholder: 'deepseek-ai/DeepSeek-V3',
    defaultModelId: 'deepseek-ai/DeepSeek-V3',
    category: 'compatible',
    envVar: 'SILICONFLOW_API_KEY',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://api.siliconflow.cn/v1',
      api: 'openai-completions',
      apiKeyEnv: 'SILICONFLOW_API_KEY',
    },
  },
  {
    id: 'deepseek',
    name: 'DeepSeek',
    icon: '🐋',
    placeholder: 'sk-...',
    model: 'DeepSeek',
    requiresApiKey: true,
    defaultBaseUrl: 'https://api.deepseek.com/v1',
    showModelId: true,
    modelIdPlaceholder: 'deepseek-v4-pro',
    defaultModelId: 'deepseek-v4-pro',
    apiKeyUrl: 'https://platform.deepseek.com/api_keys',
    category: 'official',
    envVar: 'DEEPSEEK_API_KEY',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://api.deepseek.com/v1',
      api: 'openai-completions',
      apiKeyEnv: 'DEEPSEEK_API_KEY',
    },
  },
  {
    id: 'minimax-portal',
    name: 'MiniMax (Global)',
    icon: '☁️',
    placeholder: 'sk-...',
    model: 'MiniMax',
    requiresApiKey: false,
    isOAuth: true,
    supportsApiKey: true,
    defaultModelId: 'MiniMax-M2.7',
    showModelId: true,
    modelIdPlaceholder: 'MiniMax-M2.7',
    apiKeyUrl: 'https://platform.minimax.io',
    category: 'official',
    envVar: 'MINIMAX_API_KEY',
    supportedAuthModes: ['oauth_device', 'api_key'],
    defaultAuthMode: 'oauth_device',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://api.minimax.io/anthropic',
      api: 'anthropic-messages',
      apiKeyEnv: 'MINIMAX_API_KEY',
      models: [
        {
          id: 'MiniMax-M2.7',
          name: 'MiniMax M2.7',
          reasoning: false,
          input: ['text'],
          cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
          contextWindow: 204800,
          maxTokens: 131072,
        },
      ],
    },
  },
  {
    id: 'minimax-portal-cn',
    name: 'MiniMax (CN)',
    icon: '☁️',
    placeholder: 'sk-...',
    model: 'MiniMax',
    requiresApiKey: false,
    isOAuth: true,
    supportsApiKey: true,
    defaultModelId: 'MiniMax-M2.7',
    showModelId: true,
    modelIdPlaceholder: 'MiniMax-M2.7',
    apiKeyUrl: 'https://platform.minimaxi.com/',
    category: 'official',
    envVar: 'MINIMAX_CN_API_KEY',
    supportedAuthModes: ['oauth_device', 'api_key'],
    defaultAuthMode: 'oauth_device',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://api.minimaxi.com/anthropic',
      api: 'anthropic-messages',
      apiKeyEnv: 'MINIMAX_CN_API_KEY',
      models: [
        {
          id: 'MiniMax-M2.7',
          name: 'MiniMax M2.7',
          reasoning: false,
          input: ['text'],
          cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
          contextWindow: 204800,
          maxTokens: 131072,
        },
      ],
    },
  },
  {
    id: 'modelstudio',
    name: 'Model Studio',
    icon: '☁️',
    placeholder: 'sk-...',
    model: 'Qwen',
    requiresApiKey: true,
    defaultBaseUrl: 'https://coding.dashscope.aliyuncs.com/v1',
    showBaseUrl: true,
    defaultModelId: 'qwen3.6-plus',
    showModelId: true,
    showModelIdInDevModeOnly: true,
    modelIdPlaceholder: 'qwen3.6-plus',
    category: 'official',
    envVar: 'MODELSTUDIO_API_KEY',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
    providerConfig: {
      baseUrl: 'https://coding.dashscope.aliyuncs.com/v1',
      api: 'openai-completions',
      apiKeyEnv: 'MODELSTUDIO_API_KEY',
    },
  },
  {
    id: 'ollama',
    name: 'Ollama',
    icon: '🦙',
    placeholder: 'Not required',
    requiresApiKey: false,
    defaultBaseUrl: 'http://localhost:11434/v1',
    showBaseUrl: true,
    showModelId: true,
    modelIdPlaceholder: 'qwen3:latest',
    category: 'local',
    supportedAuthModes: ['local'],
    defaultAuthMode: 'local',
    supportsMultipleAccounts: true,
  },
  {
    id: 'custom',
    name: 'Custom',
    icon: '⚙️',
    placeholder: 'API key...',
    requiresApiKey: true,
    showBaseUrl: true,
    showModelId: true,
    modelIdPlaceholder: 'your-provider/model-id',
    category: 'custom',
    envVar: 'CUSTOM_API_KEY',
    supportedAuthModes: ['api_key'],
    defaultAuthMode: 'api_key',
    supportsMultipleAccounts: true,
  },
];

const PROVIDER_DEFINITION_MAP = new Map(
  PROVIDER_DEFINITIONS.map((definition) => [definition.id, definition]),
);

export function getProviderDefinition(
  type: ProviderType | string,
): ProviderDefinition | undefined {
  return PROVIDER_DEFINITION_MAP.get(type as ProviderType);
}

export function getProviderTypeInfo(
  type: ProviderType,
): ProviderTypeInfo | undefined {
  return getProviderDefinition(type);
}

export function getProviderEnvVar(type: string): string | undefined {
  return getProviderDefinition(type)?.envVar;
}

export function getProviderDefaultModel(type: string): string | undefined {
  return getProviderDefinition(type)?.defaultModelId;
}

export function getProviderBackendConfig(
  type: string,
): ProviderBackendConfig | undefined {
  return getProviderDefinition(type)?.providerConfig;
}

export function getProviderUiInfoList(): ProviderTypeInfo[] {
  return PROVIDER_DEFINITIONS;
}

export function getKeyableProviderTypes(): string[] {
  return PROVIDER_DEFINITIONS.filter((definition) => definition.envVar).map(
    (definition) => definition.id,
  );
}
