import type { BaseResponse, PaginationParams } from './common'

export type ExportTaskScene = 'device' | 'iot_card' | 'order'

export type ExportTaskFormat = 'xlsx' | 'csv'

export enum ExportTaskStatus {
  PENDING = 1,
  PROCESSING = 2,
  COMPLETED = 3,
  FAILED = 4,
  CANCELED = 5
}

export interface ExportTaskQueryParams extends PaginationParams {
  scene?: ExportTaskScene
  status?: ExportTaskStatus
  start_time?: string
  end_time?: string
}

export interface ExportTaskItem {
  id: number
  task_id?: number
  task_no: string
  scene: ExportTaskScene
  status: ExportTaskStatus
  status_name?: string
  progress?: number
  format: ExportTaskFormat
  total_rows?: number
  processed_rows?: number
  total_shards?: number
  success_shards?: number
  failed_shards?: number
  file_key?: string
  error_message?: string
  cancel_requested?: boolean
  total_count?: number
  success_count?: number
  failed_count?: number
  error_code?: string
  error_summary?: string
  updated_at?: string
  created_at: string
  started_at?: string
  completed_at?: string
}

export interface ExportTaskListResponse {
  page: number
  size: number
  total: number
  items: ExportTaskItem[]
}

export interface CreateExportTaskRequest {
  scene: ExportTaskScene
  format: ExportTaskFormat
  query?: Record<string, unknown>
}

export interface CreateExportTaskResponse {
  task_id: number
  task_no: string
  status: ExportTaskStatus
  status_name: string
  message: string
}

export interface ExportTaskDetail extends ExportTaskItem {
  download_url?: string
  download_expires_at?: string
}

export interface CancelExportTaskResponse {
  task_id: number
  status: ExportTaskStatus
  status_name: string
  cancel_requested: boolean
  message: string
}

export type ExportTaskListApiResponse = BaseResponse<ExportTaskListResponse>
export type CreateExportTaskApiResponse = BaseResponse<CreateExportTaskResponse>
export type ExportTaskDetailApiResponse = BaseResponse<ExportTaskDetail>
export type CancelExportTaskApiResponse = BaseResponse<CancelExportTaskResponse>
