import { decodePathSegment } from "@/lib/drive/drive-url"

function encodeFolderPath(folderPath: string): string {
  if (folderPath === "/" || folderPath.trim() === "") return ""
  return folderPath
    .replace(/^\/+/, "")
    .split("/")
    .filter(Boolean)
    .map((seg) => encodeURIComponent(decodePathSegment(seg)))
    .join("/")
}

export function driveOrgFilesApiPath(folderId: string, folderPath: string): string {
  const encoded = encodeFolderPath(folderPath)
  const base = `/drive/org-folders/${encodeURIComponent(folderId)}/files`
  return encoded ? `${base}/${encoded}` : `${base}/`
}

export function driveMountFilesApiPath(mountId: string, folderPath: string): string {
  const encoded = encodeFolderPath(folderPath)
  const base = `/drive/mounts/${encodeURIComponent(mountId)}/files`
  return encoded ? `${base}/${encoded}` : `${base}/`
}

export function driveOrgDownloadApiPath(folderId: string, filePath: string): string {
  const encoded = encodeFolderPath(filePath)
  return `/drive/org-folders/${encodeURIComponent(folderId)}/download/${encoded}`
}

export function driveMountDownloadApiPath(mountId: string, filePath: string): string {
  const encoded = encodeFolderPath(filePath)
  return `/drive/mounts/${encodeURIComponent(mountId)}/download/${encoded}`
}

export function driveOrgPreviewApiPath(
  folderId: string,
  filePath: string,
  width = 400,
  height = 300
): string {
  const encoded = encodeFolderPath(filePath)
  return `/drive/org-folders/${encodeURIComponent(folderId)}/preview/${encoded}?w=${width}&h=${height}`
}

export function driveMountPreviewApiPath(
  mountId: string,
  filePath: string,
  width = 400,
  height = 300
): string {
  const encoded = encodeFolderPath(filePath)
  return `/drive/mounts/${encodeURIComponent(mountId)}/preview/${encoded}?w=${width}&h=${height}`
}

export function driveMountExternalURLApiPath(mountId: string, filePath: string): string {
  const encoded = encodeFolderPath(filePath)
  return `/drive/mounts/${encodeURIComponent(mountId)}/files/external-url/${encoded}`
}

export interface DrivePathRef {
  root?: "personal" | "org" | "mount"
  root_id?: string
  path: string
}

export function pathRefFromRoute(
  view: string,
  rootId: string | null,
  path: string
): DrivePathRef {
  if (view === "org" && rootId) return { root: "org", root_id: rootId, path }
  if (view === "mount" && rootId) return { root: "mount", root_id: rootId, path }
  return { path }
}

export function withPathRefBody<T extends Record<string, unknown>>(
  body: T,
  ref: DrivePathRef
): T & Partial<DrivePathRef> {
  if (ref.root && ref.root !== "personal") {
    return { ...body, root: ref.root, root_id: ref.root_id, path: ref.path }
  }
  return body
}

export function withMoveCopyRefs(
  source: DrivePathRef,
  destination: DrivePathRef
): Record<string, string> {
  const payload: Record<string, string> = {
    source: source.path,
    destination: destination.path,
  }
  if (source.root && source.root !== "personal") {
    payload.source_root = source.root
    if (source.root_id) payload.source_root_id = source.root_id
  }
  if (destination.root && destination.root !== "personal") {
    payload.destination_root = destination.root
    if (destination.root_id) payload.destination_root_id = destination.root_id
  }
  return payload
}
