/**
 * Folders API — listing, creation, renaming, deletion, document moves.
 *
 * Consumers: stores/folders.js, FolderTreeItem.vue, AppSidebar.vue
 */

import { request, jsonRequest } from './utils.js'

export function listFolders(parentId = null) {
  const params = new URLSearchParams()
  if (parentId != null) params.set('parent_id', parentId)
  const qs = params.toString()
  return request(`/api/folders${qs ? `?${qs}` : ''}`)
}

export function createFolder(name, parentId = null) {
  return jsonRequest('/api/folders', 'POST', { name, parent_id: parentId || null })
}

export function getFolder(folderId) {
  return request(`/api/folders/${folderId}`)
}

export function renameFolder(folderId, name) {
  return jsonRequest(`/api/folders/${folderId}`, 'PATCH', { name })
}

export function deleteFolder(folderId) {
  return request(`/api/folders/${folderId}`, { method: 'DELETE' })
}

export function moveDocument(docId, folderId) {
  return jsonRequest(`/api/documents/${docId}/folder`, 'PATCH', { folder_id: folderId || null })
}
