import type { DriveView } from "@/lib/drive/drive-url"
import { displayFileName } from "@/lib/drive/display-file-name"
import { normalizeDriveFolderPath } from "@/lib/drive/drive-sidebar-tree"

export const DRIVE_SEARCH_SCOPES = ["all", "shared", "folder"] as const
export type DriveSearchScope = (typeof DRIVE_SEARCH_SCOPES)[number]

export interface DriveSearchState {
  query: string
  scope: DriveSearchScope
  /** Folder path when scope is `folder`. */
  folderPath: string
}

export function defaultDriveSearchScope(
  view: DriveView,
  folderPath: string
): DriveSearchScope {
  if (view === "shared" && folderPath === "/") return "shared"
  if (folderPath !== "/") return "folder"
  return "all"
}

export function driveSearchScopeLabel(
  scope: DriveSearchScope,
  folderPath: string
): string {
  if (scope === "all") return "Mon Drive"
  if (scope === "shared") return "Partagés avec moi"
  const normalized = normalizeDriveFolderPath(folderPath)
  if (normalized === "/") return "Dossier courant"
  const segments = normalized.slice(1).split("/")
  return segments.map(displayFileName).join(" / ")
}

export function driveSearchScopeShortLabel(scope: DriveSearchScope): string {
  if (scope === "all") return "Mon Drive"
  if (scope === "shared") return "Partagés"
  return "Ce dossier"
}

export function buildDriveSearchUrl(state: DriveSearchState, routeRoot = "drive"): string {
  const params = new URLSearchParams()
  params.set("q", state.query.trim())
  params.set("scope", state.scope)
  if (state.scope === "folder" && state.folderPath !== "/") {
    params.set("path", normalizeDriveFolderPath(state.folderPath))
  }
  return `/${routeRoot}/search?${params.toString()}`
}

export function parseDriveSearchParams(
  searchParams: URLSearchParams,
  fallback: Pick<DriveSearchState, "scope" | "folderPath">
): DriveSearchState {
  const query = searchParams.get("q")?.trim() ?? ""
  const scopeRaw = searchParams.get("scope")
  const scope = DRIVE_SEARCH_SCOPES.includes(scopeRaw as DriveSearchScope)
    ? (scopeRaw as DriveSearchScope)
    : fallback.scope
  const pathRaw = searchParams.get("path")
  const folderPath = pathRaw
    ? normalizeDriveFolderPath(pathRaw)
    : fallback.folderPath
  return { query, scope, folderPath }
}

export function parentFolderPath(filePath: string): string {
  const normalized = normalizeDriveFolderPath(filePath)
  if (normalized === "/") return "/"
  const lastSlash = normalized.lastIndexOf("/")
  if (lastSlash <= 0) return "/"
  return normalized.slice(0, lastSlash) || "/"
}

/** Parent folder path for a file or folder item. */
export function itemParentFolderPath(itemPath: string, itemType: "file" | "directory"): string {
  const normalized = normalizeDriveFolderPath(itemPath)
  if (itemType === "directory") {
    return parentFolderPath(normalized)
  }
  return parentFolderPath(normalized)
}

export function itemLocationLabel(itemPath: string, itemType: "file" | "directory"): string {
  const parent = itemParentFolderPath(itemPath, itemType)
  if (parent === "/") return "Mon Drive"
  return parent
    .slice(1)
    .split("/")
    .map(displayFileName)
    .join(" / ")
}

export function searchResultsTitle(state: DriveSearchState): string {
  const scopeLabel = driveSearchScopeLabel(state.scope, state.folderPath)
  return `Résultats pour « ${state.query} » dans ${scopeLabel}`
}

export function fileBrowserViewForSearchScope(scope: DriveSearchScope): "files" | "shared" {
  return scope === "shared" ? "shared" : "files"
}
