import request from '@/config/axios'

export interface PhotoVO {
  url: string
  desc?: string
}

export interface InspectionRecordVO {
  id?: number
  taskId?: number
  taskName?: string
  instanceId: number
  deviceId?: string
  deviceName?: string
  submitterUserId: string
  submitterUserName?: string
  submitTime?: string
  resultStatus: 'NORMAL' | 'ABNORMAL'
  resultSummary?: string
  abnormalDesc?: string
  photos?: PhotoVO[]
  createTime?: string
}

export interface InspectionRecordPageReqVO {
  pageNo: string
  pageSize: string
  taskId?: string
  instanceId?: string
  submitterUserId?: string
  dateFrom?: string
  dateTo?: string
}

export interface InspectionRecordPageRespVO {
  list: InspectionRecordVO[]
  total: number
}

// 提交点检记录
export const createInspectionRecord = (data: InspectionRecordVO) => {
  return request.post({
    url: '/iot/inspection/record/create',
    data
  })
}

// 分页查询点检记录
export const getInspectionRecordPage = (params: InspectionRecordPageReqVO) => {
  return request.get({
    url: '/iot/inspection/record/page',
    params
  })
}

// 获取点检记录详情
export const getInspectionRecord = (id: number) => {
  return request.get({
    url: '/iot/inspection/record/get',
    params: { id }
  })
}

// 获取实例下的点检记录列表
export const getInspectionRecordList = (instanceId: number) => {
  return request.get({
    url: '/iot/inspection/record/list',
    params: { instanceId }
  })
}

// 点检记录创建请求VO
export interface InspectionRecordCreateReqVO {
  instanceId: number // 实例ID
  resultStatus: 'NORMAL' | 'ABNORMAL' // 结果状态
  resultSummary?: string // 结果摘要
  abnormalDesc?: string // 异常描述
  photos?: PhotoVO[] // 照片列表
  submitterUserId: string // 提交人ID
  submitterUserName?: string // 提交人姓名
}

// 点检记录 API
export const InspectionRecordApi = {
  // 创建点检记录
  createInspectionRecord: async (data: InspectionRecordCreateReqVO) => {
    return await request.post({
      url: `/iot/inspection/record/create`,
      data
    })
  },

  // 批量创建点检记录（混合 NORMAL/ABNORMAL）
  createInspectionRecordBatchMixed: async (data: {
    items: Array<{
      instanceId: number
      resultStatus: 'NORMAL' | 'ABNORMAL'
      resultSummary: string
    }>
    submitterUserId: string
    submitterUserName?: string
  }) => {
    return await request.post({
      url: `/iot/inspection/record/create-batch-mixed`,
      data
    })
  }
}
