import { get, put, post } from './http'
import type { Node, Edge } from '@vue-flow/core'

// 画布快照：与 Vue Flow toObject() 产物对齐
export interface FlowSnapshot {
  nodes: Node[]
  edges: Edge[]
  viewport?: { x: number; y: number; zoom: number }
  position?: [number, number]
  zoom?: number
}

// 后端返回结构（按需调整字段名以匹配你的接口）
export interface FlowRecord extends FlowSnapshot {
  id: string
  title?: string
  updatedAt?: string
}

export interface UploadResult {
  url: string
  [key: string]: any
}

// 读取画布
export function getFlow(projectId: string) {
  return get<FlowRecord>(`/flow/${projectId}`)
}

// 保存画布（整图覆盖写）
export function saveFlow(projectId: string, data: FlowSnapshot) {
  return put<FlowRecord>(`/flow/${projectId}`, data)
}

// 上传媒体文件，拿到可持久访问的 URL
export function uploadMedia(file: File) {
  const form = new FormData()
  form.append('file', file)
  return post<UploadResult>('/upload', form, {
    headers: { 'Content-Type': 'multipart/form-data' },
    // 上传可能较慢，放宽超时
    timeout: 60000,
  })
}
