import IconFile from '~icons/lucide/file'
import IconFileType from '~icons/lucide/file-type'
import IconFileText from '~icons/lucide/file-text'
import IconFileSpreadsheet from '~icons/lucide/file-spreadsheet'
import IconPresentation from '~icons/lucide/presentation'
import IconImage from '~icons/lucide/image'
import IconArchive from '~icons/lucide/archive'
import IconFileAudio2 from '~icons/lucide/file-audio-2'
import IconFileVideo2 from '~icons/lucide/file-video-2'
import IconFileCode from '~icons/lucide/file-code'

const iconMap = [
  {
    label: 'PDF',
    exts: ['pdf'],
    component: IconFileType,
    colorClass: 'text-red-500'
  },
  {
    label: 'Word',
    exts: ['doc', 'docx'],
    component: IconFileText,
    colorClass: 'text-blue-500'
  },
  {
    label: 'Excel',
    exts: ['xls', 'xlsx', 'csv'],
    component: IconFileSpreadsheet,
    colorClass: 'text-green-500'
  },
  {
    label: 'PowerPoint',
    exts: ['ppt', 'pptx'],
    component: IconPresentation,
    colorClass: 'text-orange-500'
  },
  {
    label: 'Image',
    exts: ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp', 'heic'],
    component: IconImage,
    colorClass: 'text-purple-500'
  },
  {
    label: 'Archive',
    exts: ['zip', 'rar', '7z', 'tar', 'gz'],
    component: IconArchive,
    colorClass: 'text-amber-500'
  },
  {
    label: 'Audio',
    exts: ['mp3', 'wav', 'ogg', 'flac', 'aac'],
    component: IconFileAudio2,
    colorClass: 'text-pink-500'
  },
  {
    label: 'Vidéo',
    exts: ['mp4', 'mov', 'avi', 'mkv', 'webm'],
    component: IconFileVideo2,
    colorClass: 'text-indigo-500'
  },
  {
    label: 'Texte',
    exts: ['txt', 'md', 'rtf'],
    component: IconFileText,
    colorClass: 'text-gray-500'
  },
  {
    label: 'Code',
    exts: ['json', 'xml', 'yml', 'yaml', 'js', 'ts', 'py', 'java', 'cs'],
    component: IconFileCode,
    colorClass: 'text-sky-500'
  }
]

const mimeGroups = [
  { prefix: 'image/', component: IconImage, colorClass: 'text-purple-500', label: 'Image' },
  { prefix: 'video/', component: IconFileVideo2, colorClass: 'text-indigo-500', label: 'Vidéo' },
  { prefix: 'audio/', component: IconFileAudio2, colorClass: 'text-pink-500', label: 'Audio' },
  { prefix: 'text/', component: IconFileText, colorClass: 'text-gray-500', label: 'Texte' },
  { prefix: 'application/pdf', component: IconFileType, colorClass: 'text-red-500', label: 'PDF' },
  { prefix: 'application/zip', component: IconArchive, colorClass: 'text-amber-500', label: 'Archive' },
  { prefix: 'application/x-', component: IconArchive, colorClass: 'text-amber-500', label: 'Archive' }
]

export const getFileIcon = ({ name = '', mime = '' } = {}) => {
  const extension = name.split('.').pop()?.toLowerCase() || ''

  if (extension) {
    const match = iconMap.find(entry => entry.exts.includes(extension))
    if (match) {
      return {
        component: match.component,
        colorClass: match.colorClass,
        label: match.label
      }
    }
  }

  if (mime) {
    const match = mimeGroups.find(entry => mime.startsWith(entry.prefix))
    if (match) {
      return {
        component: match.component,
        colorClass: match.colorClass,
        label: match.label || mime
      }
    }
  }

  return {
    component: IconFile,
    colorClass: 'text-primary',
    label: 'Document'
  }
}

export const describeFileType = ({ name = '', mime = '' } = {}) => {
  const icon = getFileIcon({ name, mime })
  return icon.label
}
