import {
  DEMO_DRIVE_FILES,
  DEMO_DRIVE_RECENT_IDS,
  DEMO_DRIVE_SHARED,
  DEMO_DRIVE_STARRED_IDS,
  DEMO_DRIVE_TRASH,
} from "@/components/demo/demo-drive-data"
import type { DriveFileInfo, DriveListResponse } from "@/lib/api/types"

function normalizeFolderPath(path: string): string {
  if (!path || path === "/") return "/"
  return path.startsWith("/") ? path.replace(/\/+$/, "") : `/${path.replace(/\/+$/, "")}`
}

function parentPath(path: string): string {
  const normalized = path.replace(/\/+$/, "")
  const idx = normalized.lastIndexOf("/")
  if (idx <= 0) return "/"
  return normalized.slice(0, idx)
}

function listChildren(files: DriveFileInfo[], folderPath: string): DriveFileInfo[] {
  const folder = normalizeFolderPath(folderPath)
  return files.filter((f) => {
    if (f.type === "directory") {
      if (folder === "/") return f.path.split("/").length === 2
      return parentPath(f.path) === folder
    }
    return parentPath(f.path) === folder
  })
}

function paginate(
  items: DriveFileInfo[],
  page: number,
  pageSize = 50
): DriveListResponse {
  const start = (page - 1) * pageSize
  const slice = items.slice(start, start + pageSize)
  return {
    files: slice,
    pagination: { page, page_size: pageSize, total: items.length },
  }
}

export function listDemoDriveFiles(
  files: DriveFileInfo[],
  folderPath: string,
  page: number
): DriveListResponse {
  const children = listChildren(files, folderPath)
  return paginate(children, page)
}

export function listDemoDriveRecent(files: DriveFileInfo[]): DriveListResponse {
  const byId = new Map(files.map((f) => [f.file_id, f]))
  const recent = DEMO_DRIVE_RECENT_IDS
    .map((id) => byId.get(id))
    .filter((f): f is DriveFileInfo => Boolean(f))
  return { files: recent, pagination: { page: 1, page_size: 50, total: recent.length } }
}

export function listDemoDriveStarred(
  files: DriveFileInfo[],
  folderPath: string
): DriveListResponse {
  const folder = normalizeFolderPath(folderPath)
  const starred = files.filter(
    (f) =>
      f.is_favorite &&
      (folder === "/" ? true : f.path.startsWith(folder === "/" ? "" : folder + "/"))
  )
  return { files: starred, pagination: { page: 1, page_size: 50, total: starred.length } }
}

export function listDemoDriveTrash(): DriveListResponse {
  return {
    files: [...DEMO_DRIVE_TRASH],
    pagination: { page: 1, page_size: 50, total: DEMO_DRIVE_TRASH.length },
  }
}

export function listDemoDriveShared(): DriveListResponse {
  return {
    files: [...DEMO_DRIVE_SHARED],
    pagination: { page: 1, page_size: 50, total: DEMO_DRIVE_SHARED.length },
  }
}

export function searchDemoDriveFiles(
  files: DriveFileInfo[],
  query: string,
  scope: string,
  folderPath: string,
  page: number
): DriveListResponse {
  const q = query.trim().toLowerCase()
  let pool = files
  if (scope === "folder") {
    const folder = normalizeFolderPath(folderPath)
    pool = files.filter(
      (f) => f.path === folder || f.path.startsWith(folder === "/" ? "/" : folder + "/")
    )
  }
  const matched = pool.filter((f) => {
    if (!q) return false
    return (
      f.name.toLowerCase().includes(q) ||
      f.path.toLowerCase().includes(q)
    )
  })
  return paginate(matched, page)
}

export function createInitialDemoDriveFiles(): DriveFileInfo[] {
  return [...DEMO_DRIVE_FILES]
}
