import type {
  DataProcessOutputType,
  DataProcessPreviewFileStatus,
  DataProcessReasoningDetail,
} from '@/types/dataProcess'

export type ProcessType = 'structured' | 'unstructured' | 'external'

export type StepId = 'create' | 'model' | 'upload' | 'preview' | 'generate' | 'results'

export const QA_PAIRS_GENERATION_LIMITS = { min: 1, max: 50 } as const

export function normalizeQaPairsGenerationCount(value: unknown, fallback = 1): number {
  const parsed = Number(value)
  const normalized = Number.isFinite(parsed) ? Math.trunc(parsed) : fallback
  return Math.min(
    QA_PAIRS_GENERATION_LIMITS.max,
    Math.max(QA_PAIRS_GENERATION_LIMITS.min, normalized),
  )
}

export type PreprocessOption =
  | 'clean_invalid'
  | 'detect_structure'
  | 'deduplicate'
  | 'normalize_format'
  | 'filter_anomaly'
  | 'desensitize'

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

export interface GenerationControlOptions {
  generationModelId: string | number | ''
  generationPrompt: string
  outputType: DataProcessOutputType
  reasoningDetail: DataProcessReasoningDetail
  temperature: number
  maxTokens: number
  jsonMode: boolean
  qualityFilterEnabled: boolean
  filterLowQuality: boolean
  filterShortContent: boolean
  minOutputLength: number
}

export interface StructuredProcessOptions extends GenerationControlOptions {
  preprocessOptions: PreprocessOption[]
  semanticEnrichment: boolean
  qaPairsPerRow: number
  datasetSplit: DatasetSplitOptions
}

export type UnstructuredPreprocessOption =
  | 'clean_invalid_content'
  | 'detect_document_structure'
  | 'merge_short_content'
  | 'filter_low_quality'
  | 'deduplicate_content'
  | 'desensitize'
  | 'preserve_context'

export type ChunkMethod = 'layout_hybrid' | 'semantic' | 'fixed'

export interface UnstructuredProcessOptions extends GenerationControlOptions {
  preprocessOptions: UnstructuredPreprocessOption[]
  chunkMethod: ChunkMethod
  chunkSize: number
  chunkOverlap: number
  minChunkSize: number
  semanticBreakpointPercentile: number
  preserveTables: boolean
  preserveCodeBlocks: boolean
  preserveLists: boolean
  semanticEnrichment: boolean
  qaPairsPerChunk: number
  datasetSplit: DatasetSplitOptions
}

export interface ExternalDataSource {
  type: 'postgresql'
  url: string
  authMode: 'none' | 'basic'
  username?: string
  password?: string
  limit: number
  query?: string
  fileName?: string
}

export interface UploadedDataFile {
  uid: string | number
  sourceFileId?: string
  rawFile?: File
  name: string
  size: number
  count: number
  content: string
  fileFormat?: string
  checksumSha256?: string
  status: 'queued' | 'uploading' | 'ready' | 'failed'
  uploadProgress: number
  uploadError?: string
  previewStatus?: DataProcessPreviewFileStatus
  previewProgress?: number
  previewError?: string
  previewCount?: number
  previewConfigSignature?: string
}

export interface SourceLine {
  number: number
  content: string
  start: number
  end: number
}

export interface PreviewItem {
  id: string
  sourceFileId: string
  originalContent: string
  editedContent: string
  sourceStart: number | null
  sourceEnd: number | null
  sourceStartLine: number | null
  sourceEndLine: number | null
  sourcePages?: number[]
  tokenCount: number
  status: 'original' | 'modified' | 'manual' | 'invalid'
  qualityScore?: number
  qualityDetails?: Record<string, number>
  piiStats?: Record<string, number>
  updatedAt?: string
}

export interface GenerationState {
  status: 'idle' | 'running' | 'success' | 'failed'
  progress: number
  message: string
}

export interface ResultItem {
  id: string
  previewItemId: string | null
  instruction: string
  input: string
  output: string
  originalInstruction: string
  originalInput: string
  originalOutput: string
  status: 'valid' | 'modified' | 'invalid'
  error?: string
  split?: 'train' | 'validation' | 'test'
  qualityScore?: number
  qualityDetails?: Record<string, number>
  qualityFlags?: string[]
  updatedAt?: string
}
