import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

export function timeAgo(dateStr?: string | null): string | null {
  if (!dateStr) return null;
  const diffMs = Date.now() - new Date(dateStr).getTime();
  const diffMins = Math.floor(diffMs / 60000);
  if (diffMins < 1) return "just now";
  if (diffMins < 60) return `${diffMins}m ago`;
  const diffHours = Math.floor(diffMins / 60);
  if (diffHours < 24) return `${diffHours}h ago`;
  return new Date(dateStr).toLocaleDateString();
}

export function statusDot(status: "online" | "busy" | "offline"): string {
  return status === "online"
    ? "bg-emerald-500"
    : status === "busy"
    ? "bg-amber-500"
    : "bg-zinc-600";
}

/** Format path with folder name prominent: "myproject" or "…/parent/myproject" */
export function shortPath(path: string): { folder: string; parent: string } {
  const parts = path.split("/").filter(Boolean);
  const folder = parts.pop() || path;
  const parent = parts.length > 1 ? `…/${parts.slice(-1)[0]}/` : parts.length === 1 ? `${parts[0]}/` : "";
  return { folder, parent };
}
