/**
 * API Type Definitions
 * Based on backend OpenAPI specification
 */

// Authentication
export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  access_token: string
  token_type: string
  expires_in: number  // Token expiration time in seconds
}

export interface User {
  id: number
  username: string
  email?: string
  displayName?: string | null
}

// File Upload (V2 API)
export interface UploadResponse {
  task_id: string
  filename: string
  file_size: number
  file_type: string
  status: 'pending' | 'processing' | 'completed' | 'failed'
}

export interface FileInfo {
  id: number
  filename: string
  file_size: number
  file_format: string  // Changed from 'format' to match backend
  status: 'pending' | 'processing' | 'completed' | 'failed'
}

// OCR Processing
export interface ProcessRequest {
  batch_id: number
  lang?: string
  detect_layout?: boolean  // Changed from confidence_threshold to match backend
}

export interface ProcessResponse {
  message: string       // Added to match backend
  batch_id: number
  total_files: number   // Added to match backend
  status: string
  // Removed task_id - backend uses batch-level tracking instead
}

export interface TaskStatus {
  task_id: string
  status: 'pending' | 'processing' | 'completed' | 'failed'
  progress_percentage: number
  current_file?: string
  files_processed: number
  total_files: number
  error?: string
}

export interface BatchStatus {
  batch: {
    id: number
    status: 'pending' | 'processing' | 'completed' | 'failed'
    progress_percentage: number
    created_at: string
    completed_at?: string
  }
  files: FileResult[]
}

export interface FileResult {
  id: number
  filename: string
  status: 'pending' | 'processing' | 'completed' | 'failed'
  processing_time?: number
  error?: string
}

// OCR Results
export interface OCRResult {
  file_id: number
  filename: string
  status: string
  markdown_content: string
  json_data: OCRJsonData
  confidence: number
  processing_time: number
}

export interface OCRJsonData {
  total_text_regions: number
  average_confidence: number
  text_blocks: TextBlock[]
  layout_info?: LayoutInfo
}

export interface TextBlock {
  text: string
  confidence: number
  bbox: [number, number, number, number]
  position: number
}

export interface LayoutInfo {
  tables_detected: number
  images_detected: number
  structure: string
}

// Export
export interface ExportRequest {
  batch_id: number
  format: 'txt' | 'json' | 'excel' | 'markdown' | 'pdf'
  rule_id?: number
  options?: ExportOptions
}

export interface ExportOptions {
  confidence_threshold?: number
  include_metadata?: boolean
  filename_pattern?: string
  css_template?: string
}

export interface ExportRule {
  id: number
  rule_name: string
  config_json: Record<string, any>
  css_template?: string
  created_at: string
}

export interface CSSTemplate {
  name: string
  description: string
  // filename is not returned by backend - use name as identifier
}

// Translation (FUTURE FEATURE)
export interface TranslateRequest {
  file_id: number
  source_lang: string
  target_lang: string
  engine_type?: 'argos' | 'ernie' | 'google'
}

export interface TranslateResponse {
  task_id: string
  file_id: number
  status: 'pending' | 'processing' | 'completed' | 'failed'
  translated_content?: string
}

export interface TranslationConfig {
  id: number
  source_lang: string
  target_lang: string
  engine_type: 'argos' | 'ernie' | 'google'
  engine_config: Record<string, any>
  created_at: string
}

// API Response
export interface ApiResponse<T = any> {
  success: boolean
  data?: T
  error?: string
  message?: string
}

// Error Response
export interface ApiError {
  detail: string
  status_code: number
}
