import {
  IconPdf,
  IconPhoto,
  IconPlayerSkipForward,
  IconFileSpreadsheet,
  IconMusic,
  IconPresentationAnalytics,
  IconNotes,
} from '@tabler/icons-react-native';
import {AttachmentType} from '@interfaces/attachments';

const EXTENSIONS_BY_TYPE = {
  [AttachmentType.IMAGE]: ['png', 'jpg', 'jpeg', 'webp', 'gif', 'heic', 'heif'],
  [AttachmentType.VIDEO]: ['avi', 'mpeg', 'mp4', 'mov'],
  [AttachmentType.AUDIO]: ['mp3'],
  [AttachmentType.PRESENTATION]: ['ppt', 'pptx', 'ppsx', 'ppsm'],
  [AttachmentType.EXCEL]: ['xls', 'csv', 'xlsx', 'xlsm'],
  [AttachmentType.PDF]: ['pdf'],
};

// AUDIO/GIF map kept on their original icons so the legacy <Attachment />
// component (used in learning/AttachmentList, libraries/KnowledgeLibrary)
// renders unchanged. Feed call sites bypass this lookup by passing
// `type: undefined` and an extension — see the switch below.
const ATTACHMENT_ICONS: Record<AttachmentType, any> = {
  [AttachmentType.AUDIO]: IconPlayerSkipForward,
  [AttachmentType.FILE]: IconNotes,
  [AttachmentType.GIF]: IconPlayerSkipForward,
  [AttachmentType.IMAGE]: IconPhoto,
  [AttachmentType.VIDEO]: IconPlayerSkipForward,
  [AttachmentType.PDF]: IconPdf,
  [AttachmentType.EXCEL]: IconFileSpreadsheet,
  [AttachmentType.PRESENTATION]: IconPresentationAnalytics,
  [AttachmentType.DOCUMENT]: IconNotes,
};

export function getIconByTypeOrExtension(
  type?: AttachmentType,
  extension?: string,
) {
  if (type && ATTACHMENT_ICONS[type]) {
    return ATTACHMENT_ICONS[type];
  }

  if (extension) {
    for (const [fileType, extensions] of Object.entries(EXTENSIONS_BY_TYPE)) {
      if (extensions.includes(extension)) {
        switch (fileType) {
          case AttachmentType.IMAGE:
            return IconPhoto;
          case AttachmentType.VIDEO:
            return IconPlayerSkipForward;
          case AttachmentType.AUDIO:
            return IconMusic;
          case AttachmentType.PRESENTATION:
            return IconPresentationAnalytics;
          case AttachmentType.EXCEL:
            return IconFileSpreadsheet;
          case AttachmentType.PDF:
            return IconPdf;
        }
      }
    }
  }

  return IconNotes;
}
