/**
 * SEO-friendly detail URLs: /base/{id}/{slug}
 *
 * The `id` stays the lookup key (it is unique per franchisee — the same program
 * can have a different id for each franchisee), so data fetching is unchanged.
 * The `slug` is appended purely for SEO / readability.
 *
 * Always falls back to /base/{id} (or /base) when no slug/id is available, so
 * existing links never break.
 */
export function toUrlSlug(slug?: string | null): string {
  return (slug ?? "")
    .toString()
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, "-") // non-alphanumerics → hyphen (BUILD_UP → build-up)
    .replace(/^-+|-+$/g, ""); // trim leading/trailing hyphens
}

export function detailHref(
  base: string,
  id?: string | null,
  slug?: string | null,
): string {
  if (!id) return base;
  const s = toUrlSlug(slug);
  return s ? `${base}/${id}/${s}` : `${base}/${id}`;
}
