import type { Locale } from "@/types/sections";

/**
 * Page keys used internally. Each maps to FR and EN URL slugs.
 * The FR slug is the canonical one (no language prefix).
 * EN routes live under /en/<slug>.
 */
export type PageKey =
  | "home"
  | "municipality"
  | "mayor"
  | "council"
  | "services"
  | "documents"
  | "community"
  | "communityPhotos"
  | "activitiesPhotos"
  | "developmentPlan"
  | "townMap"
  | "businesses"
  | "events"
  | "publicNotices"
  | "minutes"
  | "newsletters";

export const ROUTES: Record<PageKey, { fr: string; en: string }> = {
  home:             { fr: "/",                                  en: "/en" },
  municipality:     { fr: "/municipalite",                      en: "/en/municipality" },
  mayor:            { fr: "/mot-du-maire",                      en: "/en/word-from-the-mayor" },
  council:          { fr: "/conseil-municipal",                 en: "/en/our-council" },
  services:         { fr: "/services-municipaux-et-publics",    en: "/en/our-services" },
  documents:        { fr: "/documents-importants",              en: "/en/important-documents" },
  community:        { fr: "/notre-communaute",                  en: "/en/our-community" },
  communityPhotos:  { fr: "/album-photos-communaute",           en: "/en/community-photo-album" },
  activitiesPhotos: { fr: "/album-photos-activites",            en: "/en/activities-photo-album" },
  developmentPlan:  { fr: "/plan-de-developpement",             en: "/en/development-plan" },
  townMap:          { fr: "/carte-de-la-ville",                 en: "/en/town-map" },
  businesses:       { fr: "/entreprises-locales",               en: "/en/local-businesses" },
  events:           { fr: "/evenements",                        en: "/en/events" },
  publicNotices:    { fr: "/avis-publics",                      en: "/en/public-notices" },
  minutes:          { fr: "/proces-verbaux",                    en: "/en/minutes" },
  newsletters:      { fr: "/bulletins",                         en: "/en/newsletters" },
};

export function getPath(page: PageKey, locale: Locale): string {
  return ROUTES[page][locale];
}

/** Given the current pathname, return the equivalent path in the target locale. */
export function switchLocalePath(pathname: string, target: Locale): string {
  const normalized = pathname.replace(/\/+$/, "") || "/";
  for (const key of Object.keys(ROUTES) as PageKey[]) {
    const fr = ROUTES[key].fr;
    const en = ROUTES[key].en;
    if (normalized === fr || normalized === en) {
      return ROUTES[key][target];
    }
  }
  return target === "en" ? "/en" : "/";
}

/** Detect the locale implied by a pathname (anything under /en/* or /en is EN). */
export function detectLocaleFromPath(pathname: string): Locale {
  return pathname === "/en" || pathname.startsWith("/en/") ? "en" : "fr";
}