export const locales = ["de", "en"] as const;
export type Locale = (typeof locales)[number];
export const defaultLocale: Locale = "de";

export type PublicRouteId =
  | "home"
  | "about"
  | "projects"
  | "projectItem"
  | "contact"
  | "legal"
  | "privacy"
  | "adminLogin";

export type ResolvedPath =
  | { locale: Locale; route: Exclude<PublicRouteId, "projectItem"> }
  | { locale: Locale; route: "projectItem"; slug: string };

const staticPublic: Record<Locale, Record<Exclude<PublicRouteId, "projectItem" | "adminLogin">, string>> = {
  de: {
    home: "/",
    about: "/ueber-mich",
    projects: "/projekte",
    contact: "/kontakt",
    legal: "/impressum",
    privacy: "/datenschutz",
  },
  en: {
    home: "/en",
    about: "/en/about",
    projects: "/en/projects",
    contact: "/en/contact",
    legal: "/en/legal",
    privacy: "/en/privacy",
  },
};

const projectsPrefix: Record<Locale, string> = {
  de: "/projekte",
  en: "/en/projects",
};

const adminLogin = "/admin/login";

export function isLocale(value: string): value is Locale {
  return locales.includes(value as Locale);
}

export function normalizePath(pathname: string): string {
  if (!pathname) return "/";
  if (pathname.length > 1 && pathname.endsWith("/")) return pathname.slice(0, -1);
  return pathname;
}

export function publicPath(locale: Locale, route: PublicRouteId, slug?: string): string {
  if (route === "projectItem") return `${projectsPrefix[locale]}/${slug ?? ""}`;
  if (route === "adminLogin") return adminLogin;
  return staticPublic[locale][route];
}

export function resolvePublicPath(pathname: string): ResolvedPath | null {
  const path = normalizePath(pathname);
  if (path === adminLogin || path === "/admin") {
    return { locale: defaultLocale, route: "adminLogin" };
  }
  for (const locale of locales) {
    for (const [route, url] of Object.entries(staticPublic[locale])) {
      if (path === url) {
        return { locale, route: route as Exclude<PublicRouteId, "projectItem"> };
      }
    }
    const pfx = projectsPrefix[locale];
    if (path.startsWith(`${pfx}/`)) {
      const slug = path.slice(pfx.length + 1);
      if (slug && !slug.includes("/")) return { locale, route: "projectItem", slug };
    }
  }
  return null;
}

export function toInternalPath(pathname: string): string | null {
  const resolved = resolvePublicPath(pathname);
  if (!resolved) return null;
  if (resolved.route === "home") return `/${resolved.locale}`;
  if (resolved.route === "projectItem") {
    return `/${resolved.locale}/projects/${resolved.slug}`;
  }
  if (resolved.route === "adminLogin") return null;
  return `/${resolved.locale}/${resolved.route}`;
}

export function switchLocale(pathname: string, target: Locale): string | null {
  const resolved = resolvePublicPath(pathname);
  if (!resolved) return null;
  if (resolved.route === "projectItem") return publicPath(target, "projectItem", resolved.slug);
  if (resolved.route === "adminLogin") return null;
  return publicPath(target, resolved.route);
}

export function toPublicFromInternal(pathname: string): string | null {
  const path = normalizePath(pathname);
  const match = path.match(/^\/(de|en)(?:\/(.*))?$/);
  if (!match) return null;
  const locale = match[1] as Locale;
  const rest = match[2] ?? "";
  if (!rest) return publicPath(locale, "home");
  const [segment, slug] = rest.split("/");
  if (slug && segment === "projects") return publicPath(locale, "projectItem", slug);
  if (slug) return null;
  const map: Record<string, Exclude<PublicRouteId, "projectItem" | "adminLogin" | "home">> = {
    about: "about",
    projects: "projects",
    contact: "contact",
    legal: "legal",
    privacy: "privacy",
  };
  return map[segment] ? publicPath(locale, map[segment]) : null;
}
