import request from '@/config/axios'

// 巡检实例响应VO
export interface InspectionInstanceRespVO {
  id: number // 实例ID
  taskId: number // 任务ID
  taskName: string // 任务名称
  deviceId: string // 设备ID
  deviceName: string // 设备名称
  windowStartTime: string // 窗口开始时间
  windowEndTime: string // 窗口结束时间
  status: 'PENDING' | 'NORMAL' | 'ABNORMAL' | 'MISSED' // 实例状态
  latestRecordId: number // 最新记录ID
  latestResultStatus: 'NORMAL' | 'ABNORMAL' // 最新结果状态
  assigneeUserId: string // 指派用户ID
  assigneeUserName: string // 指派用户名称
  createTime: string // 创建时间
}

export interface InspectionInstanceVO {
  id?: number
  taskId: number
  deviceId: string
  deviceName: string
  windowStartTime: string
  windowEndTime: string
  status: 'PENDING' | 'NORMAL' | 'ABNORMAL' | 'MISSED'
  latestRecordId?: number
  creator?: string
  createTime?: string
}

export interface InspectionInstancePageReqVO {
  pageNo: number
  pageSize: number
  taskId?: number
  deviceId?: string
  deviceName?: string
  status?: string
  dateFrom?: string
  dateTo?: string
}

export interface InspectionInstancePageRespVO {
  list: InspectionInstanceVO[]
  total: number
}

export interface RebuildInstanceReqVO {
  taskId: number
  dateFrom: string
  dateTo: string
  forceRecreate?: boolean
}

// 重建点检实例
export const rebuildInspectionInstance = (data: RebuildInstanceReqVO) => {
  return request.post({
    url: '/iot/inspection/instance/rebuild',
    params: data
  })
}

// 获取点检实例详情
export const getInspectionInstance = (id: number) => {
  return request.get({
    url: '/iot/inspection/instance/get',
    params: { id }
  })
}

// 分页查询点检实例
export const getInspectionInstancePage = (params: InspectionInstancePageReqVO) => {
  return request.get({
    url: '/iot/inspection/instance/page',
    params
  })
}

// 查询设备当前可执行的实例
export const getCurrentInstanceByDevice = (deviceId: string, submitterUserId: string) => {
  return request.get({
    url: '/iot/inspection/instance/current-by-device',
    params: { deviceId, submitterUserId }
  })
}

// 巡检实例 API
export const InspectionInstanceApi = {
  // 获得设备当前进行中的实例
  getCurrentByDevice: async (deviceId: string, submitterUserId?: string) => {
    return await request.get({
      url: `/iot/inspection/instance/current-by-device`,
      params: { deviceId, submitterUserId }
    })
  },

  // 按产品ID查询同分类下进行中的实例
  getCurrentByProductCategory: async (productId: number | string, submitterUserId?: string) => {
    return await request.get({
      url: `/iot/inspection/instance/current-by-product-category`,
      params: { productId, submitterUserId }
    })
  }
}
