import request from '@/utils/request'
import type { ApiResponse, PaginatedResponse } from '@/types'

// --- 采集日志数据模型 ---
export interface CollectAnalysis {
  id: number
  analysisTime: string
  collectAddressId: number
  addressName?: string
  machineId: number
  machineName?: string
  analysisType: string
  previousProgram?: string
  currentProgram?: string
  partCountDelta?: number
  analysisSummary?: string
}

export interface CollectCycle {
  id: number
  cycleTime: string
  collectAddressId: number
  addressName?: string
  totalMachines: number
  successCount: number
  failCount: number
  hasAnomaly: number
  changeDistribution?: string
  cycleSummary?: string
}

export interface CollectRaw {
  id: number
  collectAddressId: number
  requestTime: string
  responseTime: string
  responseDuration?: number | null
  isSuccess: number
  statusCode?: number | null
  rawJson?: string
  errorMessage?: string
  createdAt: string
}

// --- 公开的 API 封装 ---
// 获取分析记录列表
export function fetchAnalysisList(params?: {
  page?: number
  pageSize?: number
  dateRange?: string[] | null
  addressId?: number
  machineId?: number
  analysisType?: string
  programName?: string
  keyword?: string
}) {
  return request.get<{ items: CollectAnalysis[]; total: number }>(
    '/admin/collect-log/analysis',
    { params }
  )
}

// 获取分析详情
export function fetchAnalysisDetail(id: number) {
  return request.get<CollectAnalysis>(`/admin/collect-log/analysis/${id}`)
}

// 根据原始日志检索分析记录
export function fetchAnalysisByRaw(rawLogId: number | string) {
  return request.get<{ items: CollectAnalysis[] }>(`/admin/collect-log/analysis/by-raw/${rawLogId}`)
}

// 获取采集周期列表
export function fetchCycleList(params?: {
  page?: number
  pageSize?: number
  dateRange?: string[] | null
  addressId?: number
  hasAnomaly?: string
}) {
  return request.get<{ items: CollectCycle[]; total: number }>(
    '/admin/collect-log/cycle',
    { params }
  )
}

// 获取原始日志列表
export function fetchRawList(params?: {
  page?: number
  pageSize?: number
  dateRange?: string[] | null
  addressId?: number
}) {
  return request.get<{ items: CollectRaw[]; total: number }>(
    '/admin/collect-log/raw',
    { params }
  )
}

// 获取原始日志详情
export function fetchRawDetail(id: number) {
  return request.get<CollectRaw>(`/admin/collect-log/raw/${id}`)
}

export default {}
