import type { AixAPI_Access } from '~/modules/aix/server/api/aix.wiretypes';

import { ModelVendorAlibaba } from './alibaba/alibaba.vendor';
import { ModelVendorAnthropic } from './anthropic/anthropic.vendor';
import { ModelVendorAzure } from './azure/azure.vendor';
import { ModelVendorBedrock } from './bedrock/bedrock.vendor';
import { ModelVendorDeepseek } from './deepseek/deepseekai.vendor';
import { ModelVendorGemini } from './gemini/gemini.vendor';
import { ModelVendorGroq } from './groq/groq.vendor';
import { ModelVendorLMStudio } from './lmstudio/lmstudio.vendor';
import { ModelVendorLocalAI } from './localai/localai.vendor';
import { ModelVendorMistral } from './mistral/mistral.vendor';
import { ModelVendorMoonshot } from './moonshot/moonshot.vendor';
import { ModelVendorOllama } from './ollama/ollama.vendor';
import { ModelVendorOpenAI } from './openai/openai.vendor';
import { ModelVendorOpenPipe } from './openpipe/openpipe.vendor';
import { ModelVendorOpenRouter } from './openrouter/openrouter.vendor';
import { ModelVendorPerplexity } from './perplexity/perplexity.vendor';
import { ModelVendorTogetherAI } from './togetherai/togetherai.vendor';
import { ModelVendorXAI } from './xai/xai.vendor';
import { ModelVendorZAI } from './zai/zai.vendor';

import type { IModelVendor } from './IModelVendor';


export type ModelVendorId =
  | 'alibaba'
  | 'anthropic'
  | 'azure'
  | 'bedrock'
  | 'deepseek'
  | 'googleai'
  | 'groq'
  | 'lmstudio'
  | 'localai'
  | 'mistral'
  | 'moonshot'
  | 'ollama'
  | 'openai'
  | 'openpipe'
  | 'openrouter'
  | 'perplexity'
  | 'togetherai'
  | 'xai'
  | 'zai'
  ;

/** Global: Vendor Instances Registry **/
const MODEL_VENDOR_REGISTRY: Record<ModelVendorId, IModelVendor> = {
  alibaba: ModelVendorAlibaba,
  anthropic: ModelVendorAnthropic,
  azure: ModelVendorAzure,
  bedrock: ModelVendorBedrock,
  deepseek: ModelVendorDeepseek,
  googleai: ModelVendorGemini,
  groq: ModelVendorGroq,
  lmstudio: ModelVendorLMStudio,
  localai: ModelVendorLocalAI,
  mistral: ModelVendorMistral,
  moonshot: ModelVendorMoonshot,
  ollama: ModelVendorOllama,
  openai: ModelVendorOpenAI,
  openpipe: ModelVendorOpenPipe,
  openrouter: ModelVendorOpenRouter,
  perplexity: ModelVendorPerplexity,
  togetherai: ModelVendorTogetherAI,
  xai: ModelVendorXAI,
  zai: ModelVendorZAI,
} as Record<string, IModelVendor>;


export function findAllModelVendors(): IModelVendor[] {
  const modelVendors = Object.values(MODEL_VENDOR_REGISTRY);
  modelVendors.sort((a, b) => a.displayRank - b.displayRank);
  return modelVendors;
}

export function findModelVendor<TServiceSettings extends object = {}, TAccess = AixAPI_Access>(
  vendorId?: ModelVendorId,
): IModelVendor<TServiceSettings, TAccess> | null {
  return vendorId ? (MODEL_VENDOR_REGISTRY[vendorId] as IModelVendor<TServiceSettings, TAccess>) ?? null : null;
}

// export function getDefaultModelVendor(): IModelVendor {
//   return MODEL_VENDOR_REGISTRY.openai;
// }