
import {
  navigation,
  type NavigationItem,
} from "@/config/dashboard-navigation";


export function normalizePath(path: string): string {
  return path.replace(/\/+$/, "") || "/";
}


export function formatRouteTitle(segment: string): string {
  return segment
    .replace(/-/g, " ")
    .replace(/\b\w/g, (char) => char.toUpperCase());
}


export function getActiveNavigation(pathname: string) {
  const currentPath = normalizePath(pathname);

  for (const item of navigation) {
    const parentPath = normalizePath(item.href);

    // Exact parent match
    if (currentPath === parentPath) {
      return {
        parent: item,
        child: null,
      };
    }

    // Exact child match
    const child = item.children?.find(
      (child) => normalizePath(child.href) === currentPath
    );

    if (child) {
      return {
        parent: item,
        child,
      };
    }
  }

  return {
    parent: null,
    child: null,
  };
}


export function isItemActive(
  pathname: string,
  item: NavigationItem
): boolean {
  const currentPath = normalizePath(pathname);
  const parentPath = normalizePath(item.href);

  if (parentPath === "/dashboard") {
    return currentPath === "/dashboard";
  }

  // Parent route match
  if (
    currentPath === parentPath ||
    currentPath.startsWith(`${parentPath}/`)
  ) {
    return true;
  }

  // Child route match
  return (
    item.children?.some((child) => {
      const childPath = normalizePath(child.href);

      return (
        currentPath === childPath ||
        currentPath.startsWith(`${childPath}/`)
      );
    }) ?? false
  );
}


export function isChildActive(
  pathname: string,
  href: string
): boolean {
  const currentPath = normalizePath(pathname);
  const childPath = normalizePath(href);

  return currentPath === childPath;
}


function getDynamicRoute(pathname: string) {
  const segments = normalizePath(pathname)
    .split("/")
    .filter(Boolean);

  // User Details
  if (
    segments.length === 3 &&
    segments[0] === "dashboard" &&
    segments[1] === "users" &&
    segments[2] !== "create"
  ) {
    return {
      title: "User Details",
      breadcrumb: [
        "Dashboard",
        "Users",
        "User Details",
      ],
    };
  }

  // Registration Details
  if (
    segments.length === 3 &&
    segments[0] === "dashboard" &&
    segments[1] === "registrations" &&
    segments[2] !== "create"
  ) {
    return {
      title: "Registration Details",
      breadcrumb: [
        "Dashboard",
        "Registrations",
        "Registration Details",
      ],
    };
  }

  return null;
}

export function getDashboardBreadcrumb(pathname: string) {
  const currentPath = normalizePath(pathname);

  // Check dynamic routes first
  const dynamicRoute = getDynamicRoute(currentPath);

  if (dynamicRoute) {
    return dynamicRoute;
  }

  // Find static navigation
  const { parent, child } = getActiveNavigation(currentPath);

  if (parent) {
    const title = child?.label ?? parent.label;

    return {
      title,
      breadcrumb:
        title === "Dashboard"
          ? ["Dashboard"]
          : parent.label === title
            ? ["Dashboard", parent.label]
            : ["Dashboard", parent.label, title],
    };
  }

  // Fallback for unknown routes
  const lastSegment =
    currentPath.split("/").filter(Boolean).at(-1) ?? "dashboard";

  const title = formatRouteTitle(lastSegment);

  return {
    title,
    breadcrumb: ["Dashboard", title],
  };
}


export function getPageTitle(pathname: string): string {
  return getDashboardBreadcrumb(pathname).title;
}

export function getBreadcrumb(pathname: string): string[] {
  return getDashboardBreadcrumb(pathname).breadcrumb;
}
