/**
 * Notebook 模块 API 客户端
 */

import request from './request'
import type {
  Notebook,
  NotebookConfig,
  NotebookSource,
  NotebookMessage,
  NotebookOutput,
} from '@/types/notebook'

const BASE = '/api/v1/notebook'

// ── Notebook CRUD ──────────────────────────────────────────

export async function createNotebook(title: string, description = ''): Promise<{ id: string; title: string }> {
  const { data } = await request.post(BASE, { title, description })
  return data
}

export interface NotebookListParams {
  q?: string
  status?: string
  limit?: number
  offset?: number
}

export async function listNotebooks(params?: NotebookListParams): Promise<{ items: Notebook[]; total: number }> {
  const { data } = await request.get(BASE, { params })
  return data
}

export async function getNotebook(id: string): Promise<Notebook> {
  const { data } = await request.get(`${BASE}/${id}`)
  return data
}

export async function updateNotebook(id: string, body: {
  title?: string
  description?: string
  config?: NotebookConfig
}): Promise<void> {
  await request.put(`${BASE}/${id}`, body)
}

export async function deleteNotebook(id: string): Promise<void> {
  await request.delete(`${BASE}/${id}`)
}

export async function archiveNotebook(id: string): Promise<void> {
  await request.post(`${BASE}/${id}/archive`)
}

// ── Sources ──────────────────────────────────────────────────

export async function listSources(notebookId: string): Promise<{ items: NotebookSource[]; total: number; max_sources: number }> {
  const { data } = await request.get(`${BASE}/${notebookId}/sources`)
  return data
}

export async function addSourceBySearch(notebookId: string, docId: string, title = ''): Promise<{ id: string; doc_id: string }> {
  const { data } = await request.post(`${BASE}/${notebookId}/sources/search`, { doc_id: docId, title })
  return data
}

export async function addSourceByUpload(notebookId: string, file: File): Promise<{ id: string; doc_id: string; task_id: string }> {
  const formData = new FormData()
  formData.append('file', file)
  const { data } = await request.post(`${BASE}/${notebookId}/sources/upload`, formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
  })
  return data
}

export async function addSourceByPaste(notebookId: string, title: string, content: string): Promise<{ id: string; doc_id: string; task_id: string }> {
  const { data } = await request.post(`${BASE}/${notebookId}/sources/paste`, { title, content })
  return data
}

export async function updateSource(notebookId: string, sourceId: string, body: {
  selected?: boolean
  title?: string
}): Promise<void> {
  await request.put(`${BASE}/${notebookId}/sources/${sourceId}`, body)
}

export async function deleteSource(notebookId: string, sourceId: string): Promise<void> {
  await request.delete(`${BASE}/${notebookId}/sources/${sourceId}`)
}

export async function retrySourceIngest(notebookId: string, sourceId: string): Promise<{ task_id: string }> {
  const { data } = await request.post(`${BASE}/${notebookId}/sources/${sourceId}/retry`)
  return data
}

// ── Chat ─────────────────────────────────────────────────────

export function buildChatUrl(notebookId: string): string {
  return `${BASE}/${notebookId}/chat`
}

export async function getChatHistory(notebookId: string, sessionId = 'default', limit = 100): Promise<{
  messages: NotebookMessage[]
  total: number
}> {
  const { data } = await request.get(`${BASE}/${notebookId}/chat/history`, {
    params: { session_id: sessionId, limit },
  })
  return data
}

export async function getSuggestions(notebookId: string): Promise<{ suggestions: string[] }> {
  const { data } = await request.post(`${BASE}/${notebookId}/chat/suggest`)
  return data
}

export async function updateChatConfig(notebookId: string, config: NotebookConfig): Promise<void> {
  await request.put(`${BASE}/${notebookId}/chat/config`, { config })
}

// ── Outputs ──────────────────────────────────────────────────

export function buildOutputUrl(notebookId: string): string {
  return `${BASE}/${notebookId}/outputs`
}

export async function listOutputs(notebookId: string): Promise<{ items: NotebookOutput[]; total: number }> {
  const { data } = await request.get(`${BASE}/${notebookId}/outputs`)
  return data
}

export async function getOutput(notebookId: string, outputId: string): Promise<NotebookOutput> {
  const { data } = await request.get(`${BASE}/${notebookId}/outputs/${outputId}`)
  return data
}

export async function deleteOutput(notebookId: string, outputId: string): Promise<void> {
  await request.delete(`${BASE}/${notebookId}/outputs/${outputId}`)
}
