import type {
  AgentKnowledgeDatasetConfig,
  AgentSoulAppFeaturesConfig,
  AgentSoulModelConfig,
  ToolProviderType,
} from '@dify/contracts/api/console/agent/types.gen'
import type { FileTreeIconType } from '@langgenius/dify-ui/file-tree'
import type { DefaultModel } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { ToolDefaultValue } from '@/app/components/workflow/block-selector/types'
import type {
  MetadataFilteringConditions,
  MetadataFilteringModeEnum,
  MultipleRetrievalConfig,
  SingleRetrievalConfig,
} from '@/app/components/workflow/nodes/knowledge-retrieval/types'
import type { ModelConfig } from '@/app/components/workflow/types'
import type { DataSet } from '@/models/datasets'
import type { RETRIEVE_TYPE } from '@/types/app'
import type { I18nKeysWithPrefix } from '@/types/i18n'

export type EnvScope = 'secret' | 'plain'

export type AgentComposerModel = DefaultModel & {
  model_settings?: AgentSoulModelConfig['model_settings']
}

export type EnvVariable = {
  id: string
  key: string
  value: string
  scope: EnvScope
  masked?: boolean
}

export type AgentSkill = {
  description?: string
  fileId?: string
  hash?: string
  id: string
  isMissing?: boolean
  mimeType?: string
  name: string
  size?: number
  skillMdKey?: string
}

export type AgentFileNode = {
  driveKey?: string
  hash?: string
  id: string
  icon: FileTreeIconType
  isMissing?: boolean
  fileId?: string
  configName?: string
  children?: AgentFileNode[]
  virtualContent?: string
  mimeType?: string
  name: string
  size?: number
}

export type AgentKnowledgeRetrievalItem = {
  id: string
  name?: string
  description?: string
  nameKey?: I18nKeysWithPrefix<'agentV2', 'agentDetail.configure.knowledgeRetrieval.'>
  queryMode?: 'agent' | 'custom'
  customQuery?: string
  datasetRefs?: AgentKnowledgeDatasetConfig[]
  selectedDatasets?: DataSet[]
  retrievalMode?: RETRIEVE_TYPE
  multipleRetrievalConfig?: MultipleRetrievalConfig
  singleRetrievalConfig?: SingleRetrievalConfig
  metadataFilterMode?: MetadataFilteringModeEnum
  metadataFilteringConditions?: MetadataFilteringConditions
  metadataModelConfig?: ModelConfig
}

type AgentToolBase = {
  id: string
  name: string
}

export type AgentToolAction = {
  id: string
  name: string
  toolName: string
  description: string
}

type AgentProviderToolCredentialType = 'api-key' | 'oauth2' | 'unauthorized'

export type AgentProviderTool = AgentToolBase & {
  kind: 'provider'
  displayName?: string
  pluginId?: string
  pluginUniqueIdentifier?: string
  iconClassName: string
  icon?: ToolDefaultValue['provider_icon']
  iconDark?: ToolDefaultValue['provider_icon_dark']
  providerType: ToolProviderType
  allowDelete?: boolean
  credentialId?: string
  credentialKey?: I18nKeysWithPrefix<'agentV2', 'agentDetail.configure.tools.'>
  credentialType?: AgentProviderToolCredentialType
  credentialVariant: 'authorized' | 'unauthorized' | 'none'
  actions: AgentToolAction[]
}

export type AgentCliTool = AgentToolBase & {
  kind: 'cli'
  action?: 'preAuthorize'
  installCommand?: string
  envVariables?: EnvVariable[]
}

export type AgentTool = AgentProviderTool | AgentCliTool

export type AgentSoulConfigFormState = {
  configNote: string
  prompt: string
  model?: AgentComposerModel
  appFeatures?: AgentSoulAppFeaturesConfig
  skills: AgentSkill[]
  files: AgentFileNode[]
  tools: AgentTool[]
  knowledgeRetrievals: AgentKnowledgeRetrievalItem[]
  envVariables: EnvVariable[]
  toolSettings: Record<string, Record<string, unknown>>
}

export const defaultAgentSoulConfigFormState: AgentSoulConfigFormState = {
  configNote: '',
  prompt: '',
  skills: [],
  files: [],
  tools: [],
  knowledgeRetrievals: [],
  envVariables: [],
  toolSettings: {},
}
