import {
  File as FileLucide,
  FileText,
  FileImage,
  FileArchive,
  FileSpreadsheet,
  Presentation,
  FileType,
} from "lucide-react"
import type { ComponentType } from "react"

import { cn } from "@/shared/lib/utils"

type FileCategory =
  | "image"
  | "pdf"
  | "word"
  | "excel"
  | "powerpoint"
  | "text"
  | "archive"
  | "other"

const ICON_MAP: Record<FileCategory, ComponentType<{ className?: string }>> = {
  image: FileImage,
  pdf: FileText,
  word: FileText,
  excel: FileSpreadsheet,
  powerpoint: Presentation,
  text: FileType,
  archive: FileArchive,
  other: FileLucide,
}

const COLOR_MAP: Record<FileCategory, string> = {
  image: "text-pink-600",
  pdf: "text-red-600",
  word: "text-blue-600",
  excel: "text-green-600",
  powerpoint: "text-orange-600",
  text: "text-gray-600",
  archive: "text-yellow-600",
  other: "text-muted-foreground",
}

function resolveCategory(mimeType: string): FileCategory {
  if (mimeType.startsWith("image/")) return "image"
  if (mimeType === "application/pdf") return "pdf"
  if (
    mimeType === "application/msword" ||
    mimeType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  ) {
    return "word"
  }
  if (
    mimeType === "application/vnd.ms-excel" ||
    mimeType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  ) {
    return "excel"
  }
  if (
    mimeType === "application/vnd.ms-powerpoint" ||
    mimeType === "application/vnd.openxmlformats-officedocument.presentationml.presentation"
  ) {
    return "powerpoint"
  }
  if (mimeType === "text/plain" || mimeType === "text/markdown") return "text"
  if (mimeType === "application/zip" || mimeType === "application/x-rar-compressed") {
    return "archive"
  }
  return "other"
}

export function FileIcon({
  mimeType,
  className,
}: {
  mimeType: string
  className?: string
}) {
  const category = resolveCategory(mimeType)
  const Icon = ICON_MAP[category]
  return (
    <Icon className={cn("h-5 w-5", COLOR_MAP[category], className)} aria-hidden="true" />
  )
}
