export interface CrawlSource {
  id: string
  name: string
  source_type: string
  path: string
  file_extensions: string[]
  max_depth: number
  exclude_patterns: string[]
  enabled: boolean
  created_at: string
}

export interface CrawlJob {
  id: string
  source_id: string
  source_name?: string
  status: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled'
  job_type: 'full' | 'delta'
  files_found: number
  files_processed: number
  files_new: number
  files_changed: number
  files_skipped: number
  files_error: number
  error_message?: string
  started_at?: string
  completed_at?: string
  created_at: string
}

export interface CrawlDocument {
  id: string
  file_name: string
  file_extension: string
  file_size_bytes: number
  classification: string | null
  classification_confidence: number | null
  classification_corrected: boolean
  extraction_status: string
  archived: boolean
  ipfs_cid: string | null
  first_seen_at: string
  last_seen_at: string
  version_count: number
  source_name?: string
}

export interface OnboardingReport {
  id: string
  total_documents_found: number
  classification_breakdown: Record<string, number>
  gaps: GapItem[]
  compliance_score: number
  gap_summary?: { critical: number; high: number; medium: number }
  created_at: string
}

export interface GapItem {
  id: string
  category: string
  description: string
  severity: 'CRITICAL' | 'HIGH' | 'MEDIUM'
  regulation: string
  requiredAction: string
}

export const TENANT_ID = '00000000-0000-0000-0000-000000000001' // Default tenant

export async function api(path: string, options: RequestInit = {}) {
  const res = await fetch(`/api/sdk/v1/crawler/${path}`, {
    ...options,
    headers: {
      'Content-Type': 'application/json',
      'X-Tenant-ID': TENANT_ID,
      ...options.headers,
    },
  })
  if (res.status === 204) return null
  return res.json()
}

export const CLASSIFICATION_LABELS: Record<string, { label: string; color: string }> = {
  VVT: { label: 'VVT', color: 'bg-blue-100 text-blue-700' },
  TOM: { label: 'TOM', color: 'bg-green-100 text-green-700' },
  DSE: { label: 'DSE', color: 'bg-purple-100 text-purple-700' },
  AVV: { label: 'AVV', color: 'bg-orange-100 text-orange-700' },
  DSFA: { label: 'DSFA', color: 'bg-red-100 text-red-700' },
  Loeschkonzept: { label: 'Loeschkonzept', color: 'bg-yellow-100 text-yellow-700' },
  Einwilligung: { label: 'Einwilligung', color: 'bg-pink-100 text-pink-700' },
  Vertrag: { label: 'Vertrag', color: 'bg-indigo-100 text-indigo-700' },
  Richtlinie: { label: 'Richtlinie', color: 'bg-teal-100 text-teal-700' },
  Schulungsnachweis: { label: 'Schulung', color: 'bg-cyan-100 text-cyan-700' },
  Sonstiges: { label: 'Sonstiges', color: 'bg-gray-100 text-gray-700' },
}

export const ALL_CLASSIFICATIONS = Object.keys(CLASSIFICATION_LABELS)

export type Tab = 'sources' | 'jobs' | 'documents' | 'report'
