export type MetadataFieldType = 'string' | 'number' | 'boolean' | 'enum' | 'array_enum'
export type MetadataFieldStatus = 'draft' | 'active' | 'deprecated'
export type MetadataScope = 'kb_document' | 'intent_rule' | 'script_flow' | 'prompt_template'
export type FieldRole = 'resource_filter' | 'slot' | 'prompt_var' | 'routing_signal'

export interface MetadataFieldDefinition {
  id: string
  field_key: string
  label: string
  type: MetadataFieldType
  description?: string
  required: boolean
  options?: string[]
  default?: string | number | boolean
  scope: MetadataScope[]
  is_filterable: boolean
  is_rank_feature: boolean
  usage_description?: string
  field_roles: FieldRole[]
  status: MetadataFieldStatus
  created_at?: string
  updated_at?: string
}

export interface MetadataFieldCreateRequest {
  field_key: string
  label: string
  type: MetadataFieldType
  required: boolean
  options?: string[]
  default?: string | number | boolean
  scope: MetadataScope[]
  is_filterable?: boolean
  is_rank_feature?: boolean
  usage_description?: string
  field_roles?: FieldRole[]
  status: MetadataFieldStatus
}

export interface MetadataFieldUpdateRequest {
  label?: string
  required?: boolean
  options?: string[]
  default?: string | number | boolean
  scope?: MetadataScope[]
  is_filterable?: boolean
  is_rank_feature?: boolean
  usage_description?: string
  field_roles?: FieldRole[]
  status?: MetadataFieldStatus
}

export interface MetadataFieldListResponse {
  items: MetadataFieldDefinition[]
}

export interface MetadataPayload {
  [key: string]: string | number | boolean | string[] | undefined
}

export interface TypeChangeConflict {
  field_key: string
  label: string
  conflict_type: 'removed' | 'type_mismatch'
  old_value?: string | number | boolean | string[]
  suggested_action: 'remove' | 'map'
  map_to?: string
}

export interface TypeChangeResult {
  preserved: { field_key: string; value: string | number | boolean | string[] | undefined }[]
  conflicts: TypeChangeConflict[]
}

export const METADATA_STATUS_OPTIONS = [
  { value: 'draft', label: '草稿', color: 'info', description: '字段可编辑，不可用于新建对象' },
  { value: 'active', label: '生效', color: 'success', description: '可用于新建与编辑对象' },
  { value: 'deprecated', label: '废弃', color: 'danger', description: '不可用于新建，历史数据可读' }
]

export const METADATA_SCOPE_OPTIONS = [
  { value: 'kb_document', label: '知识库文档' },
  { value: 'intent_rule', label: '意图规则' },
  { value: 'script_flow', label: '话术流程' },
  { value: 'prompt_template', label: 'Prompt模板' }
]

export const METADATA_TYPE_OPTIONS = [
  { value: 'string', label: '文本' },
  { value: 'number', label: '数字' },
  { value: 'boolean', label: '布尔值' },
  { value: 'enum', label: '单选枚举' },
  { value: 'array_enum', label: '多选枚举' }
]

export const STATUS_TAG_MAP: Record<MetadataFieldStatus, '' | 'success' | 'warning' | 'danger' | 'info'> = {
  draft: 'info',
  active: 'success',
  deprecated: 'danger'
}

export const FIELD_ROLE_OPTIONS: { value: FieldRole; label: string; description: string }[] = [
  { value: 'resource_filter', label: '资源过滤', description: '用于 KB 文档检索时的元数据过滤' },
  { value: 'slot', label: '运行时槽位', description: '对话流程中的结构化槽位，用于信息收集' },
  { value: 'prompt_var', label: '提示词变量', description: '注入到 LLM Prompt 中的变量' },
  { value: 'routing_signal', label: '路由信号', description: '用于意图路由和风险判断的信号' }
]

export const FIELD_ROLE_TAG_MAP: Record<FieldRole, '' | 'success' | 'warning' | 'danger' | 'info'> = {
  resource_filter: '',
  slot: 'success',
  prompt_var: 'warning',
  routing_signal: 'danger'
}
