import apiClient from '../client'
import type {
  DocumentListResponse,
  DocumentDetailResponse,
  DocumentItem,
  UploadDocumentResponse,
  DocumentCategoriesResponse,
} from '../types'

export const documentsApi = {
  list: async (params?: {
    status?: string
    category?: string
    limit?: number
    offset?: number
  }): Promise<DocumentListResponse> => {
    const { data } = await apiClient.get('/api/v1/admin/documents', { params })
    return data
  },

  getCategories: async (): Promise<DocumentCategoriesResponse> => {
    const { data } = await apiClient.get('/api/v1/admin/documents/categories')
    return data
  },

  getDetail: async (documentId: string): Promise<DocumentDetailResponse> => {
    const { data } = await apiClient.get(`/api/v1/admin/documents/${documentId}`)
    return data
  },

  upload: async (
    file: File,
    options?: { groupKey?: string; category?: string }
  ): Promise<UploadDocumentResponse> => {
    const formData = new FormData()
    formData.append('file', file)

    const params: Record<string, string> = {}
    if (options?.groupKey) {
      params.group_key = options.groupKey
    }
    if (options?.category) {
      params.category = options.category
    }

    const { data } = await apiClient.post('/api/v1/admin/documents', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
      params,
    })
    return data
  },

  batchUpload: async (
    files: File[],
    csvFile?: File
  ): Promise<{ batch_id: string; message: string; documents_created: number }> => {
    const formData = new FormData()

    files.forEach((file) => {
      formData.append('files', file)
    })

    if (csvFile) {
      formData.append('csv_file', csvFile)
    }

    const { data } = await apiClient.post('/api/v1/admin/batch/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    })
    return data
  },

  delete: async (documentId: string): Promise<void> => {
    await apiClient.delete(`/api/v1/admin/documents/${documentId}`)
  },

  updateStatus: async (
    documentId: string,
    status: string
  ): Promise<DocumentItem> => {
    const { data } = await apiClient.patch(
      `/api/v1/admin/documents/${documentId}/status`,
      null,
      { params: { status } }
    )
    return data
  },

  triggerAutoLabel: async (documentId: string): Promise<{ message: string }> => {
    const { data } = await apiClient.post(
      `/api/v1/admin/documents/${documentId}/auto-label`
    )
    return data
  },

  updateGroupKey: async (
    documentId: string,
    groupKey: string | null
  ): Promise<{ status: string; document_id: string; group_key: string | null; message: string }> => {
    const { data } = await apiClient.patch(
      `/api/v1/admin/documents/${documentId}/group-key`,
      null,
      { params: { group_key: groupKey } }
    )
    return data
  },

  updateCategory: async (
    documentId: string,
    category: string
  ): Promise<{ status: string; document_id: string; category: string; message: string }> => {
    const { data } = await apiClient.patch(
      `/api/v1/admin/documents/${documentId}/category`,
      { category }
    )
    return data
  },
}
