import { useLocation } from "@/lib/router";
import { cn } from "@/lib/utils";

/**
 * Derive breadcrumb segments from a pathname.
 *
 * Exposed for unit testing — the component itself just consumes this.
 *
 * Mapping rules:
 * - `/:prefix/assistant*` → `["ASSISTANT"]` (any suffix collapses)
 * - `/:prefix/content-studio` → `["STUDIO"]`
 * - `/:prefix/content-studio/<sub>` → `["STUDIO", "<SUB>"]`
 * - `/:prefix/convert*` → `["STUDIO", "CONVERT"]` (Phase 10 folds Convert into Studio)
 * - `/:prefix/projects` → `["PROJECTS"]`
 * - `/:prefix/projects/<slug>*` → `["PROJECTS", "<SLUG>"]`
 * - `/:prefix/(issues|agents|routines|goals|approvals|costs|activity|inbox)` → `["PROJECTS"]`
 * - `/:prefix/issues/<slug>` → `["PROJECTS", "<SLUG>"]`
 * - `/instance/settings` → `["SETTINGS"]`
 * - `/instance/settings/<sub>` (non-general) → `["SETTINGS", "<SUB>"]`
 * - everything else → `["HOME"]`
 */
export function deriveBreadcrumbSegments(pathname: string): string[] {
  // /instance/settings/...
  if (pathname === "/instance/settings" || pathname.startsWith("/instance/settings/")) {
    const rest = pathname.replace(/^\/instance\/settings\/?/, "");
    if (!rest) return ["SETTINGS"];
    const leaf = rest.split("/")[0] ?? "";
    if (!leaf || leaf === "general") return ["SETTINGS"];
    return ["SETTINGS", leaf.toUpperCase()];
  }

  // Company-prefixed routes: /NEX/<rest>
  const match = pathname.match(/^\/[^/]+\/(.*)$/);
  if (!match) return ["HOME"];
  const rest = match[1] ?? "";
  if (!rest) return ["HOME"];

  const segments = rest.split("/").filter(Boolean);
  const head = segments[0];
  const next = segments[1];

  switch (head) {
    case "assistant":
      return ["ASSISTANT"];

    case "content-studio":
    case "studio":
      return next ? ["STUDIO", next.toUpperCase()] : ["STUDIO"];

    case "convert":
      return ["STUDIO", "CONVERT"];

    case "projects":
      return next ? ["PROJECTS", next.toUpperCase()] : ["PROJECTS"];

    case "issues":
    case "agents":
    case "routines":
    case "goals":
    case "approvals":
    case "costs":
    case "activity":
    case "inbox":
    case "execution-workspaces":
      return next ? ["PROJECTS", next.toUpperCase()] : ["PROJECTS"];

    default:
      return ["HOME"];
  }
}

export function ModeBreadcrumb() {
  const { pathname } = useLocation();
  const segments = deriveBreadcrumbSegments(pathname);

  return (
    <div
      data-testid="mode-breadcrumb"
      className="flex items-center gap-2 text-[14px] font-semibold uppercase tracking-[0.1em]"
    >
      {segments.map((segment, index) => {
        const isLeaf = index === segments.length - 1;
        return (
          <span key={index} className="flex items-center gap-2">
            {index > 0 && (
              <span
                data-testid="mode-breadcrumb-sep"
                aria-hidden="true"
                className="text-muted-foreground"
              >
                /
              </span>
            )}
            <span
              data-testid="mode-breadcrumb-segment"
              className={cn(isLeaf ? "text-primary" : "text-muted-foreground")}
            >
              {segment}
            </span>
          </span>
        );
      })}
    </div>
  );
}
