import type { DriveFile } from "@/lib/types";

const GOOGLE_MIME_PREFIX = "application/vnd.google-apps.";

export function isGoogleDriveMimeType(mimeType: string) {
  return mimeType.trim().toLowerCase().startsWith(GOOGLE_MIME_PREFIX);
}

export function googleDriveAttachmentLabel(mimeType: string) {
  const value = mimeType.trim().toLowerCase();
  if (value === "application/vnd.google-apps.folder") return "Folder";
  if (value === "application/vnd.google-apps.document") return "Google Doc";
  if (value === "application/vnd.google-apps.spreadsheet") return "Google Sheet";
  if (value === "application/vnd.google-apps.presentation") return "Google Slides";
  if (value === "application/vnd.google-apps.drawing") return "Drawing";
  return "Google File";
}

export function googleDriveOpenUrl(file: DriveFile) {
  if (file.mime_type === "application/vnd.google-apps.folder") {
    return `https://drive.google.com/drive/folders/${encodeURIComponent(file.id)}`;
  }
  if (file.mime_type.startsWith(GOOGLE_MIME_PREFIX)) {
    return file.web_view_link || `https://drive.google.com/open?id=${encodeURIComponent(file.id)}`;
  }
  return file.web_view_link || `https://drive.google.com/file/d/${encodeURIComponent(file.id)}/view`;
}

export function googleDriveDownloadUrl(file: DriveFile) {
  return file.web_content_link || `/api/drive/files/${encodeURIComponent(file.id)}/download`;
}
