'use strict';

const SUPPORTED_MODEL_CATALOG = Object.freeze({
  anthropic: Object.freeze([
    {
      id: 'claude-opus-4-7',
      name: 'Claude Opus 4.7',
      capabilities: Object.freeze({ chat: true, vision: true, tools: true, streaming: true, reasoning: true, code: true }),
    },
    {
      id: 'claude-opus-4-6',
      name: 'Claude Opus 4.6',
      capabilities: Object.freeze({ chat: true, vision: true, tools: true, streaming: true, reasoning: true, code: true }),
    },
    {
      id: 'claude-sonnet-4-6',
      name: 'Claude Sonnet 4.6',
      capabilities: Object.freeze({ chat: true, vision: true, tools: true, streaming: true, reasoning: true, code: true }),
    },
    {
      id: 'claude-haiku-4-5-20251001',
      name: 'Claude Haiku 4.5',
      capabilities: Object.freeze({ chat: true, vision: true, tools: true, streaming: true, code: true }),
    },
  ]),
  openai: Object.freeze([
    {
      id: 'gpt-5.5',
      name: 'GPT-5.5',
      capabilities: Object.freeze(['chat', 'tools', 'vision', 'reasoning', 'code']),
    },
    {
      id: 'gpt-5.4',
      name: 'GPT-5.4',
      capabilities: Object.freeze(['chat', 'tools', 'vision', 'reasoning', 'code']),
    },
    {
      id: 'gpt-5.4-mini',
      name: 'GPT-5.4 Mini',
      capabilities: Object.freeze(['chat', 'tools', 'vision', 'reasoning', 'code']),
    },
    {
      id: 'gpt-5.3-codex',
      name: 'GPT-5.3 Codex',
      capabilities: Object.freeze(['chat', 'tools', 'reasoning', 'code']),
    },
  ]),
  google: Object.freeze([
    {
      id: 'gemini-2.5-pro',
      name: 'Gemini 2.5 Pro',
      capabilities: Object.freeze(['chat', 'code', 'tools', 'reasoning', 'vision']),
    },
    {
      id: 'gemini-2.5-flash',
      name: 'Gemini 2.5 Flash',
      capabilities: Object.freeze(['chat', 'code', 'tools', 'vision']),
    },
  ]),
  deepseek: Object.freeze([
    {
      id: 'deepseek-v4-pro',
      name: 'DeepSeek V4 Pro',
      capabilities: Object.freeze(['chat', 'tools', 'reasoning', 'code']),
    },
    {
      id: 'deepseek-v4-flash',
      name: 'DeepSeek V4 Flash',
      capabilities: Object.freeze(['chat', 'tools', 'code']),
    },
  ]),
  moonshot: Object.freeze([
    {
      id: 'kimi-k2.6',
      name: 'Kimi K2.6',
      capabilities: Object.freeze(['chat', 'tools', 'vision', 'reasoning', 'code', 'streaming', 'json']),
      maxContextTokens: 256000,
      costPer1mInput: 0.95,
      costPer1mOutput: 4.00,
    },
    {
      id: 'kimi-k2.5',
      name: 'Kimi K2.5',
      capabilities: Object.freeze(['chat', 'tools', 'vision', 'reasoning', 'code', 'streaming', 'json']),
      maxContextTokens: 256000,
      costPer1mInput: 0.60,
      costPer1mOutput: 3.00,
    },
    {
      id: 'kimi-k2-turbo-preview',
      name: 'Kimi K2 Turbo Preview',
      capabilities: Object.freeze(['chat', 'tools', 'reasoning', 'code', 'streaming']),
      maxContextTokens: 256000,
    },
    {
      id: 'kimi-k2-thinking',
      name: 'Kimi K2 Thinking',
      capabilities: Object.freeze(['chat', 'tools', 'reasoning', 'code', 'streaming']),
      maxContextTokens: 256000,
    },
    {
      id: 'kimi-k2-thinking-turbo',
      name: 'Kimi K2 Thinking Turbo',
      capabilities: Object.freeze(['chat', 'tools', 'reasoning', 'code', 'streaming']),
      maxContextTokens: 256000,
    },
    {
      id: 'kimi-k2-0905-preview',
      name: 'Kimi K2 0905 Preview',
      capabilities: Object.freeze(['chat', 'tools', 'reasoning', 'code', 'streaming']),
      maxContextTokens: 256000,
      costPer1mInput: 0.60,
      costPer1mOutput: 2.50,
    },
    {
      id: 'moonshot-v1-128k',
      name: 'Moonshot V1 128K',
      capabilities: Object.freeze(['chat', 'tools', 'code', 'streaming']),
      maxContextTokens: 128000,
    },
    {
      id: 'moonshot-v1-32k',
      name: 'Moonshot V1 32K',
      capabilities: Object.freeze(['chat', 'tools', 'code', 'streaming']),
      maxContextTokens: 32000,
    },
    {
      id: 'moonshot-v1-8k',
      name: 'Moonshot V1 8K',
      capabilities: Object.freeze(['chat', 'tools', 'code', 'streaming']),
      maxContextTokens: 8000,
    },
    {
      id: 'moonshot-v1-128k-vision-preview',
      name: 'Moonshot V1 128K Vision Preview',
      capabilities: Object.freeze(['chat', 'tools', 'vision', 'streaming']),
      maxContextTokens: 128000,
    },
    {
      id: 'moonshot-v1-32k-vision-preview',
      name: 'Moonshot V1 32K Vision Preview',
      capabilities: Object.freeze(['chat', 'tools', 'vision', 'streaming']),
      maxContextTokens: 32000,
    },
    {
      id: 'moonshot-v1-8k-vision-preview',
      name: 'Moonshot V1 8K Vision Preview',
      capabilities: Object.freeze(['chat', 'tools', 'vision', 'streaming']),
      maxContextTokens: 8000,
    },
  ]),
  ollama: Object.freeze([
    {
      id: 'gemma4:e4b',
      name: 'Gemma 4 E4B',
      capabilities: Object.freeze(['chat', 'code', 'tools', 'reasoning']),
    },
    {
      id: 'gemma4:26b',
      name: 'Gemma 4 26B',
      capabilities: Object.freeze(['chat', 'code', 'tools', 'reasoning']),
    },
  ]),
});

function cloneModel(model) {
  return {
    ...model,
    capabilities: Array.isArray(model.capabilities)
      ? [...model.capabilities]
      : { ...(model.capabilities || {}) },
  };
}

function supportedModelsForProvider(providerType) {
  const key = String(providerType || '').toLowerCase();
  return (SUPPORTED_MODEL_CATALOG[key] || []).map(cloneModel);
}

function supportedModelIdsForProvider(providerType) {
  return new Set(supportedModelsForProvider(providerType).map((model) => model.id));
}

function findSupportedModel(providerType, modelId) {
  const id = String(modelId || '');
  return supportedModelsForProvider(providerType).find((model) => model.id === id) || null;
}

function filterToSupportedModels(providerType, listedModels = []) {
  const liveById = new Map();
  for (const model of listedModels || []) {
    const id = model && (model.id || model.model_id || model.modelId);
    if (id && !liveById.has(id)) liveById.set(id, model);
  }

  return supportedModelsForProvider(providerType)
    .filter((model) => liveById.has(model.id))
    .map((model) => {
      const live = liveById.get(model.id) || {};
      return {
        ...model,
        maxContextTokens: live.maxContextTokens || live.max_context_tokens || model.maxContextTokens || null,
        maxOutputTokens: live.maxOutputTokens || live.max_output_tokens || model.maxOutputTokens || null,
        costPer1mInput: live.costPer1mInput ?? live.cost_per_1m_input ?? model.costPer1mInput ?? null,
        costPer1mOutput: live.costPer1mOutput ?? live.cost_per_1m_output ?? model.costPer1mOutput ?? null,
      };
    });
}

module.exports = {
  SUPPORTED_MODEL_CATALOG,
  supportedModelsForProvider,
  supportedModelIdsForProvider,
  findSupportedModel,
  filterToSupportedModels,
};
