export type FileKind = "image" | "video" | "pdf" | "sheet" | "archive" | "other";

export function formatFileSize(sizeBytes: number) {
  if (sizeBytes < 1024) return `${sizeBytes} B`;
  if (sizeBytes < 1024 * 1024) return `${(sizeBytes / 1024).toFixed(1)} KB`;
  return `${(sizeBytes / (1024 * 1024)).toFixed(1)} MB`;
}

export function getFileKind(mimeType: string | null, fileName: string): FileKind {
  const normalizedMime = (mimeType || "").toLowerCase();
  const normalizedName = fileName.toLowerCase();

  if (normalizedMime.startsWith("image/")) return "image";
  if (normalizedMime.startsWith("video/")) return "video";
  if (
    normalizedName.endsWith(".mp4") ||
    normalizedName.endsWith(".mov") ||
    normalizedName.endsWith(".webm") ||
    normalizedName.endsWith(".avi") ||
    normalizedName.endsWith(".mkv")
  ) {
    return "video";
  }
  if (normalizedMime.includes("pdf") || normalizedName.endsWith(".pdf")) return "pdf";
  if (
    normalizedMime.includes("sheet") ||
    normalizedMime.includes("excel") ||
    normalizedName.endsWith(".csv") ||
    normalizedName.endsWith(".xlsx")
  ) {
    return "sheet";
  }
  if (
    normalizedMime.includes("zip") ||
    normalizedMime.includes("archive") ||
    normalizedName.endsWith(".zip") ||
    normalizedName.endsWith(".rar")
  ) {
    return "archive";
  }

  return "other";
}

export function isPreviewableFile(mimeType: string | null, fileName: string) {
  const kind = getFileKind(mimeType, fileName);
  return kind === "image" || kind === "pdf" || kind === "video";
}
