import {
  isEmbeddingModel,
  isFunctionCallingModel,
  isReasoningModel,
  isRerankModel,
  isVisionModel,
  isWebSearchModel
} from '@renderer/config/models'
import type { AdaptedApiModel, ApiModel, Model, ModelTag } from '@renderer/types'
import { objectKeys } from '@renderer/types'

/**
 * 获取模型标签的状态
 * @param models - 模型列表
 * @returns 包含各个标签布尔值的对象，表示是否存在具有该标签的模型
 */
export const getModelTags = (models: Model[]): Record<ModelTag, boolean> => {
  const result: Record<ModelTag, boolean> = {
    vision: false,
    embedding: false,
    reasoning: false,
    function_calling: false,
    web_search: false,
    rerank: false,
    free: false
  }
  const total = objectKeys(result).length
  let satisfied = 0

  for (const model of models) {
    // 如果所有标签都已满足，提前退出
    if (satisfied === total) break

    if (!result.vision && isVisionModel(model)) {
      satisfied += 1
      result.vision = true
    }
    if (!result.embedding && isEmbeddingModel(model)) {
      satisfied += 1
      result.embedding = true
    }
    if (!result.reasoning && isReasoningModel(model)) {
      satisfied += 1
      result.reasoning = true
    }
    if (!result.function_calling && isFunctionCallingModel(model)) {
      satisfied += 1
      result.function_calling = true
    }
    if (!result.web_search && isWebSearchModel(model)) {
      satisfied += 1
      result.web_search = true
    }
    if (!result.rerank && isRerankModel(model)) {
      satisfied += 1
      result.rerank = true
    }
    if (!result.free && isFreeModel(model)) {
      satisfied += 1
      result.free = true
    }
  }

  return result
}

export function isFreeModel(model: Model) {
  if (model.provider === 'cherryai') {
    return true
  }

  return (model.id + model.name).toLocaleLowerCase().includes('free')
}

export const getDuplicateModelNames = <T extends Pick<Model, 'name'>>(models: T[]): Set<string> => {
  const nameCounts = new Map<string, number>()

  for (const model of models) {
    nameCounts.set(model.name, (nameCounts.get(model.name) ?? 0) + 1)
  }

  const duplicateNames = new Set<string>()

  for (const [name, count] of nameCounts.entries()) {
    if (count > 1) {
      duplicateNames.add(name)
    }
  }

  return duplicateNames
}

export const apiModelAdapter = (model: ApiModel): AdaptedApiModel => {
  return {
    id: model.provider_model_id ?? model.id,
    provider: model.provider ?? '',
    name: model.name,
    group: '',
    origin: model
  }
}
