import { notFound } from "next/navigation";
import type { Locale } from "@/types/content";

export const locales = ["en", "id"] as const;
export const defaultLocale: Locale = "en";

export type RouteKind =
  | "home"
  | "about"
  | "solutions"
  | "solution"
  | "products"
  | "product"
  | "industries"
  | "industry"
  | "case-studies"
  | "case-study"
  | "room-designer"
  | "partners"
  | "resources"
  | "contact"
  | "insights"
  | "insight"
  | "privacy-policy";

export type ParsedRoute = {
  locale: Locale;
  kind: RouteKind;
  slug?: string;
  path: string;
};

const staticRoutes: Record<string, RouteKind> = {
  about: "about",
  solutions: "solutions",
  products: "products",
  industries: "industries",
  "success-stories": "case-studies",
  "room-designer": "room-designer",
  partners: "partners",
  resources: "resources",
  "talk-to-akarya": "contact",
  insights: "insights",
  "privacy-policy": "privacy-policy"
};

export function localizedPath(path: string, locale: Locale) {
  const cleanPath = path === "/" ? "" : path.replace(/^\/id(?=\/|$)/, "");
  if (locale === "en") return cleanPath || "/";
  return `/id${cleanPath || ""}`;
}

export function parseSegments(segments?: string[]): ParsedRoute {
  const parts = segments ?? [];
  const locale: Locale = parts[0] === "id" ? "id" : "en";
  const rest = locale === "id" ? parts.slice(1) : parts;
  const path = `/${parts.join("/")}`.replace(/\/$/, "") || "/";

  if (rest.length === 0) return { locale, kind: "home", path };

  if (rest.length === 1 && staticRoutes[rest[0]]) {
    return { locale, kind: staticRoutes[rest[0]], path };
  }

  if (rest.length === 2 && rest[0] === "solutions") {
    return { locale, kind: "solution", slug: rest[1], path };
  }

  if (rest.length === 2 && rest[0] === "products") {
    return { locale, kind: "product", slug: rest[1], path };
  }

  if (rest.length === 2 && rest[0] === "industries") {
    return { locale, kind: "industry", slug: rest[1], path };
  }

  if (rest.length === 2 && rest[0] === "success-stories") {
    return { locale, kind: "case-study", slug: rest[1], path };
  }

  if (rest.length === 2 && rest[0] === "insights") {
    return { locale, kind: "insight", slug: rest[1], path };
  }

  notFound();
}

export function getPathForKind(kind: RouteKind, slug?: string) {
  switch (kind) {
    case "home":
      return "/";
    case "solution":
      return `/solutions/${slug}`;
    case "product":
      return `/products/${slug}`;
    case "industry":
      return `/industries/${slug}`;
    case "case-study":
      return `/success-stories/${slug}`;
    case "insight":
      return `/insights/${slug}`;
    default:
      if (kind === "contact") return "/talk-to-akarya";
      return kind === "case-studies" ? "/success-stories" : `/${kind}`;
  }
}
