import { api } from '#/api/request.js';

// 获取LLM供应商
export async function getLlmProviderList() {
  return api.get('/api/v1/modelProvider/list');
}

export interface ModelListQuery {
  modelType?: string;
  providerId?: string;
}

export async function getModelList(params: ModelListQuery = {}) {
  return api.get('/api/v1/model/list', { params });
}

export async function getInvokeModelList() {
  return api.get('/api/v1/model/invokeList');
}

// 保存LLM
export async function saveLlm(data: string) {
  return api.post('/api/v1/model/save', data);
}

// 删除LLM
export async function deleteLlm(data: any) {
  return api.post(`/api/v1/model/remove`, data);
}

// 修改LLM
export async function updateLlm(data: any) {
  return api.post(`/api/v1/model/update`, data);
}

export async function verifyModelConfig(id: string) {
  return api.get('/api/v1/model/verifyLlmConfig', { params: { id } });
}

export type ModelCapabilitySource = 'CATALOG' | 'DEFAULT' | 'RULE';

export interface RemoteModelDescriptor {
  addable: boolean;
  added: boolean;
  capabilitySource: ModelCapabilitySource;
  displayName: string;
  family: string;
  modelId: string;
  modelType: 'chatModel' | 'embeddingModel' | 'rerankModel';
  supportImage?: boolean | null;
  supportThinking?: boolean | null;
  supportTool?: boolean | null;
  unavailableReason?: null | string;
}

export interface RemoteModelListData {
  models: RemoteModelDescriptor[];
  providerId: string;
  truncated: boolean;
}

export type RemoteModelImportStatus = 'ALREADY_EXISTS' | 'CREATED';

export interface RemoteModelImportData {
  localModelId: string;
  modelId: string;
  modelType: RemoteModelDescriptor['modelType'];
  status: RemoteModelImportStatus;
}

export async function getRemoteModels(providerId: string) {
  return api.get(`/api/v1/modelProvider/${providerId}/remoteModels`);
}

export async function importRemoteModel(providerId: string, modelId: string) {
  return api.post(`/api/v1/modelProvider/${providerId}/remoteModels/import`, {
    modelId,
  });
}

export type ModelVerificationStageStatus =
  | 'FAILED'
  | 'PARTIAL'
  | 'PASSED'
  | 'SKIPPED';

export interface ModelVerificationData {
  dimension?: number;
  effectiveHttpVersion?: string;
  message?: string;
  nonStreaming?: ModelVerificationStageStatus;
  status?: ModelVerificationStageStatus;
  streaming?: ModelVerificationStageStatus;
}

export interface ModelInvokeConfigPayload {
  id: string;
  invokeCode?: string;
  publishEnabled: boolean;
}

export async function updateModelInvokeConfig(data: ModelInvokeConfigPayload) {
  return api.post('/api/v1/model/updateInvokeConfig', data);
}

export interface BatchModelInvokePublishPayload {
  ids: string[];
  publishEnabled: boolean;
}

export async function batchUpdateInvokePublishStatus(
  data: BatchModelInvokePublishPayload,
) {
  return api.post('/api/v1/model/batchUpdateInvokePublishStatus', data);
}

// 一键添加LLM
export async function quickAddLlm(data: any) {
  return api.post(`/api/v1/model/quickAdd`, data);
}

export interface llmType {
  id: string;
  providerId?: string;
  title: string;
  modelName?: string;
  modelProvider: {
    icon: string;
    providerName: string;
    providerType: string;
  };
  llmModel: string;
  icon: string;
  description: string;
  modelType: string;
  groupName: string;
  invokeCode?: string;
  publishEnabled?: boolean;
  supportTool?: boolean;
  supportImage?: boolean;
  supportImageB64Only?: boolean;
  supportToolMessage?: boolean;
  added: boolean;
  aiLlmProvider: any;
}
