/**
 * 文档检索 API 模块
 * 封装全文搜索、筛选选项获取、搜索建议等接口。
 */
import request from './request'
import type { SearchParams, SearchResponse } from '@/types/search'

/**
 * Full-text search for government documents
 */
/** 全文检索公文，支持关键词 + 筛选条件 + 分页 */
export function searchDocuments(params: SearchParams): Promise<SearchResponse> {
  return request
    .post<SearchResponse>('/api/ai/v1/search', params)
    .then((res) => res.data)
}

/**
 * Fetch available filter options (knowledge categories, etc.)
 */
/** 筛选面板的可选项（知识分类、文种等），由后端动态返回 */
export interface FilterOptions {
  knowledge_categories: { label: string; value: string }[]
  doc_types: { label: string; value: string }[]
  document_scene_types: { label: string; value: string }[]
  subject_words: { label: string; value: string }[]
}

export function fetchFilterOptions(): Promise<FilterOptions> {
  return request
    .get<FilterOptions>('/api/ai/v1/search/filter-options')
    .then((res) => res.data)
}

/**
 * Search suggestions / autocomplete
 */
/** 搜索建议 / 自动补全 */
export function searchSuggest(query: string, size = 10): Promise<string[]> {
  return request
    .post<{ suggestions: string[] }>('/api/ai/v1/search/suggest', { query, size })
    .then((res) => res.data.suggestions)
}

/** 搜索已入库文档（关联文档选择器专用，最小字段集） */
export function searchDocsForBinding(query: string): Promise<SearchResponse> {
  return request
    .post<SearchResponse>('/api/ai/v1/search', { query, page_size: 10 })
    .then((res) => res.data)
}
