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

import { request } 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 request('/api/folders', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name, parent_id: parentId || null }),
  })
}

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

export function renameFolder(folderId, name) {
  return request(`/api/folders/${folderId}`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name }),
  })
}

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

export function moveDocument(docId, folderId) {
  return request(`/api/documents/${docId}/folder`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ folder_id: folderId || null }),
  })
}
