import { request } from '@/api/request'

// GoView 项目类型定义
export interface GoViewProject {
  id: string
  identity: string
  projectName: string
  state: -1 | 1 // -1 未发布, 1 已发布
  createTime: string
  updateTime?: string
  createUserId: number
  isDelete: 0 | 1
  indexImage: string
  backgroundImage: string
  remarks: string
}

export interface GoViewProjectData {
  id: string
  projectId: string
  content: string // JSON 格式字符串
  createTime: string
  createUserId: string
}

export interface GoViewFile {
  id: string
  fileName: string
  fileSize: number
  fileSuffix: string
  virtualKey: string
  relativePath: string
  absolutePath: string
  createTime: string
}

export interface OssInfo {
  BucketName: string
  bucketURL: string
}

// API 响应类型
export interface GoViewResponse<T = any> {
  code: number
  msg: string
  data: T
}

// 项目列表响应
export interface ProjectListResponse {
  list: GoViewProject[]
  total: number
}

/** 获取 OSS 信息 */
export const fetchOssInfo = () => {
  return request.get<GoViewResponse<OssInfo>>('/Visual/v1/sys/getOssInfo')
}

/** 获取项目列表 */
export const fetchProjectList = (params?: {
  page?: number
  size?: number
  state?: -1 | 1
}) => {
  return request.get<GoViewResponse<ProjectListResponse>>('/Visual/v1/project/list', { params })
}

/** 获取项目数据（公开接口，无需认证） */
export const fetchProjectData = (projectId: string) => {
  return request.get<GoViewResponse<GoViewProjectData>>('/Visual/v1/project/getData', {
    params: { projectId },
  })
}

/** 创建项目 */
export const createProject = (data: {
  projectName: string
  state: -1 | 1
  createTime?: string
  createUserId?: number
  isDelete?: 0 | 1
  indexImage?: string
  backgroundImage?: string
  remarks?: string
}) => {
  return request.post<GoViewResponse<GoViewProject>>('/Visual/v1/project/create', data)
}

/** 编辑项目 */
export const updateProject = (data: {
  identity: string
  projectName?: string
  indexImage?: string
  backgroundImage?: string
  remarks?: string
}) => {
  return request.post<GoViewResponse<GoViewProject>>('/Visual/v1/project/edit', data)
}

/** 删除项目 */
export const deleteProject = (ids: string) => {
  return request.delete<GoViewResponse<Record<string, never>>>('/Visual/v1/project/delete', {
    params: { ids },
  })
}

/** 发布/取消发布项目 */
export const publishProject = (data: { identity: string; state: -1 | 1 }) => {
  return request.put<GoViewResponse<{ identity: string; state: number }>>(
    '/Visual/v1/project/publish',
    data
  )
}

/** 上传文件 */
export const uploadFile = (file: File) => {
  const formData = new FormData()
  formData.append('object', file)
  return request.post<GoViewResponse<GoViewFile>>('/Visual/v1/project/upload', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
  })
}

/** 保存项目数据 */
export const saveProjectData = (projectId: string, content: string) => {
  const formData = new URLSearchParams()
  formData.append('projectId', projectId)
  formData.append('content', content)
  return request.post<GoViewResponse<GoViewProjectData>>('/Visual/v1/project/save/data', formData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  })
}