// =============================================================================
// DocumentUpload shared types — extracted from DocumentUploadSection for LOC compliance
// =============================================================================

export interface UploadedDocument {
  id: string
  name: string
  type: string
  size: number
  uploadedAt: Date
  extractedVersion?: string
  extractedContent?: ExtractedContent
  status: 'uploading' | 'processing' | 'ready' | 'error'
  error?: string
}

export interface ExtractedContent {
  title?: string
  version?: string
  lastModified?: string
  sections?: ExtractedSection[]
  metadata?: Record<string, string>
}

export interface ExtractedSection {
  title: string
  content: string
  type?: string
}

export interface DocumentUploadSectionProps {
  /** Type of document being uploaded (tom, dsfa, vvt, loeschfristen, etc.) */
  documentType: 'tom' | 'dsfa' | 'vvt' | 'loeschfristen' | 'consent' | 'policy' | 'custom'
  /** Title displayed in the upload section */
  title?: string
  /** Description text */
  description?: string
  /** Accepted file types */
  acceptedTypes?: string
  /** Callback when document is uploaded and processed */
  onDocumentProcessed?: (doc: UploadedDocument) => void
  /** Callback to open document in workflow editor */
  onOpenInEditor?: (doc: UploadedDocument) => void
  /** Session ID for QR upload */
  sessionId?: string
  /** Custom CSS classes */
  className?: string
}

// =============================================================================
// Helper functions
// =============================================================================

export function formatFileSize(bytes: number): string {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
}

export function detectVersionFromFilename(filename: string): string | undefined {
  // Common version patterns: v1.0, V2.1, _v3, -v1.2.3, version-2
  const patterns = [
    /[vV](\d+(?:\.\d+)*)/,
    /version[_-]?(\d+(?:\.\d+)*)/i,
    /[_-]v?(\d+\.\d+(?:\.\d+)?)[_-]/,
  ]

  for (const pattern of patterns) {
    const match = filename.match(pattern)
    if (match) {
      return match[1]
    }
  }
  return undefined
}

export function suggestNextVersion(currentVersion?: string): string {
  if (!currentVersion) return '1.0'

  const parts = currentVersion.split('.').map(Number)
  if (parts.length >= 2) {
    parts[parts.length - 1] += 1
  } else {
    parts.push(1)
  }
  return parts.join('.')
}
