import api from '@/api/client'
import type { TreeMarkdownValidation } from '@/types'

export const treeMarkdownApi = {
  /** Export a tree's JSONB structure as ResolutionFlow markdown */
  exportMarkdown: async (treeId: string): Promise<{ markdown: string }> => {
    const response = await api.get(`/trees/${treeId}/export-markdown`)
    return response.data
  },

  /** Parse markdown and update a tree's JSONB structure */
  importMarkdown: async (treeId: string, markdown: string): Promise<TreeMarkdownValidation> => {
    const response = await api.put(`/trees/${treeId}/import-markdown`, { markdown })
    return response.data
  },

  /** Validate markdown without saving */
  validateMarkdown: async (markdown: string): Promise<TreeMarkdownValidation> => {
    const response = await api.post('/trees/validate-markdown', { markdown })
    return response.data
  },
}
