import type { UploadFileResponse } from './types';

/**
 * 上传文件
 * @param file 文件对象
 * @returns 返回文件ID数组
 */
export async function uploadFile(file: File): Promise<UploadFileResponse[]> {
  const formData = new FormData();
  formData.append('file', file);

  const uploadApiUrl = import.meta.env.VITE_FILE_UPLOAD_API;

  const response = await fetch(`${uploadApiUrl}/prod-api/file`, {
    method: 'POST',
    body: formData,
  });

  if (!response.ok) {
    throw new Error('文件上传失败');
  }

  const result = await response.json();
  return result;
}

/**
 * 生成文件URL
 * @param fileId 文件ID
 * @returns 文件访问URL
 */
export function getFileUrl(fileId: string): string {
  return `https://ccnetcore.com/prod-api/file/${fileId}/true`;
}
