import request from '@/utils/request'
import type {
  KnowledgeBase,
  KnowledgeBaseCreate,
  KnowledgeBaseUpdate,
  KnowledgeBaseListResponse,
  Document,
  DocumentListResponse,
  IndexJob
} from '@/types/knowledge-base'

export function listKnowledgeBases(params?: {
  kb_type?: string
  is_enabled?: boolean
}): Promise<KnowledgeBaseListResponse> {
  return request({
    url: '/admin/kb/knowledge-bases',
    method: 'get',
    params
  })
}

export function getKnowledgeBase(kbId: string): Promise<KnowledgeBase> {
  return request({
    url: `/admin/kb/knowledge-bases/${kbId}`,
    method: 'get'
  })
}

export function createKnowledgeBase(data: KnowledgeBaseCreate): Promise<KnowledgeBase> {
  return request({
    url: '/admin/kb/knowledge-bases',
    method: 'post',
    data
  })
}

export function updateKnowledgeBase(kbId: string, data: KnowledgeBaseUpdate): Promise<KnowledgeBase> {
  return request({
    url: `/admin/kb/knowledge-bases/${kbId}`,
    method: 'put',
    data
  })
}

export function deleteKnowledgeBase(kbId: string): Promise<void> {
  return request({
    url: `/admin/kb/knowledge-bases/${kbId}`,
    method: 'delete'
  })
}

export function listDocuments(params: {
  kb_id?: string
  status?: string
  page?: number
  page_size?: number
}): Promise<DocumentListResponse> {
  return request({
    url: '/admin/kb/documents',
    method: 'get',
    params
  })
}

export function getIndexJob(jobId: string): Promise<IndexJob> {
  return request({
    url: `/admin/kb/index/jobs/${jobId}`,
    method: 'get'
  })
}

export function deleteDocument(docId: string): Promise<{ success: boolean; message: string }> {
  return request({
    url: `/admin/kb/documents/${docId}`,
    method: 'delete'
  })
}

export type {
  KnowledgeBase,
  KnowledgeBaseCreate,
  KnowledgeBaseUpdate,
  KnowledgeBaseListResponse,
  Document,
  DocumentListResponse,
  IndexJob
}
