export interface DocumentItem {
  document_id: string
  filename: string
  file_size: number
  content_type: string
  page_count: number
  status: 'pending' | 'labeled' | 'verified' | 'exported'
  auto_label_status: 'pending' | 'running' | 'completed' | 'failed' | null
  auto_label_error: string | null
  upload_source: string
  group_key: string | null
  category: string
  created_at: string
  updated_at: string
  annotation_count?: number
  annotation_sources?: {
    manual: number
    auto: number
    verified: number
  }
}

export interface DocumentListResponse {
  documents: DocumentItem[]
  total: number
  limit: number
  offset: number
}

export interface AnnotationItem {
  annotation_id: string
  page_number: number
  class_id: number
  class_name: string
  bbox: {
    x: number
    y: number
    width: number
    height: number
  }
  normalized_bbox: {
    x_center: number
    y_center: number
    width: number
    height: number
  }
  text_value: string | null
  confidence: number | null
  source: 'manual' | 'auto'
  created_at: string
}

export interface DocumentDetailResponse {
  document_id: string
  filename: string
  file_size: number
  content_type: string
  page_count: number
  status: 'pending' | 'labeled' | 'verified' | 'exported'
  auto_label_status: 'pending' | 'running' | 'completed' | 'failed' | null
  auto_label_error: string | null
  upload_source: string
  batch_id: string | null
  group_key: string | null
  category: string
  csv_field_values: Record<string, string> | null
  can_annotate: boolean
  annotation_lock_until: string | null
  annotations: AnnotationItem[]
  image_urls: string[]
  training_history: Array<{
    task_id: string
    name: string
    trained_at: string
    model_metrics: {
      mAP: number | null
      precision: number | null
      recall: number | null
    } | null
  }>
  created_at: string
  updated_at: string
}

export interface TrainingTask {
  task_id: string
  admin_token: string
  name: string
  description: string | null
  status: 'pending' | 'running' | 'completed' | 'failed'
  task_type: string
  config: Record<string, unknown>
  started_at: string | null
  completed_at: string | null
  error_message: string | null
  result_metrics: Record<string, unknown>
  model_path: string | null
  document_count: number
  metrics_mAP: number | null
  metrics_precision: number | null
  metrics_recall: number | null
  created_at: string
  updated_at: string
}

export interface ModelVersionItem {
  version_id: string
  version: string
  name: string
  status: string
  is_active: boolean
  metrics_mAP: number | null
  document_count: number
  model_type?: string
  base_model_version_id?: string | null
  gating_status?: string
  trained_at: string | null
  activated_at: string | null
  created_at: string
}

export interface TrainingModelsResponse {
  models: ModelVersionItem[]
  total: number
  limit: number
  offset: number
}

export interface ErrorResponse {
  detail: string
}

export interface UploadDocumentResponse {
  document_id: string
  filename: string
  file_size: number
  page_count: number
  status: string
  category: string
  group_key: string | null
  auto_label_started: boolean
  message: string
}

export interface DocumentCategoriesResponse {
  categories: string[]
  total: number
}

export interface CreateAnnotationRequest {
  page_number: number
  class_id: number
  bbox: {
    x: number
    y: number
    width: number
    height: number
  }
  text_value?: string
}

export interface AnnotationOverrideRequest {
  text_value?: string
  bbox?: {
    x: number
    y: number
    width: number
    height: number
  }
  class_id?: number
  class_name?: string
  reason?: string
}

export interface CrossValidationResult {
  is_valid: boolean
  payment_line_ocr: string | null
  payment_line_amount: string | null
  payment_line_account: string | null
  payment_line_account_type: 'bankgiro' | 'plusgiro' | null
  ocr_match: boolean | null
  amount_match: boolean | null
  bankgiro_match: boolean | null
  plusgiro_match: boolean | null
  details: string[]
}

// Business Features Types (Line Items, VAT)

export interface LineItem {
  row_index: number
  description: string | null
  quantity: string | null
  unit: string | null
  unit_price: string | null
  amount: string | null
  article_number: string | null
  vat_rate: string | null
  is_deduction: boolean
  confidence: number
}

export interface LineItemsResult {
  items: LineItem[]
  header_row: string[]
  total_amount: string | null
}

export interface VATBreakdown {
  rate: number
  base_amount: string | null
  vat_amount: string
  source: string
}

export interface VATSummary {
  breakdowns: VATBreakdown[]
  total_excl_vat: string | null
  total_vat: string | null
  total_incl_vat: string | null
  confidence: number
}

export interface MathCheckResult {
  rate: number
  base_amount: number | null
  expected_vat: number | null
  actual_vat: number | null
  is_valid: boolean
  tolerance: number
}

export interface VATValidationResult {
  is_valid: boolean
  confidence_score: number
  math_checks: MathCheckResult[]
  total_check: boolean
  line_items_vs_summary: boolean | null
  amount_consistency: boolean | null
  needs_review: boolean
  review_reasons: string[]
}

export interface InferenceResult {
  document_id: string
  document_type: string
  success: boolean
  fields: Record<string, string>
  confidence: Record<string, number>
  cross_validation: CrossValidationResult | null
  processing_time_ms: number
  visualization_url: string | null
  errors: string[]
  fallback_used: boolean
  // Business features (optional, only when extract_line_items=true)
  line_items: LineItemsResult | null
  vat_summary: VATSummary | null
  vat_validation: VATValidationResult | null
}

export interface InferenceResponse {
  result: InferenceResult
}

// Dataset types

export interface DatasetCreateRequest {
  name: string
  description?: string
  document_ids: string[]
  train_ratio?: number
  val_ratio?: number
  seed?: number
}

export interface DatasetResponse {
  dataset_id: string
  name: string
  status: string
  message: string
}

export interface DatasetDocumentItem {
  document_id: string
  split: string
  page_count: number
  annotation_count: number
}

export interface DatasetListItem {
  dataset_id: string
  name: string
  description: string | null
  status: string
  training_status: string | null
  active_training_task_id: string | null
  total_documents: number
  total_images: number
  total_annotations: number
  created_at: string
}

export interface DatasetListResponse {
  total: number
  limit: number
  offset: number
  datasets: DatasetListItem[]
}

export interface DatasetDetailResponse {
  dataset_id: string
  name: string
  description: string | null
  status: string
  training_status: string | null
  active_training_task_id: string | null
  train_ratio: number
  val_ratio: number
  seed: number
  total_documents: number
  total_images: number
  total_annotations: number
  dataset_path: string | null
  error_message: string | null
  documents: DatasetDocumentItem[]
  created_at: string
  updated_at: string
}

export interface AugmentationParams {
  enabled: boolean
  probability: number
  params: Record<string, unknown>
}

export interface AugmentationTrainingConfig {
  gaussian_noise?: AugmentationParams
  perspective_warp?: AugmentationParams
  wrinkle?: AugmentationParams
  edge_damage?: AugmentationParams
  stain?: AugmentationParams
  lighting_variation?: AugmentationParams
  shadow?: AugmentationParams
  gaussian_blur?: AugmentationParams
  motion_blur?: AugmentationParams
  salt_pepper?: AugmentationParams
  paper_texture?: AugmentationParams
  scanner_artifacts?: AugmentationParams
  preserve_bboxes?: boolean
  seed?: number | null
}

export interface DatasetTrainRequest {
  name: string
  config: {
    model_name?: string
    base_model_version_id?: string | null
    epochs?: number
    batch_size?: number
    image_size?: number
    learning_rate?: number
    device?: string
    augmentation?: AugmentationTrainingConfig
    augmentation_multiplier?: number
  }
}

export interface TrainingTaskResponse {
  task_id: string
  status: string
  message: string
}

// Model Version types

export interface ModelVersionDetailResponse {
  version_id: string
  version: string
  name: string
  description: string | null
  model_path: string
  status: string
  is_active: boolean
  task_id: string | null
  dataset_id: string | null
  metrics_mAP: number | null
  metrics_precision: number | null
  metrics_recall: number | null
  document_count: number
  model_type?: string
  base_model_version_id?: string | null
  base_training_dataset_id?: string | null
  gating_status?: string
  training_config: Record<string, unknown> | null
  file_size: number | null
  trained_at: string | null
  activated_at: string | null
  created_at: string
  updated_at: string
}

// Fine-Tune Pool types

export interface PoolEntryItem {
  entry_id: string
  document_id: string
  added_by: string | null
  reason: string | null
  is_verified: boolean
  verified_at: string | null
  verified_by: string | null
  created_at: string
}

export interface PoolListResponse {
  total: number
  limit: number
  offset: number
  entries: PoolEntryItem[]
}

export interface PoolStatsResponse {
  total_entries: number
  verified_entries: number
  unverified_entries: number
  is_ready: boolean
  min_required: number
}

export interface PoolEntryResponse {
  entry_id: string
  message: string
}

export interface ModelVersionListResponse {
  total: number
  limit: number
  offset: number
  models: ModelVersionItem[]
}

export interface ModelVersionResponse {
  version_id: string
  status: string
  message: string
}

export interface ActiveModelResponse {
  has_active_model: boolean
  model: ModelVersionItem | null
}

// Dashboard types

export interface DashboardStatsResponse {
  total_documents: number
  annotation_complete: number
  annotation_incomplete: number
  pending: number
  completeness_rate: number
}

export interface DashboardActiveModelInfo {
  version_id: string
  version: string
  name: string
  metrics_mAP: number | null
  metrics_precision: number | null
  metrics_recall: number | null
  document_count: number
  activated_at: string | null
}

export interface DashboardRunningTrainingInfo {
  task_id: string
  name: string
  status: string
  started_at: string | null
  progress: number
}

export interface DashboardActiveModelResponse {
  model: DashboardActiveModelInfo | null
  running_training: DashboardRunningTrainingInfo | null
}

export interface ActivityItem {
  type: 'document_uploaded' | 'annotation_modified' | 'training_completed' | 'training_failed' | 'model_activated'
  description: string
  timestamp: string
  metadata: Record<string, unknown>
}

export interface RecentActivityResponse {
  activities: ActivityItem[]
}
