/** 数据处理模块的前后端契约。API 字段统一使用 snake_case。 */

export type DataProcessStatus = 'pending' | 'running' | 'completed' | 'failed' | 'stopped'
export type DataProcessType = 'structured' | 'unstructured' | 'external'
export type DataProcessResultStatus = 'valid' | 'modified' | 'invalid'
export type DataProcessSplit = 'train' | 'validation' | 'test'
export type DataProcessOutputType = 'standard' | 'reasoning'
export type DataProcessReasoningDetail = 'normal' | 'detailed'

export interface DataProcessPage<T> {
  items: T[]
  total: number
  page: number
  page_size: number
}

export interface DataProcessDatasetSplit {
  train: number
  validation: number
  test: number
}

export type DataProcessConfig = Record<string, unknown> & {
  dataset_split?: DataProcessDatasetSplit
  output_type?: DataProcessOutputType
  reasoning_detail?: DataProcessReasoningDetail
}

export interface DataProcessTask {
  id: string | number
  name: string
  description?: string
  status: DataProcessStatus
  process_type: DataProcessType
  config?: DataProcessConfig
  progress?: number
  source_dataset_id?: string | number | null
  source_dataset_name?: string | null
  source_dataset?: string | null
  output_dataset_id?: string | number | null
  output_dataset_name?: string | null
  output_dataset?: string | null
  output_datasets?: DataProcessOutputDataset[]
  source_file_count?: number
  preview_count?: number
  input_count?: number
  output_count?: number
  filtered_count?: number
  duplicate_count?: number
  error_count?: number
  creator_name?: string | null
  creator?: string | null
  created_by?: string | number | null
  create_time?: string
  created_at?: string
  start_time?: string | null
  started_at?: string | null
  complete_time?: string | null
  completed_at?: string | null
  updated_at: string
  duration?: string | null
  duration_seconds?: number | null
  failure_reason?: string | null
  source_files?: DataProcessSourceFile[]
}

export interface DataProcessOutputDataset {
  id: string | number
  name: string
  type: 'train' | 'val' | 'test' | string
  count?: number
  dataset_split?: DataProcessSplit
}

export interface DataProcessTaskCreatePayload {
  name: string
  description?: string
  process_type: DataProcessType
  config: DataProcessConfig
}

export interface DataProcessRegeneratePayload extends DataProcessTaskCreatePayload {
  expected_updated_at: string
}

export interface DataProcessRegenerateResult {
  task: DataProcessTask
  preview_invalidated: boolean
  published_outputs_preserved: boolean
}

export type DataProcessTaskUpdatePayload = Partial<DataProcessTaskCreatePayload>

export interface DataProcessSourceFile {
  id: string | number
  task_id?: string | number
  name: string
  size_bytes: number
  record_count: number
  file_format?: string
  checksum_sha256?: string
  status?: string
  create_time?: string
}

export interface DataProcessSourceContent {
  file_id?: string | number
  file?: DataProcessSourceFile
  content: string
  start_line?: number
  end_line?: number
  line_count?: number
  total_lines?: number
  has_more?: boolean
  truncated?: boolean
  offset?: number
  limit?: number
  total_chars?: number
}

export interface DataProcessPdfPageRange {
  page_number: number
  source_start: number
  source_end: number
}

export interface DataProcessPdfPages {
  page_count: number
  pages: DataProcessPdfPageRange[]
}

export interface DataProcessDocxParagraph {
  type: 'paragraph'
  text: string
  style: string
  heading_level: number | null
  alignment: 'left' | 'center' | 'right' | 'justify' | 'distribute'
  is_list: boolean
  source_start: number
  source_end: number
}

export interface DataProcessDocxTableRow {
  cells: string[]
  source_start: number
  source_end: number
}

export interface DataProcessDocxTable {
  type: 'table'
  rows: DataProcessDocxTableRow[]
}

export interface DataProcessDocxPreview {
  format: 'docx'
  file_name: string
  blocks: Array<DataProcessDocxParagraph | DataProcessDocxTable>
  truncated: boolean
}

export interface DataProcessXlsxSheet {
  index: number
  name: string
  state: string
}

export interface DataProcessXlsxPreviewRow {
  row_number: number
  record_index: number
  values: unknown[]
  record: Record<string, unknown>
}

export interface DataProcessXlsxActiveSheet {
  index: number
  name: string
  columns: string[]
  rows: DataProcessXlsxPreviewRow[]
  offset: number
  limit: number
  has_more: boolean
}

export interface DataProcessXlsxPreview {
  format: 'xlsx'
  file_name: string
  sheets: DataProcessXlsxSheet[]
  active_sheet: DataProcessXlsxActiveSheet
}

export type DataProcessOfficePreview = DataProcessDocxPreview | DataProcessXlsxPreview

export interface DataProcessExternalSourcePayload {
  type: 'postgresql'
  url: string
  auth_mode: 'none' | 'basic'
  username?: string
  password?: string
  limit: number
  query?: string
  file_name?: string
}

export interface DataProcessExternalTestResult {
  connected: boolean
  latency_ms?: number
  message?: string
}

export interface DataProcessPreviewItem {
  id: string | number
  source_file_id: string | number
  original_content: string
  edited_content: string
  source_start: number | null
  source_end: number | null
  source_start_line: number | null
  source_end_line: number | null
  token_count: number
  status: 'original' | 'modified' | 'manual' | 'invalid'
  quality_score?: DataProcessQualityScore | null
  updated_at?: string
}

export interface DataProcessPreviewBuildPayload {
  replace_existing?: true
  source_file_ids?: Array<string | number>
}

export type DataProcessPreviewFileStatus = 'waiting' | 'processing' | 'success' | 'failed'

export interface DataProcessPreviewBuildFileResult {
  source_file_id: string | number
  preview_count: number
  status: 'completed' | 'empty'
}

export interface DataProcessPreviewFileBuildProgress {
  source_file_id: string | number
  status: Exclude<DataProcessPreviewFileStatus, 'waiting'>
  progress: number
  preview_count?: number
  error?: string
}

export interface DataProcessPreviewBuildResult extends DataProcessPage<DataProcessPreviewItem> {
  file_counts?: Record<string, number>
  files?: DataProcessPreviewBuildFileResult[]
}

export interface DataProcessPreviewCreatePayload {
  source_file_id?: string | number | null
  original_content?: string
  edited_content: string
  source_start?: number | null
  source_end?: number | null
  source_start_line?: number | null
  source_end_line?: number | null
  token_count?: number
  status?: DataProcessPreviewItem['status']
}

export interface DataProcessPreviewUpdatePayload {
  edited_content: string
  status?: DataProcessPreviewItem['status']
  expected_updated_at?: string
}

export interface DataProcessProgress {
  task_id: string | number
  status: DataProcessStatus
  stage?: string
  progress: number
  message?: string
  processed_count?: number
  total_count?: number
  input_count?: number
  output_count?: number
  filtered_count?: number
  duplicate_count?: number
  error_count?: number
  failure_reason?: string | null
  updated_at?: string
}

export interface DataProcessResult {
  id: string | number
  preview_item_id?: string | number | null
  instruction: string
  input: string
  output: string
  original_instruction?: string | null
  original_input?: string | null
  original_output?: string | null
  status: DataProcessResultStatus
  error?: string | null
  split?: DataProcessSplit | null
  quality_score?: DataProcessQualityScore | null
  updated_at?: string
}

export interface DataProcessResultUpdatePayload {
  instruction: string
  input: string
  output: string
  expected_updated_at?: string
}

export interface DataProcessQualityScore {
  overall?: number
  completeness?: number
  length?: number
  readability?: number
  relevance?: number
  duplicate?: number
  is_valid?: boolean
  flags?: string[]
  fingerprint?: string
  [key: string]: unknown
}

export interface DataProcessPublishPayload {
  dataset_name: string
  dataset_type: 'train' | 'test' | 'eval' | 'val' | 'other'
  storage_type: 'local'
  split: DataProcessDatasetSplit
  format: 'alpaca_jsonl' | 'jsonl'
}

export interface DataProcessPublishResult {
  dataset_id?: string | number
  output_dataset_id?: string | number
  dataset_name?: string
  record_count?: number
  dataset?: {
    id: string | number
    name?: string
    record_count?: number
    count?: number
    [key: string]: unknown
  }
  datasets?: DataProcessOutputDataset[]
  output_datasets?: DataProcessOutputDataset[]
  created?: boolean
}
