import http from '@/utils/http'

// 缺陷检测请求参数
export interface DefectDetectionRequest {
  confThreshold: number
  defectTypeList: string[]
  imageId: string
  modelId: string
}

// 手动添加缺陷请求参数
export interface ManualDefectAddRequest {
  attachId: string
  axial: number
  chordwise: number
  defectCode: string
  defectLevel: string
  defectName: string
  defectPosition: string
  defectType: string
  description: string
  detectionDate: string
  labelInfo: string
  markInfo: {
    bbox: number[]
    clsId: number
    confidence: number
    label: string
  }
  repairIdea: string
  repairStatus: string
  source: string
}

// 缺陷检测结果
export interface DefectDetectionResult {
  attachId: string
  attachPath: string
  axial: number
  chordwise: number
  defectCode: string
  defectId: string
  defectLevel: string
  defectLevelLabel: string
  defectName: string
  defectPosition: string
  defectType: string
  defectTypeLabel: string
  description: string
  detectionDate: string
  imageId: string
  labelInfo: string
  markInfo: {
    bbox: number[]
    clsId: number
    confidence: number
    label: string
  }
  repairIdea: string
  repairStatus: string
  repairStatusLabel: string
  source: string
  sourceLabel: string
}

// API响应结构
export interface DefectDetectionResponse {
  code: number
  data: DefectDetectionResult[]
  msg: string
  status: number
  success: boolean
}

// 手动添加缺陷响应结构
export interface ManualDefectAddResponse {
  code: number
  data: DefectDetectionResult
  msg: string
  status: number
  success: boolean
}

/** @desc 单图自动标注缺陷 */
export const detectDefects = (params: DefectDetectionRequest) => {
  return http.post<DefectDetectionResponse>('/defect/detect', params)
}

/** @desc 手动添加缺陷记录 */
export const addManualDefect = (params: ManualDefectAddRequest,imageId:string) => {
  return http.post<ManualDefectAddResponse>(`/defect/${imageId}`, params)
}

// 缺陷相关的其他接口

// 缺陷列表查询参数接口
export interface DefectListParams {
  defectId?: string;
  defectLevel?: string;
  defectType?: string;
  keyword?: string;
  turbineId?: string;
  imageId?: string; // 添加imageId参数，用于按图像筛选缺陷
}

/** @desc 获取缺陷列表 */
export const getDefectList = (params: DefectListParams) => {
  return http.get<{
    code: number
    data: DefectDetectionResult[]
    msg: string
    status: number
    success: boolean
  }>('/defect/list',  params )
}

/** @desc 添加缺陷 */
export const addDefect = (params: Partial<DefectDetectionResult>) => {
  return http.post<{
    code: number
    data: DefectDetectionResult
    msg: string
    status: number
    success: boolean
  }>('/defect/add', params)
}

/** @desc 更新缺陷 */
export const updateDefect = (defectId: string, params: Partial<DefectDetectionResult>) => {
  return http.put<{
    code: number
    data: DefectDetectionResult
    msg: string
    status: number
    success: boolean
  }>(`/defect/${defectId}`, params)
}

/** @desc 删除缺陷 */
export const deleteDefect = (defectId: string) => {
  return http.del<{
    code: number
    msg: string
    status: number
    success: boolean
  }>(`/defect/${defectId}`)
}

/** @desc 上传标注后的图片作为附件 */
export const uploadAnnotatedImage = (imageBlob: Blob, fileName: string) => {
  const formData = new FormData()
  formData.append('file', imageBlob, fileName)
  
  return http.post<{
    code: number
    data: AttachInfoData
    msg: string
    status: number
    success: boolean
  }>('/attach-info/defect_mark_pic', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 附件信息数据类型
export interface AttachInfoData {
  attachId: string
  attachPath: string
  fileName: string
  fileSize: number
  contentType: string
  uploadTime: string
  [key: string]: any
}

// 缺陷信息接口
export interface DefectInfo {
  id: string;
  defectId?: string;
  defectName?: string;
  defectLevel?: string;
  defectType?: string;
  defectPosition?: string;
  detectionDate?: string;
  description?: string;
  repairStatus?: string;
  repairIdea?: string;
  labelInfo?: string;
  markInfo?: {
    bbox?: number[];
    clsId?: number;
    confidence?: number;
    label?: string;
    [key: string]: any;
  };
  [key: string]: any;
}

// 缺陷等级类型
export interface DefectLevelType {
  code: string;
  name: string;
  value: string;
  sort: number;
  description?: string;
}

// 缺陷类型
export interface DefectType {
  code: string;
  name: string;
  value: string;
  sort: number;
  description?: string;
}

// 获取缺陷等级列表
export const getDefectLevels = () => {
  return http.get<{
    code: number;
    data: DefectLevelType[];
    msg: string;
    status: number;
    success: boolean;
  }>('/common/list/defect-level')
}

// 获取缺陷类型列表
export const getDefectTypes = () => {
  return http.get<{
    code: number;
    data: DefectType[];
    msg: string;
    status: number;
    success: boolean;
  }>('/common/list/defect-type')
} 