import { http } from '@/utils/http'
import { CustomRequestOptions } from '@/interceptors/request'
import request from '@/utils/request'
import * as API from '../app/types'

export interface IFooItem {
  id: string
  name: string
}

/** GET 请求 */
export const getFooAPI = (path: string, name: string) => {
  return http.get<IFooItem>(path, { name })
}
/** GET 请求；支持 传递 header 的范例 */
export const getFooAPI2 = (path: string, name: string) => {
  return http.get<IFooItem>(path, { name }, { 'Content-Type-100': '100' })
}

export interface IMessage {
  role: 'user' | 'system' | 'assistant'
  content: Array<{ type: 'text'; text: string } | { type: 'image_url'; image_url: string }>
}

export interface IGptRequestBody {
  model: string
  max_tokens: number
  temperature: number
  top_p: number
  listUuid: string
  presence_penalty: number
  frequency_penalty: number
  messages: IMessage[]
  stream: boolean
}

// GPT 响应类型（可根据你接口返回结构细化）
export interface IGptResponse {
  id: string
  object: string
  choices: any[]
}

// 👇 支持传 path 和 body 的 post 函数
export const postGptAPI = (path: string, body: IGptRequestBody, headers?: Record<string, any>) => {
  return http.post<IGptResponse>(path, body)
}
// export const postUpload = (path: string, form: FormData, query?: Record<string, any>, headers?: Record<string, any>) => {
//   return http.post<IFooItem>(path, form,query,headers)
// }

// 1. 定义一个新的扁平化接口
export type UploadMultiForm = {
  /** 文件字段，key 为 k1…k10，可传单个 File 或 File[] */
  [K in `k${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10}`]?: File | File[]
} & {
  source: string // 画作来源
  mask: string // 画家 uid 或 用户 id
  type: string // 资源类型：image / file
}

// 2. 改造 uploadMulti，让它直接把所有字段扁平化地 append 到 FormData
export async function uploadMulti(form: UploadMultiForm, options?: CustomRequestOptions) {
  const formData = new FormData()

  Object.entries(form).forEach(([key, val]) => {
    if (val instanceof File) {
      // 单个文件
      formData.append(key, val)
    } else if (Array.isArray(val)) {
      // 文件数组
      val.forEach((file) => formData.append(key, file))
    } else {
      // 普通文本字段
      formData.append(key, String(val))
    }
  })

  return request<API.ApiResponse>('/upload/multi', {
    method: 'POST',
    headers: {
      // H5/浏览器 会自动带 boundary
      'Content-Type': 'multipart/form-data',
    },
    data: formData,
    ...(options || {}),
  })
}

// export interface UploadMultiBody {
//   source: string // 画作来源
//   mask: string // 画家 uid 或 用户 id
//   detail: string // 详情描述
//   type: string // 资源类型：image / file
// }

// export interface UploadMultiParams {
//   files?: File[] // 最多取前 3 个文件，分别对应 k1、k2、k3
// }

// /**
//  * 上传多文件到 /upload/multi
//  */
// export async function uploadMulti({
//   params,
//   body,
//   options,
// }: {
//   /** 文件和文本字段 */
//   params: UploadMultiParams
//   body: UploadMultiBody
//   /** uni.request / uni.uploadFile 的可选配置 */
//   options?: CustomRequestOptions
// }) {
//   // 1. 构造 FormData
//   const formData = new FormData()
//   ;(params.files || []).slice(0, 10).forEach((file, idx) => {
//     formData.append(`k${idx + 1}`, file)
//   })
//   formData.append('source', body.source)
//   formData.append('mask', body.mask)
//   formData.append('detail', body.detail)
//   formData.append('type', body.type)

//   // 2. 发起请求
//   return request<API.ApiResponse>('/upload/multi', {
//     method: 'POST',
//     headers: {
//       // 通常不需要手动写 Content-Type，浏览器/uniapp 会自动带 boundary
//       'Content-Type': 'multipart/form-data',
//     },
//     data: formData,
//     ...(options || {}),
//   })
// }

/** POST 请求 */
export const postFooAPI = (path: string, body: Record<string, any>) => {
  return http.post<IFooItem>(path, body)
}
/** POST 请求；需要传递 query 参数的范例；微信小程序经常有同时需要query参数和body参数的场景 */
export const postFooAPI2 = (
  path: string,
  body: Record<string, any>,
  query?: Record<string, any>,
) => {
  return http.post<IFooItem>(path, body, query)
}

/** POST 请求；支持 传递 header 的范例 */
export const postFooAPI3 = (
  path: string,
  body: Record<string, any>,
  query?: Record<string, any>,
  headers?: Record<string, any>,
) => {
  return http.post<IFooItem>(path, body, query, headers)
}
