import { apiClient } from "@/lib/api/client"
import { driveMountExternalURLApiPath } from "@/lib/api/drive-roots"
import type { DriveFileInfo } from "@/lib/api/types"

/** Mounted cloud file that should open in Google / Microsoft web apps by default. */
export function shouldOpenMountFileExternally(file: DriveFileInfo): boolean {
  return (
    file.root_kind === "mount" &&
    file.type !== "directory" &&
    Boolean(file.open_externally)
  )
}

export async function resolveMountExternalURL(
  mountId: string,
  filePath: string
): Promise<string | null> {
  const path = filePath.startsWith("/") ? filePath : `/${filePath}`
  const data = await apiClient.get<{ external_url?: string }>(
    driveMountExternalURLApiPath(mountId, path)
  )
  const url = data.external_url?.trim()
  return url || null
}

/** Open mount cloud document in provider web app (new tab). Returns false if blocked or unresolved. */
export async function openMountFileExternally(file: DriveFileInfo): Promise<boolean> {
  let url = file.external_url?.trim()
  if (!url && file.root_id) {
    url = (await resolveMountExternalURL(file.root_id, file.path)) ?? undefined
  }
  if (!url) return false
  const opened = window.open(url, "_blank", "noopener,noreferrer")
  return Boolean(opened)
}

/** Branding for mount cloud-native files (label + Iconify icon). */
export type MountCloudSuiteBrand = {
  label: string
  icon: string
}

export function mountCloudSuiteBrand(file: DriveFileInfo): MountCloudSuiteBrand | null {
  if (!shouldOpenMountFileExternally(file)) return null
  const label = mountExternalOpenLabel(file)
  const icon = mountCloudSuiteIcon(file)
  if (!label || !icon) return null
  return { label, icon }
}

/** Iconify id for native suite file type on external mounts. */
export function mountCloudSuiteIcon(file: DriveFileInfo): string | null {
  if (!shouldOpenMountFileExternally(file)) return null
  const backend = file.mount_backend
  if (backend === "google") {
    const mime = (file.mime_type ?? "").toLowerCase()
    if (mime.includes("spreadsheet")) return "logos:google-sheets"
    if (mime.includes("presentation")) return "logos:google-slides"
    if (mime.includes("document")) return "logos:google-docs"
    return "logos:google-drive"
  }
  if (backend === "microsoft") {
    const ext = file.name.split(".").pop()?.toLowerCase() ?? ""
    if (ext === "xls" || ext === "xlsx") return "vscode-icons:file-type-excel"
    if (ext === "ppt" || ext === "pptx") return "vscode-icons:file-type-powerpoint"
    if (ext === "doc" || ext === "docx") return "vscode-icons:file-type-word"
    return "logos:microsoft-onedrive"
  }
  return null
}

export function mountExternalOpenLabel(file: DriveFileInfo): string | null {
  if (!shouldOpenMountFileExternally(file)) return null
  const backend = file.mount_backend
  if (backend === "google") {
    const mime = (file.mime_type ?? "").toLowerCase()
    if (mime.includes("spreadsheet")) return "Ouvrir dans Google Sheets"
    if (mime.includes("presentation")) return "Ouvrir dans Google Slides"
    if (mime.includes("document")) return "Ouvrir dans Google Docs"
    return "Ouvrir dans Google Drive"
  }
  if (backend === "microsoft") {
    const ext = file.name.split(".").pop()?.toLowerCase() ?? ""
    if (ext === "xls" || ext === "xlsx") return "Ouvrir dans Excel Online"
    if (ext === "ppt" || ext === "pptx") return "Ouvrir dans PowerPoint Online"
    if (ext === "doc" || ext === "docx") return "Ouvrir dans Word Online"
    return "Ouvrir dans Microsoft 365"
  }
  return "Ouvrir dans l’application cloud"
}
