/**
 * Sidebar active states — light yellow/amber highlight for current route (all roles).
 */

export function isPathActive(pathname, to, { prefix = false } = {}) {
  if (!to) return false;
  if (prefix) {
    return pathname === to || pathname.startsWith(`${to}/`);
  }
  return pathname === to;
}

/** MUI admin / teacher / institute main link */
export function roleMainNavClass(collapsed, active) {
  const size = collapsed
    ? "!w-[40px] !h-[40px] !min-w-[40px] !p-0 !flex !justify-center !rounded-lg !mx-1"
    : "w-full !justify-start !px-3 !rounded-r-lg !mx-1 !border-l-[3px]";
  const state = active
    ? "!border-amber-500 !bg-[#fffbeb] !text-stone-900 !font-semibold shadow-[inset_0_0_0_1px_rgba(251,191,36,0.2)]"
    : "!border-transparent !text-[rgba(0,0,0,0.8)] !font-medium hover:!bg-[#fffbeb]/80";
  return `${size} !capitalize flex gap-3 !text-[16px] items-center !py-2 transition-colors duration-200 ${state}`;
}

/** Nested link under collapsible section */
export function roleSubNavClass(active) {
  return `!capitalize !justify-start !w-full !text-[13px] !pl-9 flex gap-3 !py-1.5 !rounded-r-lg !mx-1 !border-l-[3px] transition-colors duration-200 ${
    active
      ? "!border-amber-500 !bg-[#fffbeb] !text-stone-900 !font-semibold"
      : "!border-transparent !text-[rgba(0,0,0,0.72)] !font-medium hover:!bg-[#fffbeb]/80"
  }`;
}

export function deriveAdminSubmenu(pathname) {
  if (pathname.startsWith("/admin/pages")) return 1;
  if (pathname.startsWith("/admin/refund-management")) return 2;
  if (
    pathname.startsWith("/admin/support") ||
    pathname.startsWith("/admin/manage-faqs") ||
    pathname.startsWith("/admin/manage-knwoledgebase")
  ) {
    return 3;
  }
  return null;
}

export function deriveTeacherSubmenu(pathname) {
  if (
    pathname.startsWith("/teacher/classes/add") ||
    pathname.startsWith("/teacher/classses") ||
    pathname === "/teacher/classes" ||
    (pathname.startsWith("/teacher/classes/") && pathname.includes("/update"))
  ) {
    return 1;
  }
  if (pathname.startsWith("/teacher/upload-studypack") || pathname.startsWith("/teacher/manage-studypacks")) {
    return 2;
  }
  if (
    pathname.startsWith("/teacher/classes/uploadmaterials") ||
    pathname.startsWith("/teacher/classes/select") ||
    pathname.startsWith("/teacher/extension-requests") ||
    /^\/[^/]+\/materials$/.test(pathname)
  ) {
    return 3;
  }
  if (
    pathname.startsWith("/teacher/support-form") ||
    pathname.startsWith("/teacher/support-ticket") ||
    pathname.startsWith("/teacher/support/")
  ) {
    return 5;
  }
  return null;
}

export function deriveInstituteSubmenu(pathname) {
  if (pathname.startsWith("/institute/support-form") || pathname.startsWith("/institute/support-ticket")) {
    return 5;
  }
  return null;
}

export function deriveStudentSubmenu(pathname) {
  if (
    pathname.startsWith("/student/dashboard/available-classes") ||
    pathname.startsWith("/student/dashboard/my-classes")
  ) {
    return 1;
  }
  if (pathname.startsWith("/student/studypacks") || pathname.startsWith("/student/purchased-studypacks")) {
    return 8;
  }
  if (
    pathname.startsWith("/student/dashboard/payment-history") ||
    pathname.startsWith("/student/dashboard/refund-request") ||
    pathname.startsWith("/student/dashboard/refund-history")
  ) {
    return 2;
  }
  return null;
}
